''' pip install cryptography ''' from cryptography.hazmat.primitives.ciphers import Cipher, algorithms, modes from cryptography.hazmat.primitives import padding from cryptography.hazmat.backends import default_backend import binascii aes_key = b'libcckeylibcckey' aes_iv = b'libcciv libcciv ' # 加密 def encrypt(string): backend = default_backend() cipher = Cipher(algorithms.AES(aes_key), modes.CBC(aes_iv), backend=backend) encryptor = cipher.encryptor() padder = padding.PKCS7(algorithms.AES.block_size).padder() padded_data = padder.update(string.encode()) + padder.finalize() result = encryptor.update(padded_data) + encryptor.finalize() return binascii.hexlify(result).upper().decode() # 解密 def decrypt(upper_string): backend = default_backend() cipher = Cipher(algorithms.AES(aes_key), modes.CBC(aes_iv), backend=backend) decryptor = cipher.decryptor() encrypted_data = binascii.unhexlify(upper_string.lower()) decrypted_padded_data = decryptor.update(encrypted_data) + decryptor.finalize() unpadder = padding.PKCS7(algorithms.AES.block_size).unpadder() try: data = unpadder.update(decrypted_padded_data) + unpadder.finalize() except ValueError as e: print("Decryption error: Invalid padding bytes.") raise e return data.decode() if __name__ == '__main__': print(decrypt('导出的密文'))
标签:__,aes,AES,解密,navicat,密码,algorithms,data,backend From: https://www.cnblogs.com/vwvw/p/18655048