2017-02-09 96 views
1

我想獲得一個Python程序,解密一些Base64編碼,在ECB模式下使用AES-128加密文本。Python的AES加密

所以,我正在使用本教程: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'寫些什麼,以及如何指定它在此代碼中的位級別?

+0

@阿曼你是對的,我添加了幾個字符,它跑了。奇怪的是,我根本沒有改變教程。 – Rorschach

回答

0

您正在使用AES.MODE_CBC這意味着您的輸入字符串,即'This is a key123'必須是16字節的倍數。

如果你想繼續使用這種模式,那麼你將需要填充你的字符串。這個git repo是在CBC模式下使用填充進行AES加密的一個很好的例子。