如何解密RC2密文?
How to decrypt a RC2 ciphertext?
Python 3.5,pycrypto 2.7a1,Windows,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 文件中,所以我才寻求帮助。
IV为非密值,一般写在密文前面。因为,您已经完成了,您不需要编写额外的 IV 文件。 RC2 的块大小为 64 位,因此 IV 始终为 8 字节长。
with open('ciphertext.bin', 'rb') as f:
ciphertext = f.read()
with open('rc2key.bin', 'rb') as f:
key = f.read()
iv = ciphertext[:ARC2.block_size]
ciphertext = ciphertext[ARC2.block_size:]
cipher = ARC2.new(key, ARC2.MODE_CFB, iv)
text = cipher.decrypt(ciphertext).decode("utf-8")
with open('plaintext.txt', 'w') as f:
f.write(text)
print(text)
其他问题:
不要简单地解码二进制数据,例如密文、密钥或 IV,因为它们很可能无法打印。
如果您正在做不同的事情,请不要重复使用相同的 cipher
对象。解密需要一个新初始化的 ARC2
对象。
Python 3.5,pycrypto 2.7a1,Windows,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 文件中,所以我才寻求帮助。
IV为非密值,一般写在密文前面。因为,您已经完成了,您不需要编写额外的 IV 文件。 RC2 的块大小为 64 位,因此 IV 始终为 8 字节长。
with open('ciphertext.bin', 'rb') as f:
ciphertext = f.read()
with open('rc2key.bin', 'rb') as f:
key = f.read()
iv = ciphertext[:ARC2.block_size]
ciphertext = ciphertext[ARC2.block_size:]
cipher = ARC2.new(key, ARC2.MODE_CFB, iv)
text = cipher.decrypt(ciphertext).decode("utf-8")
with open('plaintext.txt', 'w') as f:
f.write(text)
print(text)
其他问题:
不要简单地解码二进制数据,例如密文、密钥或 IV,因为它们很可能无法打印。
如果您正在做不同的事情,请不要重复使用相同的
cipher
对象。解密需要一个新初始化的ARC2
对象。