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

Im using PKCS1_OAEP to encrypt strings stored in a local file, next i convert the ciphertext to hex format and writing this str in a new .txt file.

In another file.py i need to read the new .txt file and decrypt all hashes of file, the problem is i can't find the form to comeback of ciphertext.hex() format to ciphertext format.

any suggestions?

here is my code of the second file.py

        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: sorry for my english

Rhaigtz
  • 3
  • 1

1 Answers1

0

You should use bytes.fromhex(some_hex_string) to recover bytes from hexadecimal strings, as follows:

message = cipher.decrypt(bytes.fromhex(hash_line))
SITS
  • 16