How to fix error generated from RSA_public_decrypt function

871 views Asked by At

I have developed in C under Linux that send a random number to remote pc, then the remote pc read the number and encrypt it with openssl private key (with C# windows application), after that it send, the c program try to decrypt it with its openssl public key as the following:

int padding = RSA_PKCS1_PADDING;
RSA_public_decrypt(data_len,enc_data,decrypted,rsa,padding);

c#program is developped like this:

CryptoKey d = CryptoKey.FromPrivateKey(privateKeyAsPem, null);
RSA rsa = d.GetRSA();
byte[] result = rsa.PrivateEncrypt(code, RSA.Padding.PKCS1);
rsa.Dispose();

The problem is I got a random behavior sometimes the C program decrypt without error and sometimes I got the following error:

error:04067072:rsa routines:RSA_EAY_PUBLIC_DECRYPT:padding check failed

How to fix this problem?

NOTE:

Keys are generated under Linux like this:

openssl genpkey -algorithm RSA -out priv.pem -pkeyopt rsa_keygen_bits:2048
openssl rsa -pubout -in priv.pem -out pub.pem
0

There are 0 answers