0

Hello i'm trying to port some RSA encryption code from a C# application to python using python https://pycryptodome.readthedocs.io/

But my problem is i'm stuck on this line

using System.Security.Cryptography;
...
 
result = RSA.SignHash(testData, CryptoConfig.MapNameToOID("SHA256"));

using python i'm trying this

with open('key.pem', 'r') as f:
    private_key = RSA.import_key(f.read())
    h = SHA256.new(bytes(testData))
    signature = pkcs1_15.new(private_key).sign(h)

I tried to make a unit test comparing the two functions using an example byte array

testData = bytes(b'\x5b\xd4\x08\xae\x4f\x80\x5d\x77\xc4\x4e\xc8\x23\x63\x82\x79\x17\xad\x75\x3a\x2b\x2f\xa6\x80\xb1\x31\x74\x19\x8c\x29\x80\x38\x49')

But the resulting output(result & signature variable) with length of 128 is not the same.

What am I missing?

1

1 Answer 1

0

Solved my problem by switching pythonlibrary from "python cryptodome" to "rsa" https://stuvel.eu/python-rsa-doc/usage.html#signing-and-verification

The interesting code from above link:

(1)message = 'Go left at the blue tree'.encode()
(2)hash = rsa.compute_hash(message, 'SHA-1')
(3)signature = rsa.sign_hash(hash, privkey, 'SHA-1')

This library had splitted the "sign" functon into two parts so I basically skipped doing step (2) and it worked for me.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.