Python tutorial realizes software encryption function by itself

  • 2021-12-04 19:16:52
  • OfStack

Directory Principle Encryption Operation: Decryption Operation: Generate Random Key: Encryption Unit: Decryption Unit:
Encrypt text file decrypt file

Basic knowledge

In Python, the XOR operator is: ^, which can also be denoted as XOR. Bitwise exclusive OR means that the same value exclusive OR is 0 and different values exclusive OR is 1. Specifically, there are four possibilities: 0 ^ 0 = 0, 0 ^ 1 = 1, 1 ^ 0 = 1, and 1 ^ 1 = 0. We can also summarize the rule (A is 0 or 1): 0 and A are exclusive or A itself; The exclusive OR of 1 and A is the inverse of A.

Let's look at the properties that a 1-bit binary number satisfies:

The exclusive OR value of a 1-bit binary number with itself is 0

b ^ b = 0

Exclusive OR operation satisfies commutative law

a ^ b ^ c = a ^ (b ^ c) = (a ^ b) ^ c

The XOR of 0 and a is a

(a ^ b) ^ b = a ^ (b ^ b) = a ^ 0 = a

It is easy to know that the above properties are satisfied for any long binary number.

Principle

By understanding the nature of XOR operation, the encryption principle is very clear.

Encryption operation:

Firstly, the file is converted into binary number, and then a random key with the same length as the binary number is generated. The binary number and the key are operated by XOR to obtain the encrypted binary number.

Decryption operation:

The original binary number is obtained by XOR operation between the encrypted binary program and the key, and finally the original binary number is restored to a text file

Generate random keys:

secrets library is a pseudo-random number module introduced by Python 3.6, which is suitable for generating random keys. The token_bytes function accepts one int parameter that specifies the length of a random string of bytes. int. from_bytes converts the byte string to int, which is the binary number we need.

Encryption unit:

The encrypt function accepts an str object and returns tuples (int, int). With the encode method, we encode the string into a string of bytes. The int. from_bytes function converts a byte string to an int object. Finally, the binary object and random key are XOR operated, and the encrypted text is obtained.

Decryption unit:

decrypt accepts two int objects, encrypted text and a random key. Firstly, the two are operated by XOR, and the number of bits occupied by the decrypted int object is calculated. The decrypted. bit_length function gets the number of bits of a binary number, divided by 8 to get the bit size. In order to prevent 1-7 binary digits from being divided by 8 to get 0, we should add 7, and then divide by 8. The decrypted int object is converted to an bytes object using the int. to_bytes function. Finally, the byte string is converted into a string by decode method.

Using the above functions, we can easily encrypt and decrypt text files.

Encrypted text file

path is the address of the file to be encrypted. If the key address is not specified, a new directory and file will be created under this directory.

Decrypt a file

After encrypting and decrypting files, the decrypted files obtained are the same as the original files


Related articles: