2017-02-09 209 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加密的一个很好的例子。