本文介绍了显示错误填充无效,无法删除.的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
嗨 iam在基于Windows的application.im中尝试尝试解密加密密码.我可以很好地加密密码,但是我尝试显示这样的decrpt错误
填充无效,无法删除
我使用下面的代码
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
推荐答案
这篇关于显示错误填充无效,无法删除.的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!