3
votes

I'm having some issues using the .Net wrapper of the OpenSSL libraray for computing the RSA Private Key Encryption of a set of bytes.

Currently I read in a .pem file containing the private key I want to use into a BIO object.

public byte[] ComputeRSAEncryption(byte[] dataBlock, BIO privateKey)
{
   RSA rsa = RSA.FromPrivateKey(privateKey);
   return rsa.PrivateEncryption(dataBlock, RSA.Padding.None);
}

I'm using an RSA key of size 64 bytes and a datablock size of 64 bytes. When the above method is called I get the error:

Data too large for modulus

However if I use a datablock of size 64 bytes where all bytes are set to 0x00 the method works without error.

Is there something I'm missing?

Thanks.

1
Just because the modulus is 64 bytes does not mean your data can be 64 bytes. The modulus is actually an integer and the data as an integer must be less then the modulus. PKCS#1 padding always forces the high-order byte of the data to be zero to ensure this condition.President James K. Polk

1 Answers

4
votes

I've finally resolved the issue. For anyone else thats interested the Least Significant byte of the datablock must be of value 0x00. By enforcing this condition RSA encryption and decryption works like a charm.