对称加密问题,高分求助!
ywcyj 2004-07-02 02:17:43 我写对称加密函数,遇到一个问题:不认识键盘上的&字符,其他字符好像都没问题。大家帮我看看如何修改程序:
源代码如下:
//加密字符串
public string DesEncrypt(string strText)
{ try
{
byte[] byKey = byte[] IV= {0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF};
byte[] IV = byte[] IV= {0x23, 0x45, 0x67, 0x89, 0x0A, 0xBC, 0xDE, 0xF1};
DESCryptoServiceProvider des = new DESCryptoServiceProvider();
byte[] inputByteArray = System.Text.Encoding.UTF8.GetBytes(strText);
MemoryStream ms = new MemoryStream();
CryptoStream cs = new CryptoStream(ms, des.CreateEncryptor(byKey, IV), CryptoStreamMode.Write) ;
cs.Write(inputByteArray, 0, inputByteArray.Length);
cs.FlushFinalBlock();
return Convert.ToBase64String(ms.ToArray());
}
catch(System.Exception error)
{
return "error:" +error.Message+"\r";
}
}
//解密字符串
public string DesDecrypt(string strText)
{
try
{
byte[] byKey = byte[] IV= {0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF};
byte[] IV = byte[] IV= {0x23, 0x45, 0x67, 0x89, 0x0A, 0xBC, 0xDE, 0xF1};
byte[] inputByteArray = new Byte[strText.Length];
DESCryptoServiceProvider des = new DESCryptoServiceProvider();
inputByteArray = Convert.FromBase64String(strText);
MemoryStream ms = new MemoryStream();
CryptoStream cs = new CryptoStream(ms, des.CreateDecryptor(byKey, IV), CryptoStreamMode.Write);
cs.Write(inputByteArray, 0, inputByteArray.Length);
cs.FlushFinalBlock();
System.Text.Encoding encoding = new System.Text.UTF8Encoding();
return encoding.GetString(ms.ToArray());
}
catch(System.Exception error)
{
return "error:"+error.Message+"\r";
}
}