views:

367

answers:

1

Hello :)

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.

+3  A: 

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?

dtb
I'm not using any padding as far as I know (notice the second parameter (bool false) of the encrypt function).
Lazlo
Read the documentation carefully: "fOAEP: **false** to use PKCS#1 v1.5 padding."
dtb
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.
Lazlo