一、Pythonaes解密
Python 的 AES 模塊是一個對稱加密技術,目前是廣泛應用於信息加密保護的一種技術方案,同時也是一波加密的主流方案之一。
AES 在 Python 中是通過 Crypto 庫提供支持的。而在許多場景下,Python AES 解密也是非常必要的,因為在一些業務場景下,數據可能會被加密後存儲或傳輸,需要使用 Python 代碼進行解密。
為了解決 Python AES 解密的需求,下面我們將介紹如何進行 Python AES 解密。
二、Pythonaes加解密
在進行 Python AES 解密之前,我們需要先進行 Python AES 加密。Python AES 加解密也是通過 Crypto 庫提供支持的。要使用加密,則需要定義一個 AES 對象並指定密鑰和 IV 向量等參數。我們可以使用以下代碼進行加密:
from Crypto.Cipher import AES from Crypto.Random import get_random_bytes def encrypt_aes(key, data): iv = get_random_bytes(16) cipher = AES.new(key, AES.MODE_CBC, iv) ciphertext = iv + cipher.encrypt(data) return ciphertext
加密過程中,定義了 key 和 data 兩個參數,其中 key 表示加密密鑰,data 表示需要加密的數據。加密方式選擇 CBC 模式,並生成隨機的 IV 向量,最終返回密文 ciphertext。
三、Pythonaes解密後有亂碼
由於密文在進行 Python AES 解密之後需要進行解密,我們可以使用以下代碼來進行解密:
def decrypt_aes(key, ciphertext): iv = ciphertext[:16] cipher = AES.new(key, AES.MODE_CBC, iv) plaintext = cipher.decrypt(ciphertext[16:]) return plaintext
在進行解密的時候,同樣需要傳入一個 key 參數,表示 AES 解密的密鑰。此外,還需要傳入 ciphertext 參數,表示待解密的密文。在解密的過程中,先從密文中提取出 IV 向量,然後使用 AES 解密演算法解密出明文 plaintext,並將其返回。
當解密後結果出現亂碼時,有可能是密鑰不正確或解密模式不正確導致的。此時,需要重新檢查解密模式及密鑰是否正確,並進行修正。
四、Python解密文件
在 Python 中,我們也可以對文件進行加解密操作。以下代碼展示了如何使用 Python 進行文件的 AES 加密和解密:
文件 AES 加密:
def encrypt_file_aes(key, in_filename, out_filename, chunksize=64*1024): iv = get_random_bytes(AES.block_size) cipher = AES.new(key, AES.MODE_CBC, iv) with open(in_filename, 'rb') as infile, open(out_filename, 'wb') as outfile: outfile.write(iv) while True: chunk = infile.read(chunksize) if len(chunk) == 0: break elif len(chunk) % AES.block_size != 0: chunk += b' ' * (AES.block_size - len(chunk) % AES.block_size) outfile.write(cipher.encrypt(chunk))
文件 AES 解密:
def decrypt_file_aes(key, in_filename, out_filename, chunksize=24*1024): with open(in_filename, 'rb') as infile: iv = infile.read(AES.block_size) cipher = AES.new(key, AES.MODE_CBC, iv) with open(out_filename, 'wb') as outfile: while True: chunk = infile.read(chunksize) if len(chunk) == 0: break outfile.write(cipher.decrypt(chunk)) outfile.truncate()
五、Python解密密文
在 Python 中我們也可以通過調用加解密庫進行解密密文,如 pyaes 庫、pycryptodome 庫等,以下是使用 pyaes 庫進行解密密文的示例:
import pyaes key = 'this_key_for_demo_purposes_only!' iv = 'this_key_for_demo_iv' aes = pyaes.AESModeOfOperationCBC(key, iv=iv) plaintext = aes.decrypt(ciphertext)
六、Pythonrsa解密
RSA 演算法是非對稱加密演算法中的一種,相比較對稱加密來說,RSA 加密速度較慢,但可以提供更高的安全性。
以下是使用 Python 進行 RSA 解密的示例:
import rsa private_key = rsa.PrivateKey.load_pkcs1(open('private.pem', 'r').read().encode()) cipher = rsa.decrypt(ciphertext, private_key).decode()
七、Python加解密
在進行 Python 加解密操作的時候,我們通常還需要選擇一個適合的加密庫。Python 中有多個加解密庫,如 PyCrypto、pycryptodome、cryptography、pycryptodomex 等。
在選擇加密庫之前,需要根據實際場景來進行選取,比如可移植性、安全性、速度等因素。
以下是使用 pycryptodome 庫進行 AES 加解密的示例:
from Crypto.Cipher import AES from Crypto.Random import get_random_bytes def encrypt_aes(key, data): iv = get_random_bytes(16) cipher = AES.new(key, AES.MODE_CBC, iv) ciphertext = iv + cipher.encrypt(data) return ciphertext def decrypt_aes(key, ciphertext): iv = ciphertext[:16] cipher = AES.new(key, AES.MODE_CBC, iv) plaintext = cipher.decrypt(ciphertext[16:]) return plaintext key = get_random_bytes(16) data = 'This is a test.' ciphertext = encrypt_aes(key, data.encode()) original_data = decrypt_aes(key, ciphertext).decode() print('Original Data: %s' % data) print('Decrypted Data: %s' % original_data)
原創文章,作者:小藍,如若轉載,請註明出處:https://www.506064.com/zh-tw/n/155092.html