im encrypting message using rsa when i convert the 256 length cipher text to binary using:
binary_cipher = str(bin(int.from_bytes(cipher_text,byteorder='big'))[2:].zfill(2048))
i get 2048 sized binary string. when i try to convert this binary back to a cipher text using:
encrypted_message = hex(int(binary_cipher,2)).lstrip('0x')
i get a 512 sized cipher text and thus the rsa decryption isnt accepting it because its not the excpected key size (as i understood)
so long story short why my cipher _text's length is getting increased to 512 when i convert it back?
CodePudding user response:
cipher_text = bytes("PREM")
binary_cipher = str(bin(int.from_bytes(cipher_text,byteorder='big'))[2:].zfill(2048))
encrypted_message = hex(int(binary_cipher,2)).lstrip('0x')
print(cipher_text)
print(binary_cipher)
print(encrypted_message)
Here I get "5052454d" which is "PREM" in Hex ASCII.
Working Correctly because Hex is 2 Digits Per ASCII Character.
256 Characters are getting converted to 512 Hex Digits.
To get the ASCII back , try this :
B=bytearray.fromhex(encrypted_message)
B.decode()
Here I get "PREM"