Heshan Perera
Heshan Perera

Reputation: 4630

Why does Bouncy Castle RSAEngine.processblock method always returns 255 bytes when decrypting?

I am experiment with RSA using bouncy castle. I know this is the other way around to the general convention but to my understanding, it still should work theoretically.

I encrypt some data using an RSA private key. The length of the data being encrypted is 294 bytes. The encryption function outputs 512 bytes. I then call the decryption method by passing the above output cipher text and the corresponding public key. My problem is that the decryption always returns a buffer of 255 bytes whereas the actual input to the Encryption function was 294 bytes. What could be the reason for this ?

The following is the source code of the encryption and decryption functions.

public static byte[] RSAEncrypt(byte[] data, AsymmetricKeyParameter key)
        {
            try
            {
                RsaEngine e = new RsaEngine();
                e.Init(true, key);

                int blockSize = e.GetInputBlockSize();

                List<byte> output = new List<byte>();

                for (int chunkPosition = 0; chunkPosition < data.Length; chunkPosition += blockSize)
                {
                    int chunkSize = Math.Min(blockSize, data.Length - (chunkPosition * blockSize));
                    output.AddRange(e.ProcessBlock(data, chunkPosition, chunkSize));
                }

                return output.ToArray();
            }
            catch (Exception ex)
            {

                return null;
            }
        }


public static byte[] RSADecrypt(byte[] data, AsymmetricKeyParameter key)
        {
            try
            {
                RsaEngine e = new RsaEngine();
                e.Init(false, key);

                int blockSize = e.GetInputBlockSize();

                List<byte> output = new List<byte>();

                for (int chunkPosition = 0; chunkPosition < data.Length; chunkPosition += blockSize)
                {
                    int chunkSize = Math.Min(blockSize, data.Length - (chunkPosition * blockSize));
                    output.AddRange(e.ProcessBlock(data, chunkPosition, chunkSize));
                }

                return output.ToArray();
            }
            catch (Exception ex)
            {

                return null;
            }
        }

Upvotes: 0

Views: 2759

Answers (1)

Joachim Isaksson
Joachim Isaksson

Reputation: 181017

RSA is an asymmetric encryption method that encrypts a number less than the modulus of the RSA key (255 bytes would indicate that you're using a 256*8 = 2048 bit RSA key/modulus)

What you need to do to encrypt values greater than that is to generate a key, encrypt the data using a symmetric cipher (AES is not a bad choice) and encrypt the AES key using your private RSA key (preferably along with some other random data).

The AES key is a maximum of 256 bits, which will encrypt just fine with RSA, and AES does not have a size limit.

Upvotes: 2

Related Questions