Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/364.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 将hex()转换为pycryptodome密文格式_Python_Hash_Decode_Pycryptodome - Fatal编程技术网

Python 将hex()转换为pycryptodome密文格式

Python 将hex()转换为pycryptodome密文格式,python,hash,decode,pycryptodome,Python,Hash,Decode,Pycryptodome,Im使用PKCS1_OAEP加密存储在本地文件中的字符串, 接下来,我将密文转换为十六进制格式,并将这个str写入一个新的.txt文件 在另一个file.py中,我需要读取新的.txt文件并解密文件的所有哈希值,问题是我找不到将ciphertext.hex()格式恢复为ciphertext格式的表单 有什么建议吗 这是我的第二个file.py的代码 from Crypto.Cipher import PKCS1_OAEP from Crypto.PublicKey import RSA new

Im使用PKCS1_OAEP加密存储在本地文件中的字符串, 接下来,我将密文转换为十六进制格式,并将这个str写入一个新的.txt文件

在另一个file.py中,我需要读取新的.txt文件并解密文件的所有哈希值,问题是我找不到将ciphertext.hex()格式恢复为ciphertext格式的表单

有什么建议吗

这是我的第二个file.py的代码

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()

PS:对不起,我的英语是

您应该使用
bytes.fromhex(一些十六进制字符串)
从十六进制字符串中恢复字节,如下所示:

        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()
message = cipher.decrypt(bytes.fromhex(hash_line))