A data encryption and decryption method based on improved XOR algorithm

Through the two-layer encryption method, combined with XOR operation and byte value operation, the contradiction between security and efficiency of existing encryption algorithms is solved, and efficient, secure encryption and decryption of various data is achieved.

CN111177760BActive Publication Date: 2024-11-12BEIJING RES INST OF URANIUM GEOLOGY
View PDF 0 Cites 0 Cited by

Patent Information

Application Number
CN201911396873.8
Authority / Receiving Office
CN · China
Patent Type
Patents(China)
Current Assignee / Owner
Filing Date
2019-12-30
Publication Date
2024-11-12
Estimated Expiration
2039-12-30

AI Technical Summary

Technical Problem

There is a contradiction between security and efficiency in existing encryption algorithms. Some algorithms have complex and inefficient processing of special characters. Simple XOR encryption has poor security and cannot effectively protect various types of data.

Method used

The two-layer encryption method is adopted, firstly using XOR operation for the first layer encryption, and then using byte value operation for the second layer encryption. The opposite steps are performed during decryption to ensure data security and efficiency.

Benefits of technology

It improves the security of the XOR algorithm, and can efficiently encrypt and decrypt any type of data, ensuring the security of the data and the broadness of usage conditions.

✦ Generated by Eureka AI based on patent content.
Patent Text Reader

Abstract

The present invention belongs to the field of computer information security, and specifically relates to a data encryption and decryption method based on an improved XOR algorithm, the method specifically comprising the following steps: step (1) using XOR operation to perform the first layer of encryption; step (2) using byte value operation to perform the second layer of encryption; step (3) using byte value operation to perform the first layer of decryption; step (4) using XOR operation to perform the second layer of decryption. The present invention uses two layers of encryption, which is not only simple in principle and has wide application conditions, but also can encrypt any type of data without decryption if encrypted repeatedly, and has high security.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] The invention belongs to the field of computer information security, and in particular relates to a data encryption and decryption method based on an improved XOR algorithm. Background Art

[0002] Since entering the information age, Internet technology and computer technology have been integrated into information processing in various industries. Although it is convenient to process various information and data, it makes the data face the risk of being stolen, which has many adverse effects on the privacy of personal information and the security of corporate data. There are many encryption algorithms today, and their security and encryption speed vary. However, most algorithms are relatively complex. Some algorithms encrypt strings with some special characters, which require special encoding processing in HTTP requests. The use conditions are harsh and have a certain impact on efficiency. Although the principle of simple XOR encryption operation is simple, it can be decrypted to obtain plaintext by encrypting it twice with a password, which has poor security.

[0003] Therefore, it is necessary to provide a data encryption technology using double-layer encryption, which can encrypt any type of data with high security. Summary of the invention

[0004] The purpose of the present invention is to provide a data encryption and decryption method based on an improved XOR algorithm, which uses special rules to encrypt data and improves the security of the original XOR algorithm.

[0005] The technical solution to achieve the purpose of the present invention is:

[0006] A data encryption and decryption method based on an improved XOR algorithm, the method specifically comprising the following steps:

[0007] Step (1) uses XOR operation to perform the first layer of encryption;

[0008] Step (2) uses byte value operations to perform a second layer of encryption;

[0009] Step (3) performs first-level decryption using byte value operations;

[0010] Step (4) uses XOR operation to perform second-level decryption.

[0011] The step (1) specifically comprises the following steps:

[0012] Step (1.1) for a file, use a programming language to obtain the bytes of the file;

[0013] Step (1.2) sets the password, specifies the encoding and converts the password into byte data;

[0014] Step (1.3) cyclically concatenates the password bytes into data sets of equal length according to the length of the file bytes;

[0015] Step (1.4) performs XOR operation on the file bytes and the cyclically concatenated password bytes one by one to obtain the first layer of encrypted data set.

[0016] In the step (1.2), the password can be any characters, and its length is less than or equal to the length of the file byte set.

[0017] The meaning of the cyclic splicing in the step (1.3) is that, assuming there is data 'student' and password 'abc', after cyclic splicing according to the length of 'student', the password is 'abcabca'.

[0018] The step (2) specifically comprises the following steps:

[0019] Step (2.1) continues to traverse and take values ​​of the first-layer encrypted data set. Let the value be t (0≤t≤255). Add any positive integer n to t to get T. Then, the remainder of T with respect to 256 is t′. The conversion formula of integer t′ is as follows:

[0020] t′=(t+n)%256 n∈N* and n≠256*m, m∈N*

[0021] Step (2.2) converts the integer type t′ into a byte type, and traverses and writes it to the disk to obtain the ciphertext.

[0022] The step (3) specifically comprises the following steps:

[0023] Step (3.1) uses a programming language to obtain the bytes of the file to be decrypted;

[0024] Step (3.2) traverses the byte data and takes the value, setting the value to t′ (0≤t′≤255), and performing the following formula operation on t′ to obtain t;

[0025] t=(t′+256-n)%256 n∈N* and n≠256*m, m∈N*

[0026] Store each t traversal into a dataset.

[0027] The step (4) specifically comprises the following steps:

[0028] Step (4.1) uses the same password (same code) in the encryption process, and performs XOR on the above data set according to step (1.2), step (1.3), and step (1.4) to obtain decrypted data;

[0029] Step (4.2) writes the original file to disk.

[0030] The beneficial technical effects of the present invention are:

[0031] The present invention provides a data encryption and decryption method based on an improved XOR algorithm, which uses two layers of encryption. Not only is the principle simple, but the application conditions are wide, and repeated encryption will not result in decryption. Any type of data can be encrypted with high security. DETAILED DESCRIPTION

[0032] The present invention is further described in detail below in conjunction with embodiments.

[0033] The present invention relates to computer data reading and writing, data types, XOR operations and other related knowledge.

[0034] Byte is the file storage unit. The bottom layer of any file or data is 0 and 1. Eight bits represent a byte. Byte is the smallest storage unit. All files are binary files, that is, byte files. The decimal value of an eight-bit byte is -128 to 127, which can represent 256 different integers. The value range of byte data in computers is an integer from 0 to 255.

[0035] The present invention provides a data encryption and decryption method based on an improved XOR algorithm, the method specifically comprising the following steps:

[0036] Step (1) uses XOR operation to perform the first layer of encryption;

[0037] XOR operation is also called half-addition operation. Its operation rules are equivalent to binary addition without carry: in binary, 1 represents true and 0 represents false. The XOR operation rules are: 0⊕0=0, 1⊕0=1, 0⊕1=1, 1⊕1=0 (the same is 0, different is 1, ⊕ is XOR). These rules are the same as addition, but without carry. Any file is composed of bytes (eight bits of 0 or 1 represent one byte). We use the bytes converted from the password to perform XOR operation on the bytes of the file to be encrypted, so as to encrypt it. The specific steps are as follows:

[0038] In step (1.1), for a file, use a programming language to obtain the bytes of the file.

[0039] Use Python language to get the byte data a_bytes of image a

[0040] a_bytes=open(str(a_Path),mode="rb")

[0041] a_Path is the input path of image a.

[0042] Step (1.2) sets the password, specifies the encoding and converts the password into byte data. The password can be any character and its length is less than or equal to the length of the file byte set.

[0043] Enter the password to get the byte data of the password, specify the encoding, the password is "123456", the encoding is "utf-8"

[0044] pwd_bytes=bytes("123456","utf-8")

[0045] Step (1.3) cyclically concatenates the password bytes into data sets of equal length according to the length of the file bytes.

[0046] The meaning of cyclic concatenation is that if there is data 'student' and password 'abc', the password will be 'abcabca' after cyclic concatenation based on the length of 'student'.

[0047] Loop and concatenate the password bytes according to the image byte length to get pwds_bytes

[0048] pwds_bytes=a_bytesLen / / pwd_bytesLen*pwd_bytes+pwd_bytes[:a_bytesLen% pwd_bytesLen]

[0049] Step (1.4) performs XOR operation on the file bytes and the cyclically concatenated password bytes one by one to obtain the first layer of encrypted data set.

[0050] Each value of pwds_bytes performs XOR operation on each value of a_bytes to obtain a new value. A_bytes is the data set after XOR.

[0051] A_bytes[i]=pwds_bytes[i]^a_bytes[i], ^ is the XOR operator in Python.

[0052] Step (2) uses byte value operations to perform the second layer of encryption, which specifically includes the following sub-steps:

[0053] Step (2.1) continues to traverse the first layer of encrypted data set to obtain values, set the value to t (0≤t≤255), add any positive integer n to t to get T, and make T remainder 256 to get t′ (Formula 1); in this way, the purpose of using the same password to XOR the ciphertext but not decrypt it is achieved, thereby improving data security. The conversion formula of integer t′ is as follows:

[0054] t′=(t+n)%256 n∈N* and n≠256*m, m∈N* (1)

[0055] Traverse A_bytes, add n (n=10) to each value t in A_bytes and perform a modulo 256 operation to get t'

[0056] t′ = (t + 10) % 256

[0057] In step (2.2), convert the integer t′ into a byte type and traverse and write it to the disk to obtain the ciphertext.

[0058] Converting t′ into a byte type and traversing and outputting it to the disk can obtain A′.jpg

[0059] f_write.write(bytes([t′]))

[0060] Decryption of Figure A′:

[0061] Step (3) Perform the first layer of decryption using byte value operations, which specifically includes the following sub-steps:

[0062] Step (3.1) Use a programming language to obtain the bytes of the file to be decrypted;

[0063] Obtain the byte data A′_bytes of picture A′

[0064] A′_bytes = open(str(A′_Path), mode = "rb")

[0065] A′_Path is the path of picture A′

[0066] Step (3.2) Traverse and take values from the byte data, set the value as t′ (0 ≤ t′ ≤ 255), and perform the following formula operation on t′ to obtain t.

[0067] t = (t′ + 256 - n) % 256 where n ∈ N* and n ≠ 256 * m, m ∈ N*

[0068] Note: n is the same as n in the encryption process.

[0069] Traverse and store each t in the dataset.

[0070] Traverse A′_bytes, perform the following operation on each value t′ in A′_bytes to obtain t, and traverse and output to obtain the dataset A_bytes

[0071] t = (t′ + 256 - 10) % 256

[0072] Step (4) Perform the second layer of decryption using XOR operation, which specifically includes the following sub-steps:

[0073] Step (4.1) Use the same password (same encoding) in the encryption process to perform XOR on the above dataset according to step (1.2), step (1.3), and step (1.4) to obtain the decrypted data.

[0074] According to step 1.2, step 1.3, and step 1.4, the password is converted into password bytes according to the same encoding, and the password bytes are cyclically concatenated according to the length of A_bytes to obtain pwds_bytes. Each value of pwds_bytes is XOR-ed with each value of A_bytes to obtain a new value, and the decrypted data can be obtained.

[0075] pwd_bytes=bytes("123456","utf-8")

[0076] pwds_bytes=A_bytesLen / / pwd_bytesLen*pwd_bytes+pwd_bytes[:A_bytesLen%pwd_bytesLen]

[0077] a_bytes[i]=pwds_bytes[i]^A_bytes[i]

[0078] Step (4.2) writes the original file to disk.

[0079] Traversing and writing to disk will yield a.jpg.

[0080] f_write.write(bytes([a_bytes[i]]))

[0081] The present invention is described in detail above in conjunction with the embodiments, but the present invention is not limited to the above embodiments, and various changes can be made within the knowledge of ordinary technicians in the field without departing from the purpose of the present invention. The contents not described in detail in the present invention can adopt the existing technology.

Claims

1. A data encryption and decryption method based on an improved XOR algorithm. It is characterized in that The method specifically comprises the following steps: Step (1) uses XOR operation to perform the first layer of encryption; Step (2) uses byte value operations to perform a second layer of encryption; Step (3) performs first-level decryption using byte value operations; Step (4) performs second-layer decryption using an XOR operation; The step (1) specifically comprises the following steps: Step (1.1) for a file, use a programming language to obtain the bytes of the file; Step (1.2) sets the password, specifies the encoding and converts the password into byte data; Step (1.3) cyclically concatenates the password bytes into data sets of equal length according to the length of the file bytes; Step (1.4) performs XOR operation on the file bytes and the cyclically concatenated password bytes one by one to obtain the first layer of encrypted data set; In the step (1.2), the password can be any characters, and the length is less than or equal to the length of the file byte set; The meaning of the cyclic splicing in the step (1.3) is that, assuming there is data 'student' and password 'abc', after cyclic splicing according to the length of 'student', the password is 'abcabca'; The step (2) specifically comprises the following steps: Step (2.1) continues to traverse and take values ​​of the first-layer encrypted data set. Let the value be t (0≤t≤255). Add any positive integer n to t to get T. Then, the remainder of T with respect to 256 is tˊ. The conversion formula of integer type tˊ is as follows: tˊ=(t+n)%256n∈N* and n≠256*m, m∈N* Step (2.2) converts the integer type tˊ into a byte type, and traverses and writes it to the disk to obtain the ciphertext.

2. According to the data encryption and decryption method based on the improved XOR algorithm of claim 1, It is characterized in that The step (3) specifically comprises the following steps: Step (3.1) uses a programming language to obtain the bytes of the file to be decrypted; Step (3.2) traverses the byte data and takes the value, setting the value to tˊ(0≤tˊ≤255), and performs the following formula operation on tˊ to obtain t; t=(tˊ+256-n)%256n∈N* and n≠256*m, m∈N* Store each t traversal into a dataset.

3. According to the data encryption and decryption method based on the improved XOR algorithm of claim 2, It is characterized in that The step (4) specifically comprises the following steps: Step (4.1) uses the same password in the encryption process to perform XOR on the above data set according to step (1.2), step (1.3), and step (1.4) to obtain decrypted data; Step (4.2) writes the original file to disk.