Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use Python to realize Software encryption function

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

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!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report