写的一个控制台程序,使用的是TripleDESCryptoServiceProvider。提示用户输入密码,将密码当成密钥和初始向量IV。问题就出来了,运行的时候 输入密码后,弹出提示初始向量与算法的块大小不匹配。密钥整个是32位,24位做key,8位做初始化向量。于是我将24和8位分开输入,第一次输入24位,第二次输入8位。问问各位大神,有没有一次输入32位就解决问题的。下面是两次输入的代码:Console.WriteLine("Please enter the password :");string password =Console.ReadLine();byte[] bytes = System.Text.Encoding.ASCII.GetBytes(password);string iv = Console.ReadLine();byte[] bytes_IV = System.Text.Encoding.ASCII.GetBytes(iv); TripleDESCryptoServiceProvider TDES = new TripleDESCryptoServiceProvider();TDES.Key=bytes;TDES.IV=bytes_IV;
两者的块需求又不同,所以总是出错。分开输入就可以了。
string password = Console.ReadLine();
if (password.Length == 32)
{
byte[] bytes = System.Text.Encoding.ASCII.GetBytes(password.Substring(0, 24)); byte[] bytes_IV = System.Text.Encoding.ASCII.GetBytes(password.Substring(24)); //执行剩下操作
}
else
{
Console.WriteLine("请输入32位有效字符.");
}