Some Encryption Methods of python and python Encryption Module
- 2021-07-13 05:54:47
The built-in base64 module of Python can encode and decode base64, base32, base16, base85 and urlsafe_base64. python 3. x usually has binary input and output, and 2. x can be string.
base64 encoding and decoding of base64 module calls binascii module, b2a_base64 () function in binascii module is used for base64 encoding, and a2b_base64 () function in binascii module is used for base64 decoding.
import base64 s = 'hello, world' base64.b64encode(bytes(s, 'ascii')) b'aGVsbG8sIHdvcmxk' base64.b64decode(base64.b64encode(bytes(s, 'ascii'))) b'hello, world'
There is md5 module in Python2.x, which calls hashlib module, python3.x has removed md5, and md5 is directly carried out by calling hashlib module. Python2. x can use unicode characters directly, but 3. x must use binary byte strings.
import hashlib m = hashlib.md5() m.update(b'hello, world!') m.hexdigest() '3adbbad1791fbae3ec908894c4963870'
Similar to MD5
import hashlib m = hashlib.sha1() m.update(b'hello, world!') m.hexdigest() '1f09d30c707d53f3d16c530dd73d70a6ce7596a9'
To calculate the crc32 check value of the specified content, you can use zlib and binascii module crc32 function.
import zlib import binascii s = b'hello, world!' zlib.crc32(s) 1486392595 binascii.crc32(s) 1486392595
ps: Let's take a look at the encryption module of Python
Encryption mode is md5 encryption as an example
It is a standard module and can be imported directly
There are other encryption methods, such as. sha1 (),. sha224 (),. sha256 (), etc. The encrypted string is longer and more secure
1. Encryption steps
1. The string is converted to binary type first
Convert to binary type using. encode () method
import hashlib password='123456' print(password.encode())# String cannot be encrypted directly, so it must be converted to 2 Only those of binary type can be encrypted # The results are: b'123456'
2. Use encryption methods
Output encrypted content using the. hexdigest () method
import hashlib password='123456' m=hashlib.md5(password.encode()) # Turn to first 2 Binary, re-encrypted #print(dir(m)) # Because of this cryptographic module pycharm You can't provide prompt methods, so if you want to know what methods are available, you can use built-in functions dir() You can print out all the methods using this statement print(m.hexdigest())
3. Print the encrypted content
Use the dir () built-in function to display all methods of this variable
Because the module pycharm can't provide a prompt method, that is, typing m "." won't produce a method, you can use the built-in function dir () to print out all the methods.
#print(dir(m)) # Because of this cryptographic module pycharm You can't provide prompt methods, so if you want to know what methods are available, you can use built-in functions dir() You can print out all the methods using this statement print(m.hexdigest())
1. Check the encryption results
Because the string encrypted by md5 is 32 bits no matter how long the original string is, you can use len () method to verify whether the encryption is successful:
2. md5 is irreversible after encryption
The principle that can be decrypted on the network is to hit the database, that is, encrypt the commonly used passwords and store them in the database, and then search the database directly.
If the encrypted password you entered happens to be in the database, you can return the corresponding encrypted password to you, which is not really decrypted.
3. Other encryption methods
The encryption method in this paper takes md5 as an example, and there are other encryption methods, such as:. sha1 (),. sha224 (),. sha256 (), etc. The encrypted string length is longer and the security is higher.
m=hashlib.sha1(password.encode())# Different encryption methods m=hashlib.sha224(password.encode()) # Different encryption methods m=hashlib.sha256(password.encode()) # Different encryption methods
Step 3 Add salt
If you want to encrypt password=123456, this plaintext password is too simple to crash into the library and decrypt it easily, so add a string randomly after 123456, such as abc, which actually encrypts 123456abc. If you don't know the string abc, the possibility of reverse solution is reduced and the security is higher. Such a string is called salt.
4. Practical application
# Implement encryption: incoming 1 Value, if there is salt value, add salt to encrypt; Otherwise encrypt directly def my_md5(s:str,salt=None): s=str(s) if salt: s=s+salt m=hashlib.md5(s.encode()) return m.hexdigest()