-1
的Python 3.5,pycrypto 2.7a1,Windows中,RC2加密如何解密RC2密文?
例子:
print('Введите текс, который хотите зашифровать:')
text = input()
with open('plaintext.txt', 'w') as f:
f.write(text)
key = os.urandom(32)
with open('rc2key.bin', 'wb') as keyfile:
keyfile.write(key)
iv = Random.new().read(ARC2.block_size)
cipher = ARC2.new(key, ARC2.MODE_CFB, iv)
ciphertext = iv + cipher.encrypt(bytes(text, "utf-8"))
with open('iv.bin', 'wb') as f:
f.write(iv)
with open('ciphertext.bin', 'wb') as f:
f.write(ciphertext)
print(ciphertext.decode("cp1251"))
而且我想知道我怎么能解密这个文本,我试过了,但不能这样做。
我尝试解密:
os.system('cls')
print('Дешифруем значит')
with open('ciphertext.bin', 'rb') as f:
ciphertext = f.read()
with open('rc2key.bin', 'rb') as f:
key = f.read()
with open('iv.bin', 'rb') as f:
iv = f.read()
ciphertext = ciphertext.decode('cp1251')
iv = iv.decode('cp1251')
text = ciphertext.replace(iv, '')
text = cipher.decrypt(text)
with open('plaintext.txt', 'w') as f:
f.write(text)
print(text.decode("ascii"))
但我明白,我需要密码变量,我不能将它保存为.txt或.bin文件,以便为什么我寻求帮助。
我只能在这里看到加密代码。请显示[最小,完整和可验证示例](/ help/mcve),包括您遇到的错误。请注意,您需要在解密期间使用密钥和IV的*完全相同的字节。 –
@ArtjomB。编辑 – Tukanoid