Get the App
SLTechnology News&Howtos  ›  Development  › 

How to use Python to realize Software encryption function

Shulou Source: shulou.com Published: 2022-06-03 07:55:22 10月12日 Update

This article mainly shows you "how to use Python to achieve software encryption", the content is easy to understand, clear, hope to help you solve your doubts, the following let the editor lead you to study and learn "how to use Python to achieve software encryption" this article.

Basic knowledge

In Python, the XOR operator is: ^, which can also be recorded as XOR. Bitwise XOR means that the same value is XOR 0 and different value XOR is 1. Specifically, there are four possibilities: 0 ^ 0 = 0 ^ 1 = 1, 1 ^ 0 = 1, 1 ^ 1 = 0. We can also sum up the law (An is 0 or 1): 0 and A XOR is An itself; 1 and A XOR is An inverse.

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

The XOR value of a binary number to itself is 0

B ^ b = 0

XOR operation satisfies commutative law

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

The XOR of 0 and an 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 operations, the principle of encryption is very clear.

Encryption operation:

First, the file is converted into a binary number, then a random key as long as the binary number is generated, and the binary number is XOR with the key to get 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 a random key:

Secrets library is a pseudorandom number module introduced by Python 3.6.It is suitable for generating random keys. The token_bytes function takes an int argument that specifies the length of the random byte string. Int.from_bytes converts byte strings to int, which is the binary number we need.

Encryption unit:

The encrypt function takes a str object and returns a tuple (int, int). Using the encode method, we encode the string into a byte string. The int.from_bytes function converts a byte string into an int object. Finally, the encrypted text is obtained by XOR operation on the binary object and random key.

Decryption unit:

Decrypt accepts two int objects, encrypted text and a random key. First of all, the XOR operation is performed on the two to calculate the number of bits occupied by the decrypted int object. The decrypted.bit_length function gets the number of digits of the binary number, which can be obtained by dividing it by 8. In order to prevent, 1-7 bits of binary number divides 8 to get 0, so add 7, and then carry on the operation of dividing 8. Use the int.to_bytes function to convert the decrypted int object into a bytes object. Finally, the byte string is converted into a string by the decode method.

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

Encrypted text file

Path is the address of the file to be encrypted, and if you do not specify the key address, create a new directory and file under that directory.

Decrypt a file

After encrypting and decrypting the file, the decrypted file is the same as the original file.

The above is all the contents of the article "how to use Python to achieve software encryption". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!

Tags: Encryption binary file key object function byte text function software software encryption content property article generation same unit principle address character Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Huawei MariaDB macOS Redmi Shulou Tech Info