0

Following Topaco's answer from previous post : Trouble loading RSA public key from file

I've been reproducing the same using python :

# Modulus, Exponent
n = 0xC4F75716EC835D2325689F91FF85ED9BFC3211DB9C164F41852E264E569D2802008054A0EF459E7E3EABB87FAE576E735434D1D124B30B11BD6DE09814860155
e = 0x11

# Data to encrypt (m > n instead of m < n)
buf = "c8cd2174b98433b93094b36026de125a7f5ed85ec27ee6bb9e996cb3b938e9c6238cc65d3615fb635f6f080f6dda06315928bcae4ccf802f9680547db57b8283"

# convert buffer to bytes array then convert bytes to an unique big-endian integer
bytesFromBuf = bytearray.fromhex(buf)
m = int.from_bytes(bytesFromBuf, 'big')

# Perform Encryption
def RSAEncryption(m, e, n):
    return pow(m, e, n)

# encrypt message
c = RSAEncryption(m, e, n)

# Print hex value
print(hex(c)[2:])

And output is

7bc72fa0a4301587b8a3b16b271f8e7d235625e54778a2df37f3512a02430f2ed2ca2a533ade6eb65c69eeb9b324d47a97679297603265583899b37953395512

Which is correct.

So, why am I able to encrypt the data using python and not c++ using openssl library?

tortank
  • 23
  • 3
  • Regarding this question, openssl library seems restricting encryption when n < m unlike python or cryptopp that I just tried and probably gmp. – tortank Apr 03 '20 at 00:42
  • 1
    You aren't using a python RSA library, you're just doing the underlying modular arithmetic yourself so there is nothing to stop you from doing nonsensical things. The openssl api you are using is for RSA, so it *does* stop you from doing something silly like encrypting a value greater than the modulus. If you want to compare apples to apples then use the openssl BN api. – President James K. Polk Apr 04 '20 at 16:16
  • The other 2 comments point out the problem. The easy fix - with your code - is use a bigger key. – Legorooj Apr 05 '20 at 09:06

0 Answers0