Click here to Skip to main content
15,889,176 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
when I decrypt the encrypted text it doesn't decrypt the first few words of the document.
My Encryption method is :
public string EncryptString(string plainText,string key)
        {
            RijndaelManaged rijndaelCipher = new RijndaelManaged();

            rijndaelCipher.Key =CreateKey(key);            

            MemoryStream memoryStream = new MemoryStream();

            ICryptoTransform rijndaelEncryptor = rijndaelCipher.CreateEncryptor();

            CryptoStream cryptoStream = new CryptoStream(memoryStream, rijndaelEncryptor, CryptoStreamMode.Write);

            byte[] plainBytes = Encoding.ASCII.GetBytes(plainText);

            cryptoStream.Write(plainBytes, 0, plainBytes.Length);

            cryptoStream.FlushFinalBlock();

            byte[] cipherBytes = memoryStream.ToArray();

            memoryStream.Close();
            cryptoStream.Close();

            string cipherText = Convert.ToBase64String(cipherBytes, 0, cipherBytes.Length);

            return cipherText;
        }
private byte[] CreateKey(string password)
        {
            var salt = new byte[] { 1, 2, 23, 234, 37, 48, 134, 63, 248, 4 };

            const int Iterations = 9872;
            var rfc2898DeriveBytes = new Rfc2898DeriveBytes(password, salt, Iterations);            
                return rfc2898DeriveBytes.GetBytes(32);
            
        }

Decryption Method is :
public string DecryptString(string cipherText,string key)
       {
           RijndaelManaged rijndaelCipher = new RijndaelManaged();

           rijndaelCipher.Key =CreateKey(key);

           MemoryStream memoryStream = new MemoryStream();

           ICryptoTransform rijndaelDecryptor = rijndaelCipher.CreateDecryptor();

           CryptoStream cryptoStream = new CryptoStream(memoryStream, rijndaelDecryptor, CryptoStreamMode.Write);
           string plainText = String.Empty;

           try
           {
               byte[] cipherBytes = Convert.FromBase64String(cipherText);
               cryptoStream.Write(cipherBytes, 0, cipherBytes.Length);

               cryptoStream.FlushFinalBlock();

               byte[] plainBytes = memoryStream.ToArray();

               plainText = Encoding.ASCII.GetString(plainBytes, 0, plainBytes.Length);
           }
           finally
           {
               memoryStream.Close();
               cryptoStream.Close();
           }

           return plainText;
       }


My key is : "ghb" (which I provide when decryption method is called)..

Methods call :
textBox1.Text = BCF.EncryptString(textBox1.Text,"ghb");//encryption time 
textBox1.Text = BCF.DecryptString(textBox1.Text,"ghb");//decryption time


How to solved this method ?

Regards
Jayanta.
Posted
Comments
Dilan Shaminda 29-Jun-14 1:35am    
just check the documentation here..

http://msdn.microsoft.com/en-us/library/system.security.cryptography.rijndaelmanaged.aspx

1 solution

I recommend you to take a look at this example from MSDN.

http://msdn.microsoft.com/en-us/library/system.security.cryptography.rijndaelmanaged(v=vs.110).aspx

It works out of the box.
 
Share this answer
 
Comments
JayantaChatterjee 29-Jun-14 3:08am    
Thank you Sir.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900