showing error Padding is invalid and cannot be removed.

Posted by Amritha444 under C# on 3/14/2012 | Points: 10 | Views : 2737 | Status : [Member] | Replies : 0
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);</blockquote>

error happening in ByteCount = CryptoStream.Read(PlainTextBytes, 0, PlainTextBytes.Length) this line as <blockquote class="FQ"><div class="FQA">Quote:</div> Padding is invalid and cannot be removed.</blockquote>
Any body know why error like this arising?

Thanks in Advance




Responses

(No response found.)

Login to post response