public string DecryptString(string sInputString, string sKey)
{
DESCryptoServiceProvider des = new DESCryptoServiceProvider();
//Put the input string into the byte array
byte[] inputByteArray = new byte[sInputString.Length/2];
for (int x = 0; x < sInputString.Length / 2; x++)
{
int i = (Convert.ToInt32(sInputString.Substring(x * 2, 2), 16));
inputByteArray[x] = (byte)i;
}
des.Key = ASCIIEncoding.ASCII.GetBytes(sKey);
des.IV = ASCIIEncoding.ASCII.GetBytes(sKey);
MemoryStream ms = new MemoryStream();
CryptoStream cs = new CryptoStream(ms, des.CreateDecryptor(), CryptoStreamMode.Write);
//Flush the data through the crypto stream into the memory stream
cs.Write(inputByteArray, 0, inputByteArray.Length);
cs.FlushFinalBlock();
//Get the decrypted data back from the memory stream
//建立StringBuild对象,CreateDecrypt使用的是流对象,必须把解密后的文本变成流对象
StringBuilder ret = new StringBuilder();
return System.Text.Encoding.Default.GetString(ms.ToArray());
}
比如对中文“加密解密”加密然后解密后变成“鍔犲瘑瑙e瘑”,这是什么原因呢? 哪位大哥能指点一下,谢谢·!
3 个解决方案
#1
问题已经解决
return System.Text.Encoding.Default.GetString(ms.ToArray());
换成 return System.Text.Encoding.UTF8.GetString(ms.ToArray());就ok了
return System.Text.Encoding.Default.GetString(ms.ToArray());
换成 return System.Text.Encoding.UTF8.GetString(ms.ToArray());就ok了
#2
加解密的时候编码要一致
#3
学习...
#1
问题已经解决
return System.Text.Encoding.Default.GetString(ms.ToArray());
换成 return System.Text.Encoding.UTF8.GetString(ms.ToArray());就ok了
return System.Text.Encoding.Default.GetString(ms.ToArray());
换成 return System.Text.Encoding.UTF8.GetString(ms.ToArray());就ok了
#2
加解密的时候编码要一致
#3
学习...