Another RSACryptoServiceProvider question

From: Daryn Kiely (anonymous_at_discussions.microsoft.com)
Date: 10/29/03


Date: Wed, 29 Oct 2003 10:54:31 -0800

I have a simple little test program (see below) that
encrypts an array of bytes and turns around and decrypts
the same array. This works great if the fOAEP flag is
false (use PKCS padding) but fails if I use OAEP
padding. In both cases the encryption works, but in the
latter case the code throws a Cryptographic Exception
with the message "Error occured while decoding OAEP
padding". What am I missing here?!?!?! Oh, the code is
using the certificate store provided by Mentalis.org...

byte [] indata;
byte [] outdata;
byte [] encrypteddata = null;

CertificateStore cs = new CertificateStore("MY");
Certificate cert = cs.FindCertificateBySubjectString("R&D
Client");
RSACryptoServiceProvider rsaDecrypt =
               (RSACryptoServiceProvider) cert.PrivateKey;
RSACryptoServiceProvider rsaEncrypt =
               (RSACryptoServiceProvider) cert.PublicKey;
Console.WriteLine("Crypto Algorithm
                      {0}",rsaEncrypt.SignatureAlgorithm);
Console.WriteLine("Key Exchange Algorithm
                    {0}",rsaEncrypt.KeyExchangeAlgorithm);

indata = new byte[87];
for (int i = 0; i != indata.Length; i++)
{
        indata[i] = Convert.ToByte(i%255);
}
try
{
        encrypteddata = rsaEncrypt.Encrypt(indata, true);
}
catch (CryptographicException ce)
{
        Console.WriteLine("Crypto Exception
                              {0}",ce.Message);
        Console.WriteLine("Stack: {0}",ce.StackTrace);
}

try
{
        outdata = rsaEncrypt.Decrypt(encrypteddata, true);
}
catch (CryptographicException ce)
{
        Console.WriteLine("Crypto Exception {0}, size =
                                        {1}",ce.Message);
}



Relevant Pages

  • Another RSACryptoServiceProvider question
    ... encrypts an array of bytes and turns around and decrypts ... the same array. ... false (use PKCS padding) but fails if I use OAEP ... catch (CryptographicException ce) ...
    (microsoft.public.dotnet.security)
  • Encrypting byte array
    ... I need a function that encrypts a byte array, ... returns the encrypted byte array. ... I've done it using CryptoStream, ... then, on decryption, I don't know what is the data and what is the ...
    (microsoft.public.dotnet.security)
  • Re: Encrypting byte array
    ... > I need a function that encrypts a byte array, ... > returns the encrypted byte array. ... > block sizes are fixed, how do I know the length of ... > then, on decryption, I don't know what is the data and what is the ...
    (microsoft.public.dotnet.security)
  • Re: Padding and block ciphers
    ... while giving to each padding byte the value of the padding length ... decrypt with the wrong key, if the padding that you had to apply to the ... lets say that the block cyper encrypts bytes. ... key produces a random mapping. ...
    (sci.crypt)