提供的KEY是86A659D3035B51B1B66DF3139F1AEC33F6651334F1E61280,IV是0807060504030201
但是到程序调到
ct = mCSP.CreateEncryptor(keys, ivs);这部的时候就出现“指定键的大小对于此算法无效”错误,是什么原因?
上面的KEY,IV,我都转化成byte,有什么问题吗,请大虾指出,学习中...
但是到程序调到
ct = mCSP.CreateEncryptor(keys, ivs);这部的时候就出现“指定键的大小对于此算法无效”错误,是什么原因?
上面的KEY,IV,我都转化成byte,有什么问题吗,请大虾指出,学习中...
解决方案 »
- 父窗体中加入一个Panel,然后将要弹出的窗体作为Panel的控件显示到Panel上
- a.ToString("0,000")里面的格式化是什么意思
- 如何在多线程实现主线程的ShowDialog?
- 请问各位,如何获取光标前字符的个数
- 小弟新手,求大家帮我检查一下这个错误,谢谢了
- 关于拖拽下载功能
- 数据库连接失败:已成功与服务器建立连接,但是在登录过程中发生错误。 (provider: 共享内存提供程序, error: 0 - 管道的另一端上无任何进程。)
- C#问题(WInForm)~动态生成TextBox的问题!!
- 如何将CHAR数组转换成BYTE数组
- c#集合的问题,大佬帮忙看看
- 在Web项目中怎么让IE上面的菜单,地址栏等都去掉
- 关于google API 3.0 自定义图标
我在上面说Key的长度必须为16或24位是错误的,抱歉。string strText = "admin";
byte[] fKey = Encoding.ASCII.GetBytes("12345678");
byte[] fIV = Encoding.ASCII.GetBytes("0807060504030201");
DESCryptoServiceProvider des = new DESCryptoServiceProvider();
MemoryStream ms = new MemoryStream();
CryptoStream cs = new CryptoStream(ms, des.CreateEncryptor(fKey, fIV), CryptoStreamMode.Write);
StreamWriter sw = new StreamWriter(cs);
sw.Write(strText);
sw.Flush();
cs.FlushFinalBlock();
ms.Flush();
string result = Convert.ToBase64String(ms.GetBuffer(), 0, Convert.ToInt32(ms.Length));
alg.Key = Key;//32
alg.IV = IV;//16CryptoStream cs = new CryptoStream(ms,alg.CreateEncryptor(), CryptoStreamMode.Write);
//下面代码都一样了,主要是生成加密器
根据上面提供的参数,一定要生成KEYS数组的长度是32,IV数组的长度是16吗??那提示这个错误主要是什么原因??
你想把48位的16进制串,转换成32位的byte[]?这是不可能的事情。
DES的KEY,和IV都是8个字节长,也就是64位,如果你使用的是DES,那么你的Key和IV都用8个字节就OK了,就像我上面的例子。如果你想加强密钥,可以考虑用AES,高级加密标准,就是我在上面提到的Rijndael,可以使用更长的KEY。单就你的问题,我觉得你为什么要局限于你所输入的48位16进制串呢?
如果你的要求就是KEY的长度是24个字节(48位16进制串),那么你的加密就需要用高级加密标准,也就是要使用Rijndael。示例如下:
Rijndael alg = Rijndael.Create();
string raw = "86A659D3035B51B1B66DF3139F1AEC33F6651334F1E61280";
byte[] b3 = hexStringToBytes(raw);
alg.Key = b3;MemoryStream ms = new MemoryStream();
CryptoStream cs = new CryptoStream(ms, alg.CreateEncryptor(), CryptoStreamMode.Write);
StreamWriter sw = new StreamWriter(cs);
sw.Write(strText);
sw.Flush();
cs.FlushFinalBlock();
ms.Flush();
string result = Convert.ToBase64String(ms.GetBuffer(), 0, Convert.ToInt32(ms.Length)); hexStringToBytes:private byte[] hexStringToBytes(string hexString)
{
byte[] bytes = new byte[hexString.Length / 2 + (hexString.Length % 2 == 0 ? 0 : 1)];
for(int i = 0 ; i < bytes.Length ; i ++)
{
if(hexString.Length <= i*2 + 1)
{
bytes[i] = Convert.ToByte(Convert.ToInt32(hexString[i*2].ToString(), 16));
}
else
{
bytes[i] = Convert.ToByte(Convert.ToInt32(hexString.Substring(i*2, 2), 16));
}
} return bytes;
}
alg.Key = hexStringToBytes("86A659D3035B51B1B66DF3139F1AEC33F6651334F1E61280");
alg.IV = hexStringToBytes("0807060504030201");MemoryStream ms = new MemoryStream();
CryptoStream cs = new CryptoStream(ms, alg.CreateEncryptor(), CryptoStreamMode.Write);
StreamWriter sw = new StreamWriter(cs);
sw.Write(strText);
sw.Flush();
cs.FlushFinalBlock();
ms.Flush();
string result = Convert.ToBase64String(ms.GetBuffer(), 0, Convert.ToInt32(ms.Length));