我正在为我的学校项目编写程序,上面有一个问题。下面是我的代码:
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:UsersvitalAppDataLocalProgramsPythonPython35-32libsite-packagesCryptoCipherAES.py", line 179, in new return AESCipher(key, *args, **kwargs)
File "C:UsersvitalAppDataLocalProgramsPythonPython35-32libsite-packagesCryptoCipherAES.py", line 114, in init blockalgo.BlockAlgo.init(self, _AES, key, *args, **kwargs)
File "C:UsersvitalAppDataLocalProgramsPythonPython35-32libsite-packagesCryptoCipherblockalgo.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
我做错了什么?
错误非常明显。钥匙必须正好是这个尺寸。os.urandom
将返回正确的密钥。然而,这个键是一个字节(二进制字符串值)。此外,通过使用str(secret)
,将repr(secret)
的值写入文件中,而不是secret
。
更令人困惑的是AES.new
允许您将密钥作为Unicode传递!但是,假设密钥是ASCII字节1234123412341234
。现在,
f.write(str(secret))
将把b'1234123412341234'
写入文本文件!而不是16个字节,它现在包含这16个字节+ b
,和两个'
引号字符;总共19个字节
或者从os.urandom
中取一个随机的二进制字符串,
>>> os.urandom(16)
b'xd7x82K^x7fe[x9ex96xcb9xbfxa0xd9sxcb'
现在,而不是写入16字节D7
, 82
,…以此类推,它现在把那个字符串写入文件。出现错误是因为解密试图使用
"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
值。
你有两个很好的选择。要么继续将密钥写入文本文件,但将其转换为十六进制,要么将密钥写入二进制文件;那么该文件应该正好是密钥长度(以字节为单位)。我选择后者:
对于存储键,使用
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/decode
是bytes
-in/bytes
-out。
尽管它们被称为"-text",但它们本身都不是Python所理解的文本数据,但它们是二进制数据,应该表示为bytes
。