Bad data error when decrypting using RSA RRS feed

  • Question


    Hi, I found an error whenI  try to decrypt an encrypted array of bytes, but the following error occurs:

    Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

    Exception Details: System.Security.Cryptography.CryptographicException: Bad Data.

    RSACryptoServiceProvider RSA = new RSACryptoServiceProvider();

    //Create a new instance of the RSAParameters structure.
    RSAParameters RSAKeyInfo = new RSAParameters();

    //Initialize the byte arrays to the public key information.
    byte[] PublicKey = {214,46,220,83,160,73,40,39,201,155,19,202,3,11,191,178,56,

    byte[] Exponent = { 1, 0, 1 };

    //Create values to store password
    string password = txtInput.Text;
    byte[] btyPwd = UnicodeEncoding.ASCII.GetBytes(password);
    //Set RSAKeyInfo to the public key values.
    RSAKeyInfo.Modulus = PublicKey;
    RSAKeyInfo.Exponent = Exponent;

    //Import key parameters into RSA.

    byte[] encrypted = RSA.Encrypt(btyPwd, false);
    lblOutput.Text = UnicodeEncoding.ASCII.GetString(encrypted);

    //Create a new instance of the RSACryptoServiceProvider class.
    RSACryptoServiceProvider RSA = new RSACryptoServiceProvider();
    byte[] encrypted = UnicodeEncoding.ASCII.GetBytes(lblOutput.Text);
    byte[] btyPwd = RSA.Decrypt(encrypted, false);
    lblResult.Text = UnicodeEncoding.ASCII.GetString(btyPwd);

    WHat's the problem I faced? Solution? thanks

    Willfin David

    Wednesday, August 22, 2007 2:06 PM

All replies

  • I'm using RSACryptoServiceProvider for encryption and decryption

    The code is:

    byte[] decryptedBytes = RSACryptoServiceProvider.Decrypt(encryptedBytes,false);


    The above line shows an error:

    System.Security.Cryptography.RSACryptoServiceProvider:Bad Data


    But when i use the below code:

    byte[] decryptedBytes = m_RSACryptoServiceProvider.Decrypt(encryptedBytes,true);

    it returns byteArray of length 2.



    What is the cause for the issue?


    Willfin David

    Thursday, August 23, 2007 5:37 AM