C# DES解密报错:不正确的数据。
正常的给某个url参数加密,是没任何问题的。我页面上有个repeat点下一页的时候就报这个错。网上的解决方案很少,都试了一遍还是这样,求大神帮忙解决下!publicsta...
正常的给某个url参数加密,是没任何问题的。我页面上有个repeat 点下一页的时候就报这个错。网上的解决方案很少,都试了一遍还是这样,求大神帮忙解决下!
public static string Decode(string str, string key)
{
DESCryptoServiceProvider provider = new DESCryptoServiceProvider();
provider.Key = Encoding.ASCII.GetBytes(key.Substring(0, 8));
provider.IV = Encoding.ASCII.GetBytes(key.Substring(0, 8));
byte[] buffer = new byte[str.Length / 2];
for (int i = 0; i < (str.Length / 2); i++)
{
int num2 = Convert.ToInt32(str.Substring(i * 2, 2), 0x10);
buffer[i] = (byte)num2;
}
MemoryStream stream = new MemoryStream();
CryptoStream stream2 = new CryptoStream(stream, provider.CreateDecryptor(), CryptoStreamMode.Write);
stream2.Write(buffer, 0, buffer.Length);
stream2.FlushFinalBlock();
stream.Close();
return Encoding.GetEncoding("GB2312").GetString(stream.ToArray());
} 展开
public static string Decode(string str, string key)
{
DESCryptoServiceProvider provider = new DESCryptoServiceProvider();
provider.Key = Encoding.ASCII.GetBytes(key.Substring(0, 8));
provider.IV = Encoding.ASCII.GetBytes(key.Substring(0, 8));
byte[] buffer = new byte[str.Length / 2];
for (int i = 0; i < (str.Length / 2); i++)
{
int num2 = Convert.ToInt32(str.Substring(i * 2, 2), 0x10);
buffer[i] = (byte)num2;
}
MemoryStream stream = new MemoryStream();
CryptoStream stream2 = new CryptoStream(stream, provider.CreateDecryptor(), CryptoStreamMode.Write);
stream2.Write(buffer, 0, buffer.Length);
stream2.FlushFinalBlock();
stream.Close();
return Encoding.GetEncoding("GB2312").GetString(stream.ToArray());
} 展开
2个回答
展开全部
/// <summary>
/// DES数据加密
/// </summary>
/// <param name="targetValue">目标值</param>
/// <param name="key">密钥</param>
/// <returns>加密值</returns>
public static string Encrypt(string targetValue, string key)
{
if (string.IsNullOrEmpty(targetValue))
{
return string.Empty;
}
var returnValue = new StringBuilder();
var des = new DESCryptoServiceProvider();
byte[] inputByteArray = Encoding.Default.GetBytes(targetValue);
// 通过两次哈希密码设置对称算法的初始化向量
des.Key = Encoding.ASCII.GetBytes(FormsAuthentication.HashPasswordForStoringInConfigFile
(FormsAuthentication.HashPasswordForStoringInConfigFile(key, "md5").
Substring(0, 8), "sha1").Substring(0, 8));
// 通过两次哈希密码设置算法的机密密钥
des.IV = Encoding.ASCII.GetBytes(FormsAuthentication.HashPasswordForStoringInConfigFile
(FormsAuthentication.HashPasswordForStoringInConfigFile(key, "md5")
.Substring(0, 8), "md5").Substring(0, 8));
var ms = new MemoryStream();
var cs = new CryptoStream(ms, des.CreateEncryptor(), CryptoStreamMode.Write);
cs.Write(inputByteArray, 0, inputByteArray.Length);
cs.FlushFinalBlock();
foreach (byte b in ms.ToArray())
{
returnValue.AppendFormat("{0:X2}", b);
}
return returnValue.ToString();
}
/// <summary>
/// DES数据解密
/// </summary>
/// <param name="targetValue"></param>
/// <param name="key"></param>
/// <returns></returns>
public static string Decrypt(string targetValue, string key)
{
if (string.IsNullOrEmpty(targetValue))
{
return string.Empty;
}
// 定义DES加密对象
var des = new DESCryptoServiceProvider();
int len = targetValue.Length / 2;
var inputByteArray = new byte[len];
int x, i;
for (x = 0; x < len; x++)
{
i = Convert.ToInt32(targetValue.Substring(x * 2, 2), 16);
inputByteArray[x] = (byte)i;
}
// 通过两次哈希密码设置对称算法的初始化向量
des.Key = Encoding.ASCII.GetBytes(FormsAuthentication.HashPasswordForStoringInConfigFile
(FormsAuthentication.HashPasswordForStoringInConfigFile(key, "md5").
Substring(0, 8), "sha1").Substring(0, 8));
// 通过两次哈希密码设置算法的机密密钥
des.IV = Encoding.ASCII.GetBytes(FormsAuthentication.HashPasswordForStoringInConfigFile
(FormsAuthentication.HashPasswordForStoringInConfigFile(key, "md5")
.Substring(0, 8), "md5").Substring(0, 8));
// 定义内存流
var ms = new MemoryStream();
// 定义加密流
var cs = new CryptoStream(ms, des.CreateDecryptor(), CryptoStreamMode.Write);
cs.Write(inputByteArray, 0, inputByteArray.Length);
cs.FlushFinalBlock();
return Encoding.Default.GetString(ms.ToArray());
}
/// DES数据加密
/// </summary>
/// <param name="targetValue">目标值</param>
/// <param name="key">密钥</param>
/// <returns>加密值</returns>
public static string Encrypt(string targetValue, string key)
{
if (string.IsNullOrEmpty(targetValue))
{
return string.Empty;
}
var returnValue = new StringBuilder();
var des = new DESCryptoServiceProvider();
byte[] inputByteArray = Encoding.Default.GetBytes(targetValue);
// 通过两次哈希密码设置对称算法的初始化向量
des.Key = Encoding.ASCII.GetBytes(FormsAuthentication.HashPasswordForStoringInConfigFile
(FormsAuthentication.HashPasswordForStoringInConfigFile(key, "md5").
Substring(0, 8), "sha1").Substring(0, 8));
// 通过两次哈希密码设置算法的机密密钥
des.IV = Encoding.ASCII.GetBytes(FormsAuthentication.HashPasswordForStoringInConfigFile
(FormsAuthentication.HashPasswordForStoringInConfigFile(key, "md5")
.Substring(0, 8), "md5").Substring(0, 8));
var ms = new MemoryStream();
var cs = new CryptoStream(ms, des.CreateEncryptor(), CryptoStreamMode.Write);
cs.Write(inputByteArray, 0, inputByteArray.Length);
cs.FlushFinalBlock();
foreach (byte b in ms.ToArray())
{
returnValue.AppendFormat("{0:X2}", b);
}
return returnValue.ToString();
}
/// <summary>
/// DES数据解密
/// </summary>
/// <param name="targetValue"></param>
/// <param name="key"></param>
/// <returns></returns>
public static string Decrypt(string targetValue, string key)
{
if (string.IsNullOrEmpty(targetValue))
{
return string.Empty;
}
// 定义DES加密对象
var des = new DESCryptoServiceProvider();
int len = targetValue.Length / 2;
var inputByteArray = new byte[len];
int x, i;
for (x = 0; x < len; x++)
{
i = Convert.ToInt32(targetValue.Substring(x * 2, 2), 16);
inputByteArray[x] = (byte)i;
}
// 通过两次哈希密码设置对称算法的初始化向量
des.Key = Encoding.ASCII.GetBytes(FormsAuthentication.HashPasswordForStoringInConfigFile
(FormsAuthentication.HashPasswordForStoringInConfigFile(key, "md5").
Substring(0, 8), "sha1").Substring(0, 8));
// 通过两次哈希密码设置算法的机密密钥
des.IV = Encoding.ASCII.GetBytes(FormsAuthentication.HashPasswordForStoringInConfigFile
(FormsAuthentication.HashPasswordForStoringInConfigFile(key, "md5")
.Substring(0, 8), "md5").Substring(0, 8));
// 定义内存流
var ms = new MemoryStream();
// 定义加密流
var cs = new CryptoStream(ms, des.CreateDecryptor(), CryptoStreamMode.Write);
cs.Write(inputByteArray, 0, inputByteArray.Length);
cs.FlushFinalBlock();
return Encoding.Default.GetString(ms.ToArray());
}
展开全部
本人也遇到此问题,对比之前写的正确的代码发现解决办法。
分析:
DES加密只要向量、数据长度、秘钥如果都是8字节,那么就不会存在什么不正确的数据,因为对于加密算法而已,加密的数据应该是任意的。
在C#中,DES加密里面有加密模式、填充方式等属性,这两个数据都有默认值,分别为模式:CBC;填充PKCS7。 而模式应该也是任意的不应该会出现错误,所以问题就在填充格式了。
我把之前的问题代码里面DES加密的填充属性改为不填充,问题完美解决。只要保证向量、秘钥都是8字节且数据长度为8的整数倍即可。
分析:
DES加密只要向量、数据长度、秘钥如果都是8字节,那么就不会存在什么不正确的数据,因为对于加密算法而已,加密的数据应该是任意的。
在C#中,DES加密里面有加密模式、填充方式等属性,这两个数据都有默认值,分别为模式:CBC;填充PKCS7。 而模式应该也是任意的不应该会出现错误,所以问题就在填充格式了。
我把之前的问题代码里面DES加密的填充属性改为不填充,问题完美解决。只要保证向量、秘钥都是8字节且数据长度为8的整数倍即可。
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询