2016-08-20 119 views
-1

我正在编写我的学校项目的程序,并且上面有一个问题。 这里是我的代码:ValueError:AES密钥长度必须为16,24或32字节PyCrypto 2.7a1

def aes(): 
    #aes 
    os.system('cls') 
    print('1. Encrypt') 
    print('2. Decrypt') 

    c = input('Your choice:') 

    if int(c) == 1: 
     #cipher 
     os.system('cls') 
     print("Let's encrypt, alright") 
     print('Input a text to be encrypted') 
     text = input() 

     f = open('plaintext.txt', 'w') 
     f.write(text) 
     f.close() 

     BLOCK_SIZE = 32 
     PADDING = '{' 
     pad = lambda s: s + (BLOCK_SIZE - len(s) % BLOCK_SIZE) * PADDING 
     EncodeAES = lambda c, s: base64.b64encode(c.encrypt(pad(s))) 
     secret = os.urandom(BLOCK_SIZE) 

     f = open('aeskey.txt', 'w') 
     f.write(str(secret)) 
     f.close() 

     f = open('plaintext.txt', 'r') 
     privateInfo = f.read() 
     f.close() 

     cipher = AES.new(secret) 

     encoded = EncodeAES(cipher, privateInfo) 

     f = open('plaintext.txt', 'w') 
     f.write(str(encoded)) 
     f.close() 
     print(str(encoded)) 

    if int(c) == 2: 
     os.system('cls') 
     print("Let's decrypt, alright") 

     f = open('plaintext.txt','r') 
     encryptedString = f.read() 
     f.close() 

     PADDING = '{' 
     DecodeAES = lambda c, e: c.decrypt(base64.b64decode(e)).rstrip(PADDING) 
     encryption = encryptedString 

     f = open('aeskey.txt', 'r') 
     key = f.read() 
     f.close() 

     cipher = AES.new(key) 
     decoded = DecodeAES(cipher, encryption) 

     f = open('plaintext.txt', 'w') 
     f.write(decoded) 
     f.close() 

     print(decoded) 

完整的错误文本:

Traceback (most recent call last): File "C:/Users/vital/Desktop/Prog/Python/Enc_dec/Enc_dec.py", line 341, in aes() 
File "C:/Users/vital/Desktop/Prog/Python/Enc_dec/Enc_dec.py", line 180, in aes cipher = AES.new(key) 
File "C:\Users\vital\AppData\Local\Programs\Python\Python35-32\lib\site-packages\Crypto\Cipher\AES.py", line 179, in new return AESCipher(key, *args, **kwargs) 
File "C:\Users\vital\AppData\Local\Programs\Python\Python35-32\lib\site-packages\Crypto\Cipher\AES.py", line 114, in init blockalgo.BlockAlgo.init(self, _AES, key, *args, **kwargs) 
File "C:\Users\vital\AppData\Local\Programs\Python\Python35-32\lib\site-packages\Crypto\Cipher\blockalgo.py", line 401, in init self._cipher = factory.new(key, *args, **kwargs) 
ValueError: AES key must be either 16, 24, or 32 bytes long 

Process finished with exit code 1 

我在做什么错?

+0

它是3.5,这是pycrypto的版本2.7a1 – Tukanoid

回答

3

错误非常明显。关键必须是这个尺寸。 os.urandom将返回正确的密钥。但是,这个密钥是字节(二进制字符串值)。此外,通过使用str(secret),将repr(secret)的值写入文件而不是secret

更令人困惑的是,AES.new允许您将密钥作为Unicode传递!但是,假设密钥是ASCII字节1234123412341234。现在,

f.write(str(secret)) 

会将b'1234123412341234'写入文本文件!它现在包含那些16字节+ b和两个'引用字符,而不是16字节;总共19个字节。

或者,如果你从os.urandom采取随机二进制字符串,

>>> os.urandom(16) 
b'\xd7\x82K^\x7fe[\x9e\x96\xcb9\xbf\xa0\xd9s\xcb' 

现在,而不是写16个字节D782,..等等,现在写道,串到文件中。而且由于解密尝试使用

"b'\\xd7\\x82K^\\x7fe[\\x9e\\x96\\xcb9\\xbf\\xa0\\xd9s\\xcb'" 

作为解密密钥,其中,当编码为UTF-8导致

b"b'\\xd7\\x82K^\\x7fe[\\x9e\\x96\\xcb9\\xbf\\xa0\\xd9s\\xcb'" 

这是一个49字节长bytes值发生错误。


您有2个不错的选择。要么继续将密钥写入文本文件,而要将其转换为十六进制,或将密钥写入二进制文件;那么文件应该是字节中的密钥长度。我打算在这里后者:

因此用于存储密钥,使用

with open('aeskey.bin', 'wb') as keyfile: 
     keyfile.write(secret) 

with open('aeskey.bin', 'rb') as keyfile: 
     key = keyfile.read() 

同自然适用于密文(即加密二进制),您必须在二进制文件中写入和读取它:

with open('ciphertext.bin', 'wb') as f: 
     f.write(encoded) 

with open('ciphertext.bin', 'rb') as f: 
     encryptedString = f.read() 

如果你想为base64编码,千万注意base64.b64encode/decodebytes -in/bytes退房手续。

顺便说一句,plaintext是原始的,未加密的文本;加密的文本被称为ciphertext。 AES是一种密码,可以将明文加密为密文,并使用密钥将密文解密为明文。

尽管被称为“文本”,但它们都不是文本数据本身,正如Python所理解的那样,但它们是二进制数据,应该表示为bytes

+0

遗憾,但现在我有新的错误: ValueError异常:输入的字符串必须是线16的长度 – Tukanoid

+0

多: 解码= DecodeAES(密码,加密) DecodeAES = lambda c,e:c.decrypt(base64.b64decode(e))。rstrip(PADDING) – Tukanoid

+1

@ВиталикЛукьянов同样适用于您的密文!以二进制格式打开密文文件,并向其写入二进制文件。它不是“明文”,也许不应该写入名为“plaintext.txt”的文件。 –

相关问题