Click here to Skip to main content
15,888,039 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi iam working in windows based application.im trying to decrpt an encrypt password .i can encrypt passwords well but im trying decrpt error showing like this

Padding is invalid and cannot be removed

Iam using below code
string Salt = "Kosher", HashAlgorithm = "SHA1";

            int PasswordIterations = 2;
            string InitialVector = "OFRna73m*aze01xY";

            int KeySize = 256;
            if (string.IsNullOrEmpty(CipherText))
                return "";
            byte[] InitialVectorBytes = System.Text.Encoding.ASCII.GetBytes(InitialVector);
            byte[] SaltValueBytes = System.Text.Encoding.ASCII.GetBytes(Salt);

            byte[] CipherTextBytes = Convert.FromBase64String(CipherText);
            PasswordDeriveBytes DerivedPassword = new PasswordDeriveBytes(Password, SaltValueBytes, HashAlgorithm, PasswordIterations);
            byte[] KeyBytes = DerivedPassword.GetBytes(KeySize / 8);
            RijndaelManaged SymmetricKey = new RijndaelManaged();
            SymmetricKey.Mode = CipherMode.CBC;
            byte[] PlainTextBytes = new byte[CipherTextBytes.Length];
            int ByteCount = 0;
            using (ICryptoTransform Decryptor = SymmetricKey.CreateDecryptor(KeyBytes, InitialVectorBytes))
            {
                using (MemoryStream MemStream = new MemoryStream(CipherTextBytes))
                {
                    using (CryptoStream CryptoStream = new CryptoStream(MemStream, Decryptor, CryptoStreamMode.Read))
                    {

                        ByteCount = CryptoStream.Read(PlainTextBytes, 0, PlainTextBytes.Length);
                        MemStream.Close();
                        CryptoStream.Close();
                    }
                }
            }
            SymmetricKey.Clear();
            return System.Text.Encoding.UTF8.GetString(PlainTextBytes, 0, ByteCount);


error happening in
 ByteCount = CryptoStream.Read(PlainTextBytes, 0, PlainTextBytes.Length) this line as  <br />
<br />
<br />
Padding is invalid and cannot be removed<br />
<br />
<br />
Any body know why error like this arising?<br />
<br />
Thanks in Advance
Posted
Updated 14-Mar-12 1:54am
v3
Comments
Abhinav S 14-Mar-12 3:16am    
Code tags added.
Dave Kreskowiak 14-Mar-12 10:25am    
I'm not real keen on decrypting passwords for any reason what so ever. All it does is open up a hole for someone to use to grab someones password or decrypt the entire user/password table if they get their hands on it.
BobJanova 14-Mar-12 13:30pm    
Good point, I hadn't noticed that passwords were involved.

 
Share this answer
 
You are trying to read more bytes out of the stream than are actually encoded, because you set the plain text length to the same as the cipher length, and the cipher includes padding to the next block size.

Does CrpytoStream let you read the Length property? If so, use that to set the size of PlainTextString. If not, you'll have to save that somewhere when you write out the encrypted version, so you can read the number back and use that to size PlainTextString.

Also, make sure you set up the keys, IV, algorithm etc up the same, because if you don't you will get one of a collection of opaque 'decryption failed' exceptions.
 
Share this answer
 

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