Practical encryption module of python

Note 1: MD5, SHA1, SHA256, SHA512 encryption The encryption of these hash algorithms is supported in the built-in module hashlib of python.This part o...
Note 1: MD5, SHA1, SHA256, SHA512 encryption

The encryption of these hash algorithms is supported in the built-in module hashlib of python.
This part of this module mainly refers to Liao Xuefeng's Python 3 tutorial Write, you can learn more according to the tutorial.

Note 2: About AES encryption

AES encryption uses the third-party module pycryptodome.

Module installation command: pip install pycryptodome

There are several modes of AES. This module lists three modes: ECB, CFB and CBC. It is said that CBC mode is recognized as the best mode of security. As for their encryption principles, I have limited energy and have not studied them in depth. Let's learn about them by ourselves.

This part of this module mainly refers to Python 3 AES encryption This article is written.

#!/usr/bin/env python3 # -*- coding: utf-8 -*- import hashlib import base64 from Crypto.Cipher import AES ############################################## the_salt = "my_salt" the_key = "my_key" ###################################################### class HashManager(): #######MD5 encryption####### def get_md5(self,the_string): the_string_with_salt =the_string + the_salt the_md5 = hashlib.md5() the_md5.update(the_string_with_salt.encode('utf-8')) the_string_md5 = the_md5.hexdigest() return the_string_md5 #######SHA1 encryption####### def get_sha1(self, the_string): the_string_with_salt =the_string + the_salt the_sha1 = hashlib.sha1() the_sha1.update(the_string_with_salt.encode('utf-8')) the_string_sha1 = the_sha1.hexdigest() return the_string_sha1 #######SHA256 encryption####### def get_sha256(self, the_string): the_string_with_salt =the_string + the_salt the_sha256 = hashlib.sha256() the_sha256.update(the_string_with_salt.encode('utf-8')) the_string_sha1 = the_sha256.hexdigest() return the_string_sha1 #######SHA512 encryption####### def get_sha512(self, the_string): the_string_with_salt =the_string + the_salt the_sha512 = hashlib.sha512() the_sha512.update(the_string_with_salt.encode('utf-8')) the_string_sha1 = the_sha512.hexdigest() return the_string_sha1 #######AES Encryption, ECB Pattern####### def get_aes_ecb(self, the_string): aes = AES.new(self.pkcs7padding_tobytes(the_key), AES.MODE_ECB) # Initialize encryptor encrypt_aes = aes.encrypt(self.pkcs7padding_tobytes(the_string)) # aes encryption encrypted_text = str(base64.encodebytes(encrypt_aes), encoding='utf-8') # Convert to string form with base64 return encrypted_text #######AES Decrypt, ECB Pattern####### def back_aes_ecb(self, the_string): aes = AES.new(self.pkcs7padding_tobytes(the_key), AES.MODE_ECB) # Initialize encryptor decrypted_base64 = base64.decodebytes(the_string.encode(encoding='utf-8')) # Reverse decryption base64 into bytes decrypted_text = str(aes.decrypt(decrypted_base64), encoding='utf-8') # Perform decryption and transcoding to return str decrypted_text_last = self.pkcs7unpadding(decrypted_text) # Remove filling treatment return decrypted_text_last #######AES Encryption, CFB Pattern####### def get_aes_cfb(self, the_string): key_bytes = self.pkcs7padding_tobytes(the_key) iv = key_bytes aes = AES.new(key_bytes, AES.MODE_CFB, iv) # Initialize encryptor, key,iv use the same encrypt_aes = iv + aes.encrypt(the_string.encode()) # aes encryption encrypted_text = str(base64.encodebytes(encrypt_aes), encoding='utf-8') # Convert to string form with base64 return encrypted_text #######AES Decrypt, CFB Pattern####### def back_aes_cfb(self, the_string): key_bytes = self.pkcs7padding_tobytes(the_key) iv = key_bytes aes = AES.new(key_bytes, AES.MODE_CFB, iv) # Initialize encryptor, key,iv use the same decrypted_base64 = base64.decodebytes(the_string.encode(encoding='utf-8')) # Reverse decryption base64 into bytes decrypted_text = str(aes.decrypt(decrypted_base64[16:]), encoding='utf-8') # Perform decryption and transcoding to return str return decrypted_text #######AES Encryption, CBC Pattern####### def get_aes_cbc(self, the_string): key_bytes = self.pkcs7padding_tobytes(the_key) iv = key_bytes aes = AES.new(key_bytes, AES.MODE_CBC, iv) # Initialize encryptor, key,iv use the same encrypt_bytes = aes.encrypt(self.pkcs7padding_tobytes(the_string)) # aes encryption encrypted_text = str(base64.b64encode(encrypt_bytes), encoding='utf-8') # Convert to string form with base64 return encrypted_text #######AES Decrypt, CBC Pattern####### def back_aes_cbc(self, the_string): key_bytes = self.pkcs7padding_tobytes(the_key) iv = key_bytes aes = AES.new(key_bytes, AES.MODE_CBC, iv) # Initialize encryptor, key,iv use the same decrypted_base64 = base64.b64decode(the_string) # Reverse decryption base64 into bytes decrypted_text = str(aes.decrypt(decrypted_base64), encoding='utf-8') # Perform decryption and transcoding to return str decrypted_text_last = self.pkcs7unpadding(decrypted_text) # Remove filling treatment return decrypted_text_last #######Fill correlation function####### def pkcs7padding_tobytes(self, text): return bytes(self.pkcs7padding(text), encoding='utf-8') def pkcs7padding(self,text): bs = AES.block_size ####tips: utf-8 1 in English when coding byte,Chinese accounts for 3 byte#### length = len(text) bytes_length = len(bytes(text, encoding='utf-8')) padding_size = length if (bytes_length == length) else bytes_length #################################################### padding = bs - padding_size % bs padding_text = chr(padding) * padding # tips: chr(padding) look at the conventions with other languages. Some of them will use '\ 0' return text + padding_text def pkcs7unpadding(self,text): length = len(text) unpadding = ord(text[length - 1]) return text[0:length - unpadding]

If this article is helpful, please leave a message for encouragement.
If there is any mistake in this article, please leave a message for improvement.

2 December 2019, 13:07 | Views: 8356

Add new comment

For adding a comment, please log in
or create account

0 comments