3DES解密出错,分析出是加密字符串中间含+号未取得?
密匙设定 :GenerateKey().Substring(0, 8) = "YongBiao";
///DES加密字符串
public static string EncryptString(string encryptString)
{
try
{
byte[] rgbKey = Encoding.UTF8.GetBytes(GenerateKey().Substring(0, 8));
byte[] rgbIV = { 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF };
byte[] inputByteArray = Encoding.UTF8.GetBytes(encryptString);
DESCryptoServiceProvider dCSP = new DESCryptoServiceProvider();
MemoryStream mStream = new MemoryStream();
CryptoStream cStream = new CryptoStream(mStream, dCSP.CreateEncryptor(rgbKey, rgbIV), CryptoStreamMode.Write);
cStream.Write(inputByteArray, 0, inputByteArray.Length);
cStream.FlushFinalBlock();
return Convert.ToBase64String(mStream.ToArray());
}
catch (System.Exception error)
{
//return "error:" + error.Message + "\r";
return encryptString;
}
}
/// DES解密字符串
public static string DecryptString(string decryptString)
{
try
{
//byte[] rgbKey = Encoding.UTF8.GetBytes(decryptKey.Substring(0, 8));
byte[] rgbKey = Encoding.UTF8.GetBytes(GenerateKey().Substring(0, 8));
byte[] rgbIV = { 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF };
byte[] inputByteArray = Convert.FromBase64String(decryptString);
DESCryptoServiceProvider DCSP = new DESCryptoServiceProvider();
MemoryStream mStream = new MemoryStream();
CryptoStream cStream = new CryptoStream(mStream, DCSP.CreateDecryptor(rgbKey, rgbIV), CryptoStreamMode.Write);
cStream.Write(inputByteArray, 0, inputByteArray.Length);
cStream.FlushFinalBlock();
return Encoding.UTF8.GetString(mStream.ToArray());
}
catch (System.Exception error)
{
//return "error:" + error.Message + "\r";
return decryptString;
}
}
从一个页面a.aspx转向另一个页面时候 如 URL传递字符 b.aspx?ID= 3,加密后 ID=aUG+v97wvk9=,当新页面读取参数时候
string ID = Request.Params["ID"] == null ? "0" : Common.DecryptString(Request.Params["ID"]);
总是出错,经调试 Request.Params["ID"]传值实际上aUG+v97wvk9=变为了aUG v97wvk9= ,ID=6时候也有这个问题,如何规避页面传值时丢掉中间+等特殊字符问题?