0

I have a little hard time understanding the RSACryptoServiceProvider class... I'm supposed to encrypt a message of length 256 bits, with a key , which is also 256 bits long. Shouldn't the output of also be 256 bits long? Here's my code:

//key generation

byte[] bytes = new byte[32];
var rng = new RNGCryptoServiceProvider();
rng.GetBytes(bytes);
k2 = bytes;

//encryption function

static public byte[] Encryption(byte[] Data, RSAParameters RSAKey, bool DoOAEPPadding)
{  
    byte[] encryptedData;
    using (RSACryptoServiceProvider RSA = new RSACryptoServiceProvider())
    {
        RSA.ImportParameters(RSAKey);
        encryptedData = RSA.Encrypt(Data, DoOAEPPadding);
    }
    return encryptedData;
}

And then finally calculating

ciphertext = Encryption(k2, RSA.ExportParameters(false), false);

produces a byte[128] ciphertext aka 1024 bits. Shouldn't I get ciphertext of size byte[32]?

1
  • Maybe you should name your variables after your functions... Commented Oct 15, 2014 at 22:47

1 Answer 1

1

It seems that you use the key, k2, as data for RSA encryption. That's OK if you want e.g. to wrap a 256 bit AES key using RSA. But your RSA key is the second parameter, not the first.

The data in k2 is then padded (according to the older PKCS#1 v1.5 scheme), after which modular exponentiation will be performed using the public exponent and modulus of the RSA key. The modulus of the RSA key determines the key size. This modulus exponentiation will always produce a result between zero and modulus - 1. However, the result is always left-padded to the key size in bytes (with a function called I2OSP).

So it seems your result is 1024 bits, which means that your RSA key pair is also 1024 bits.

Sign up to request clarification or add additional context in comments.

3 Comments

Edited my answer to comply with the changes in the question. Give a shout if anything is unclear.
Do you have in mind any way of computing the RSA of k2 with key k1, that is: E_k1 (k2), using RSACryptoServiceProvider?
What's k1? If it is RSAKey then you're already doing it. An RSA public key consists of the public exponent and modulus. It should never be just 32 bytes in size and it will always produce a ciphertext of the size of the modulus, at least 11 bytes larger than the plaintext (if padding is applied).

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.