Index out of range exception when decrypting...

Oct 13, 2009 at 10:36 AM
Edited Oct 13, 2009 at 10:43 AM

Hi gang,

having issues with svn and windows 7 ... can't add my code to the repository.

Below you'll find a symmetric method for encrypting / decrypting text data. Of course, the book says decrypting is simply a matter of changing a few things but it ain't workin!

a little help? Obviously, since i can't get past the decrypt ... i can't test to see if i've encrypted properly. This is not going well :(

The exception occurs at step 6 of the DecryptTextData method..

private string EncryptTextData(string text)
{   
    // Step 1: Symmetric Algorithm object
    RijndaelManaged myAlg = CommonMethods.GetSymmetricKeyRijndaelManaged(PasswordKeywordTextBox.Text);

    // Step 2: Read the unencrypted text into data
    byte[] byteArray = Encoding.ASCII.GetBytes(text);

    // Step 4: Create the icryptotransfoorm object
    ICryptoTransform ict = myAlg.CreateEncryptor();

    // Step 5: Create the output stream and CryptoStream objects
    MemoryStream outStream = new MemoryStream(byteArray.Length);
    CryptoStream cryptoStream = new CryptoStream(outStream, ict, CryptoStreamMode.Write);

    // Step 6: write the contents (byteArray) to the CryptoStream
    cryptoStream.Write(byteArray, 0, (int)byteArray.Length);
    //cryptoStream.FlushFinalBlock();

    // Step 7: Convert our encrypted data from a memory stream into a byte array.
    string ret = CommonMethods.GetStringFromMemoryStream(outStream);

    // Step 8: Close both streams.
    cryptoStream.Close();
    outStream.Close();

    // Step 9: Return converted encrypted data into a string.
    return ret;

}

private string DecryptTextData(string text)
{
    // Step 1: Symmetric Algorithm object
    RijndaelManaged myAlg = CommonMethods.GetSymmetricKeyRijndaelManaged(PasswordKeywordTextBox.Text);

    // Step 2: Read the unencrypted text into data
    byte[] byteArray = Encoding.ASCII.GetBytes(text);

    // Step 4: Create the icryptotransfoorm object
    ICryptoTransform ict = myAlg.CreateDecryptor();

    // Step 5: Create the output stream and CryptoStream objects
    MemoryStream outStream = new MemoryStream(byteArray.Length);
    CryptoStream cryptoStream = new CryptoStream(outStream, ict, CryptoStreamMode.Read);

    // Step 6: write the contents (byteArray) to the CryptoStream
    cryptoStream.Read(byteArray, 0, (int)byteArray.Length);
    //cryptoStream.FlushFinalBlock();

    // Step 7: Convert our encrypted data from a memory stream into a byte array.
    string ret = CommonMethods.GetStringFromMemoryStream(outStream);

    // Step 8: Close both streams.
    cryptoStream.Close();
    outStream.Close();

    // Step 9: Return converted encrypted data into a string.
    return ret;

}

Coordinator
Oct 14, 2009 at 3:19 PM

Have you tried to upload the code yet?

Oct 15, 2009 at 1:09 AM

Fixed and uploaded.  When you decrypt the memory stream is created with the encrypted array.  The Read method reads from the MemoryStream to the Byte Array (opposite of Write).

Oct 15, 2009 at 2:49 PM

Awesome! Thanks!