Solved

Help with RijndaelManaged encryption problem using c#

Posted on 2007-03-25
2
1,515 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Today is the age of broadband.  More and more people are going this route determined to experience the web and it’s multitude of services as quickly and painlessly as possible. Coupled with the move to broadband, people are experiencing the web via …
A quick way to get a menu to work on our website, is using the Menu control and assign it to a web.sitemap using SiteMapDataSource. Example of web.sitemap file: (CODE) Sample code to add to the page menu: (CODE) Running the application, we wi…
Come and listen to Percona CEO Peter Zaitsev discuss what’s new in Percona open source software, including Percona Server for MySQL (https://www.percona.com/software/mysql-database/percona-server) and MongoDB (https://www.percona.com/software/mongo-…
In this video we outline the Physical Segments view of NetCrunch network monitor. By following this brief how-to video, you will be able to learn how NetCrunch visualizes your network, how granular is the information collected, as well as where to f…

726 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