问题描述
from Crypto.Cipher import PKCS1_OAEP
from Crypto.PublicKey import RSA
new_file = open("md5_rehashed.txt","w")
file_with_hashes = open('md5_hashed.txt','r')
key = RSA.importKey(open(received_message.decode('utf-8')).read())
cipher = PKCS1_OAEP.new(key)
for hash in file_with_hashes:
message = bytes(hash,'utf-8')
ciphertext = cipher.encrypt(message)
new_file.write(ciphertext.hex().upper()+'\n')
file_with_hashes.close()
new_file.close()
我使用PKCS1_OAEP加密存储在本地文件中的字符串, 接下来,我将密文转换为十六进制格式,并将此str写入新的.txt文件中。
在另一个file.py中,我需要读取新的.txt文件并解密该文件的所有哈希,问题是我找不到将ciphertext.hex()格式恢复为密文格式的格式。
有什么建议吗?
这是我第二个文件的代码。
hashed_file = open(hashed_file_route,'r')
unhashed_file = open('md5_unhashed','w')
key = RSA.importKey(open('private.pem').read())
cipher = PKCS1_OAEP.new(key)
for hash_line in hashed_file:
print(hash_line)
print(codecs.encode(bytes(hash_line,'utf-8'),'hex_codec'))
message = cipher.decrypt(hash_line.encode('utf-8'))
unhashed_file.write(message+ '\n')
unhashed_file.close()
hashed_file.close()
PS:对不起,我的英语
解决方法
您应使用bytes.fromhex(some_hex_string)
从十六进制字符串中恢复字节,如下所示:
message = cipher.decrypt(bytes.fromhex(hash_line))