C# DES 解密异常
//C# DES解密方法 public string Decrypt(string pToDecrypt, string sKey)
{
DESCryptoServiceProvider des = new DESCryptoServiceProvider();
byte[] inputByteArray = new byte[pToDecrypt.Length / 2];
for (int x = 0; x < pToDecrypt.Length / 2; x++)
{
int i = (Convert.ToInt32(pToDecrypt.Substring(x * 2, 2), 16));
inputByteArray[x] = (byte)i;
}
byte [] inputByteArraykey = new byte[sKey.Length / 2];
for (int x = 0; x < sKey.Length / 2; x++)
{
int i = (Convert.ToInt32(sKey.Substring(x * 2, 2), 16));
inputByteArraykey[x] = (byte)i;
}
des.Key = (byte[])inputByteArraykey;
des.IV = (byte[])inputByteArraykey;
MemoryStream ms = new MemoryStream();
CryptoStream cs = new CryptoStream(ms, des.CreateDecryptor(), CryptoStreamMode.Write);
cs.Write(inputByteArray, 0, inputByteArray.Length);
try
{
cs.FlushFinalBlock();
}
catch (SystemException ex)
{
System.Windows.Forms.MessageBox.Show(ex.Message);
}
StringBuilder ret = new StringBuilder();
return System.Text.Encoding.Default.GetString(ms.ToArray());
}
自己建的一个测试数据,pToDecrypt=“D9932D9DH4021B68EC91C730291EC88C”,Skey="1472583690123456",skey为16位的数据,DES解密的密钥需要8字节的skey,我按2位合为一个字节进行测试,在catch里面就会捕捉异常,“不正确的数据”,这是什么原因呢?
[ 本帖最后由 ws9187 于 2014-8-14 09:32 编辑 ]