[英]Python AES encryption
我試圖得到一個python程序解密一些Base64編碼,使用AES-128在ECB模式下加密,文本。
所以,我正在使用這個教程: http : //docs.python-guide.org/en/latest/scenarios/crypto/開始使用。
它包含以下代碼:
from Crypto.Cipher import AES
# Encryption
encryption_suite = AES.new('This is a key123', AES.MODE_CBC, 'This is an IV456')
cipher_text = encryption_suite.encrypt("A really secret message. Not for prying eyes.")
# Decryption
decryption_suite = AES.new('This is a key123', AES.MODE_CBC, 'This is an IV456')
plain_text = decryption_suite.decrypt(cipher_text)
我已將代碼復制到aes_2.py文件中。 而且,我使用它運行它: sudo python3 aes_2.py
我明白了:
Traceback (most recent call last):
File "aes_2.py", line 21, in <module>
cipher_text = encryption_suite.encrypt("A really secret message. Not for prying eyes.")
File "/usr/local/lib/python3.5/dist-packages/Crypto/Cipher/blockalgo.py", line 244, in encrypt
return self._cipher.encrypt(plaintext)
ValueError: Input strings must be a multiple of 16 in length
編輯1
我有一個文件,我被告知要解密。 我得到了一個密鑰和文件以及解密的一些規范。 這個網站解密它: http : //aesencryption.net/當我輸入密鑰,128位,並將文本輸入網站。
對於上面的代碼。 我有幾個問題。 我應該為'This is an IV456'
什么?如何在此代碼中指定它的位級別?
您正在使用AES.MODE_CBC
,這意味着您的輸入字符串即'This is a key123'
必須是16個字節的倍數。
如果您想繼續使用此模式,則需要填充字符串。 這個git repo是在CBC模式下使用填充的AES加密的一個很好的例子。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.