Solved

Help with RijndaelManaged encryption problem using c#

Posted on 2007-03-25
2
1,490 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

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

Problem Hi all,    While many today have fast Internet connection, there are many still who do not, or are connecting through devices with a slower connect, so light web pages and fast load times are still popular.    If your ASP.NET page …
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…

743 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

11 Experts available now in Live!

Get 1:1 Help Now