在c#mvc中使用解密和加密时出现错误数据
我正在用C#使用在c#mvc中使用解密和加密时出现错误数据,c#,.net,encryption,C#,.net,Encryption,我正在用C#使用解密和加密,得到了这个错误 例外情况详情: System.Security.Cryptography.Cryptography异常:错误数据 源错误: 第64行:加密流cs=新加密流(ms, des.CreateDecryptor(DecryptKey,IV),CryptoStreamMode.Write);第65行: cs.Write(inputByte,0,inputByte.Length);第66行: cs.FlushFinalBlock();第67行:System.Tex
解密
和加密
,得到了这个错误
例外情况详情:
System.Security.Cryptography.Cryptography异常:错误数据
源错误:
第64行:加密流cs=新加密流(ms,
des.CreateDecryptor(DecryptKey,IV),CryptoStreamMode.Write);第65行:
cs.Write(inputByte,0,inputByte.Length);第66行:cs.FlushFinalBlock();第67行:System.Text.Encoding encoding=System.Text.encoding.UTF8;第68行:返回 encoding.GetString(ms.ToArray()) 我如何解决这个问题;这是我的密码
public static string Encrypt(string plainText)
{
string key = "amir100amir*&%$#";
byte[] EncryptKey = { };
byte[] IV = { 55, 34, 87, 64, 87, 195, 54, 21 };
EncryptKey = System.Text.Encoding.UTF8.GetBytes(key.Substring(0, 8));
DESCryptoServiceProvider des = new DESCryptoServiceProvider();
byte[] inputByte = Encoding.UTF8.GetBytes(plainText);
MemoryStream mStream = new MemoryStream();
CryptoStream cStream = new CryptoStream(mStream, des.CreateEncryptor(EncryptKey, IV), CryptoStreamMode.Write);
cStream.Write(inputByte, 0, inputByte.Length);
cStream.FlushFinalBlock();
return Convert.ToBase64String(mStream.ToArray()).Replace("/", "_").Replace("+", "-");
}
public static string encodeSTROnUrl(string thisEncode)
{
if (null == thisEncode)
return string.Empty;
return (Encrypt(thisEncode));
}
public static string decodeSTROnUrl(string thisDecode)
{
//thisDecode = thisDecode.Replace(" ", "+");
// return Decrypt(HttpUtility.UrlDecode(thisDecode));
return Decrypt((thisDecode));
}
public static string Decrypt(string encryptedText)
{
// try
{
encryptedText = encryptedText.Replace("_", "/");
encryptedText = encryptedText.Replace("-", "+");
string key = "amir100amir*&%$#";
byte[] DecryptKey = { };
byte[] IV = { 55, 34, 87, 64, 87, 195, 54, 21 };
byte[] inputByte = new byte[encryptedText.Length];
DecryptKey = System.Text.Encoding.UTF8.GetBytes(key.Substring(0, 8));
DESCryptoServiceProvider des = new DESCryptoServiceProvider();
inputByte = Convert.FromBase64String(encryptedText);
MemoryStream ms = new MemoryStream();
CryptoStream cs = new CryptoStream(ms, des.CreateDecryptor(DecryptKey, IV), CryptoStreamMode.Write);
cs.Write(inputByte, 0, inputByte.Length);
cs.FlushFinalBlock();
System.Text.Encoding encoding = System.Text.Encoding.UTF8;
return encoding.GetString(ms.ToArray());
}
//catch
//{
//}
//finally { }
//return "Error";
}
哪一行代码产生了错误?如何调用这些函数代码>似乎有效。你能提供更多细节吗?代码对我有用。但我使用的是基本的ascii字符串;加密“meep_meep”并对其解密会产生预期的行为。我们能看到OP加密的字符串是什么吗?也许它包含非标准字符。似乎也适用于中文
極秘代码>cs.FlushFinalBlock();是错误吗