c#调用一个des加密类出错,谁指点一下啊,提示指定键的大小对此算法无效
string[] strArray2 = new string[2];
ManagementClass class2 = new ManagementClass("Win32_NetworkAdapterConfiguration");
foreach (ManagementObject obj2 in class2.GetInstances())
{
if ((bool)obj2["IPEnabled"])
{
str4 = obj2["MacAddress"].ToString();
}
obj2.Dispose();
}
调用类的地方:
strArray2 = security.Decode("yygmldcsjmdsthcg", "aaaa", "bbbb").Replace("\r\n", "|").Split(new char[] { '|' }); 调用后提示:指定键的大小对此算法无效
类的两个函数:,谁能看懂这两个函数呢,希望可以解释一下.多谢了!!!
using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Security.Cryptography;
using System.Management;
public class Security
{
public string Decode(string data, string KEY_64, string IV_64)
{
byte[] buffer3;
byte[] bytes = Encoding.ASCII.GetBytes(KEY_64);
byte[] rgbIV = Encoding.ASCII.GetBytes(IV_64);
try
{
buffer3 = Convert.FromBase64String(data);
}
catch
{
return null;
}
DESCryptoServiceProvider provider = new DESCryptoServiceProvider();
MemoryStream stream = new MemoryStream(buffer3);
CryptoStream stream2 = new CryptoStream(stream, provider.CreateDecryptor(bytes, rgbIV), CryptoStreamMode.Read);
StreamReader reader = new StreamReader(stream2);
return reader.ReadToEnd();
}
public string Encode(string data, string KEY_64, string IV_64)
{
byte[] bytes = Encoding.ASCII.GetBytes(KEY_64);
byte[] rgbIV = Encoding.ASCII.GetBytes(IV_64);
DESCryptoServiceProvider provider = new DESCryptoServiceProvider();
int keySize = provider.KeySize;
MemoryStream stream = new MemoryStream();
CryptoStream stream2 = new CryptoStream(stream, provider.CreateEncryptor(bytes, rgbIV), CryptoStreamMode.Write);
StreamWriter writer = new StreamWriter(stream2);
writer.Write(data);
writer.Flush();
stream2.FlushFinalBlock();
writer.Flush();
return Convert.ToBase64String(stream.GetBuffer(), 0, (int)stream.Length);
}
}