1

I have no idea how to describe my problem. It is the simplest way to encrypt a byte array, and I literally get "Unspecified Error" at the .Encrypt(...) method.

byte[] cleartext =
{
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
    0x71, 0x77, 0x65, 0x72, 0x74, 0x79, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
    0x61, 0x73, 0x64, 0x66, 0x67, 0x68, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
};

Logger.Hex("Clear test login text", cleartext);

byte[] ciphered = new RSACryptoServiceProvider(1024).Encrypt(cleartext, false);

Logger.Hex("Ciphered test login text", ciphered);

Console.Read();

Note: Logger.Hex displays a hex string representation of the byte array. Nothing interfering.

1 Answer 1

3

You're using a key size of 1024 bits (128 bytes) and PKCS#1 v1.5 padding, and you pass an array of 128 bytes to Encrypt.

From MSDN:

                                     Maximum Length of rgb Parameter

Direct Encryption (PKCS#1 v1.5)      Modulus size - 11. (11 bytes is the
                                     minimum padding possible.)

So your 1024-bit key is too small to encrypt 128 bytes. Have you tried increasing the key size?

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

3 Comments

I'm not using any padding as far as I know (notice the second parameter (bool false) of the encrypt function).
Read the documentation carefully: "fOAEP: false to use PKCS#1 v1.5 padding."
Dang. This is really deceiving. I need direct encryption. However: Direct Encryption and OAEP padding not supported : The maximum size allowed for a symmetric key. Is there any RSA custom class that could what I need? This code is somehow a port from Java, which can support direct encryption. I really am stuck if I can't encrypt/decrypt directly.

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.