DES解密出现-“不正确的数据”和“要解密的数据长度无效”-问题,高手来解决!在线等!
源码如下:
/// <summary>
/// 解密给定的字符串
/// </summary>
/// <param name='str'>要解密的字符</param>
/// <returns></returns>
public string DecryptString(string str)
{
byte[] ivb=Encoding.ASCII.GetBytes(this.iv);
byte[] keyb=Encoding.ASCII.GetBytes(this.EncryptKey);
byte[] toDecrypt=this.EncodingMode.GetBytes(str);
byte[] deCrypted=new byte[toDecrypt.Length];
ICryptoTransform deCryptor=des.CreateDecryptor(keyb,ivb);
MemoryStream msDecrypt=new MemoryStream(toDecrypt);
CryptoStream csDecrypt=new CryptoStream(msDecrypt,deCryptor,CryptoStreamMode.Read);
try
{
csDecrypt.Read(deCrypted,0,deCrypted.Length);
}
catch(Exception err)
{
throw new ApplicationException(err.Message);
}
finally
{
try
{
msDecrypt.Close();
csDecrypt.Close();
}
catch{;}
}
return this.EncodingMode.GetString(deCrypted);
}
为什么会出现这个问题?