Solved

Help with RijndaelManaged encryption problem using c#

Posted on 2007-03-25
2
1,492 Views
Last Modified: 2012-05-05
I need some help figuring out an issue using RijndaelManaged to encrypt and decrypt a string.

I took the code from

http://www.obviex.com/samples/Encryption.aspx

and used as is get an error about padding invalid SO I changed the padding to Padding.None and it seems to encrypt..or at least it returns an encrypted value without givig me the padding error.

but when I decrypt the string it doesnt return as the original value but instead returns as some gobbledeegoock

Below is my code and this is the data I feed to the methods....but before you loko at the code this is what I encrypt

7henewp@s5phr@se
this is what returns encrypted.
gPtlrVtf+cKn0Yu7EQrgDQ==
but this is what gets decrypted.
h`*B7*Z



protected void Button1_Click(object sender, EventArgs e)
    {
        string plainText = "7henewp@s5phr@se";
        string passPhrase = "encrypt7heb@st@rd";
        string saltValue = "2s@lty4me";
        string hashAlgorith = "SHA1";
        int pwIterations = 7;
        string initVector = "@1B9f5P2!0n3G69T";
        int keySize = 256;

        TextBox1.Text = plainText;

        string cipherText = RijndaelSimple.Encrypt(plainText,
                                                   passPhrase,
                                                   saltValue,
                                                   hashAlgorith,
                                                   pwIterations,
                                                   initVector,
                                                   keySize);
        Label1.Text = cipherText;

        string results = RijndaelSimple.Decrypt(cipherText,
                                                passPhrase,
                                                saltValue,
                                                hashAlgorith,
                                                pwIterations,
                                                initVector,
                                                keySize);
        Label2.Text = results;

public class RijndaelSimple
{
      public static string Encrypt(string plaintext,
                                 string passPhrase,
                                 string saltValue,
                                 string hashAlgorithm,
                                 int pwIterations,
                                 string initVector,
                                 int keySize)
   
      {
       
        byte[] initVectorBytes = Encoding.ASCII.GetBytes(initVector);
        byte[] saltValueBytes = Encoding.ASCII.GetBytes(saltValue);
        byte[] plainTextBytes = Encoding.UTF8.GetBytes(plaintext);
        PasswordDeriveBytes password = new PasswordDeriveBytes(passPhrase, saltValueBytes, hashAlgorithm, pwIterations);
        byte[] keyBytes = password.GetBytes(keySize / 8);
        RijndaelManaged symmetricKey = new RijndaelManaged();
        symmetricKey.Mode = CipherMode.CBC;
        symmetricKey.Padding = PaddingMode.None;
        ICryptoTransform encryptor = symmetricKey.CreateDecryptor(keyBytes, initVectorBytes);
        MemoryStream memoryStream = new MemoryStream();
        CryptoStream cryptoStream = new CryptoStream(memoryStream, encryptor, CryptoStreamMode.Write);
        cryptoStream.Write(plainTextBytes, 0, plainTextBytes.Length);
        cryptoStream.FlushFinalBlock();
        byte[] cipherTextBytes = memoryStream.ToArray();
        memoryStream.Close();
        cryptoStream.Close();
        string cipherText = Convert.ToBase64String(cipherTextBytes);
        return cipherText;
      }
    public static string Decrypt(string ciphertext,
                                 string passPhrase,
                                 string saltValue,
                                 string hashAlgorithm,
                                 int pwIterations,
                                 string initVector,
                                 int keySize)
    {

        byte[] initVectorBytes = Encoding.ASCII.GetBytes(initVector);
        byte[] saltValueBytes = Encoding.ASCII.GetBytes(saltValue);
        byte[] cipherTextBytes = Convert.FromBase64String(ciphertext);
        PasswordDeriveBytes password = new PasswordDeriveBytes(passPhrase, saltValueBytes, hashAlgorithm, pwIterations);
        byte[] keyBytes = password.GetBytes(keySize / 8);
        RijndaelManaged symmetricKey = new RijndaelManaged();
        symmetricKey.Mode = CipherMode.CBC;
        symmetricKey.Padding = PaddingMode.None;
        ICryptoTransform decryptor = symmetricKey.CreateDecryptor(keyBytes, initVectorBytes);
        MemoryStream memoryStream = new MemoryStream(cipherTextBytes);
        CryptoStream cryptoStream = new CryptoStream(memoryStream, decryptor, CryptoStreamMode.Read);
        byte[] plainTextBytes = new byte[cipherTextBytes.Length];
        int decryptedByteCount = cryptoStream.Read(plainTextBytes, 0, plainTextBytes.Length);
        memoryStream.Close();
        cryptoStream.Close();
        string plainText = Encoding.UTF8.GetString(plainTextBytes, 0, decryptedByteCount);
        return plainText;
    }
}
0
Comment
Question by:Prysson
2 Comments
 
LVL 3

Accepted Solution

by:
kingtam2000 earned 500 total points
ID: 18788663
It's caused by the fact that you are decrypting the string twice, as the encryption function is the same as the decryption function.  You should change the following code in the encrypt function to encrypt it:
        ICryptoTransform encryptor = symmetricKey.CreateDecryptor(keyBytes, initVectorBytes);
to
       ICryptoTransform encryptor = symmetricKey.CreateEncryptor(keyBytes, initVectorBytes);

Once that is changed it should work.

Hope that helps.
0
 

Author Comment

by:Prysson
ID: 18788772
Awesome catch..I dpont know how many times I looked through that and kept missing it.

Thanks for the help!!!
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Split in Javascript 5 30
C# Single Form 8 27
Close form "before" open 3 24
Gridview alignment 1 8
This article introduced a TextBox that supports transparent background.   Introduction TextBox is the most widely used control component in GUI design. Most GUI controls do not support transparent background and more or less do not have the…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
Windows 10 is mostly good. However the one thing that annoys me is how many clicks you have to do to dial a VPN connection. You have to go to settings from the start menu, (2 clicks), Network and Internet (1 click), Click VPN (another click) then fi…
Many functions in Excel can make decisions. The most simple of these is the IF function: it returns a value depending on whether a condition you describe is true or false. Once you get the hang of using the IF function, you will find it easier to us…

911 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

23 Experts available now in Live!

Get 1:1 Help Now