我想using System.Security.Cryptography;实现AES的加密解密功能,加密可以正常进行
解密时红色地方的语句会出现异常。
如果算法用的DES就没这个问题,请问这个异常怎么解决,微软官方上都没解决方案,国外一些提问网站也没解决。不知道谁能告诉我怎么做
//AES加密
public static string EnAES(string str, string encryptKey, string encryptVector)
{
int keySize = encryptKey.Length * 8;
AesCryptoServiceProvider aes = new AesCryptoServiceProvider();
if (aes.ValidKeySize(keySize) == false)
{
keySize = Convert.ToInt32(aes.LegalKeySizes[0].MaxSize);
if (keySize / 8 > encryptKey.Length)
encryptKey = encryptKey.PadRight(keySize / 8, '0');
else
encryptKey = encryptKey.Substring(0, keySize / 8);
}
if (aes.BlockSize / 8 > encryptVector.Length)
encryptVector = encryptVector.PadRight(aes.BlockSize / 8, '0');
else
encryptVector = encryptVector.Substring(0, aes.BlockSize / 8);
byte[] key = Encoding.ASCII.GetBytes(encryptKey);
byte[] data = Encoding.Unicode.GetBytes(str);
byte[] vector = Encoding.ASCII.GetBytes(encryptVector);
System.IO.MemoryStream MStream = new System.IO.MemoryStream();
CryptoStream CStream = new CryptoStream(MStream, aes.CreateEncryptor(key, vector), CryptoStreamMode.Write);
CStream.Write(data, 0, data.Length);
CStream.FlushFinalBlock();
return Convert.ToBase64String(MStream.ToArray());
}
//AES解密
public static string DeAES(string str, string encryptKey, string encryptVector)
{
int keySize = encryptKey.Length * 8;
AesCryptoServiceProvider aes = new AesCryptoServiceProvider();
if (aes.ValidKeySize(keySize) == false)
{
keySize = Convert.ToInt32(aes.LegalKeySizes[0].MaxSize);
if (keySize / 8 > encryptKey.Length)
encryptKey = encryptKey.PadRight(keySize / 8, '0');
else
encryptKey = encryptKey.Substring(0, keySize / 8);
}
if (aes.BlockSize / 8 > encryptVector.Length)
encryptVector = encryptVector.PadRight(aes.BlockSize / 8, '0');
else
encryptVector = encryptVector.Substring(0, aes.BlockSize / 8);
byte[] key = Encoding.ASCII.GetBytes(encryptKey);
byte[] data = Encoding.Unicode.GetBytes(str);
byte[] vector = Encoding.ASCII.GetBytes(encryptVector);
System.IO.MemoryStream MStream = new System.IO.MemoryStream(data.Length+256);
CryptoStream CStream = new CryptoStream(MStream, aes.CreateDecryptor(key, vector), CryptoStreamMode.Write);
CStream.Write(data, 0, data.Length);
//出exception的语句
CStream.FlushFinalBlock();
return Convert.ToBase64String(MStream.ToArray());
}
但是下面的代码能够成功
public static string Decrypt(string toDecrypt)
{
byte[] keyArray = UTF8Encoding.UTF8.GetBytes("12345678901234567890123456789012");
byte[] toEncryptArray = Convert.FromBase64String(toDecrypt);
RijndaelManaged rDel = new RijndaelManaged();
rDel.Key = keyArray;
rDel.Mode = CipherMode.ECB;
rDel.Padding = PaddingMode.PKCS7;
ICryptoTransform cTransform = rDel.CreateDecryptor();
byte[] resultArray = cTransform.TransformFinalBlock(toEncryptArray, 0, toEncryptArray.Length);
return UTF8Encoding.UTF8.GetString(resultArray);
}
public static string Encrypt(string toEncrypt)
{
byte[] keyArray = UTF8Encoding.UTF8.GetBytes("12345678901234567890123456789012");
byte[] toEncryptArray = UTF8Encoding.UTF8.GetBytes(toEncrypt);
RijndaelManaged rDel = new RijndaelManaged();
rDel.Key = keyArray; rDel.Mode = CipherMode.ECB; rDel.Padding = PaddingMode.PKCS7;
ICryptoTransform cTransform = rDel.CreateEncryptor();
byte[] resultArray = cTransform.TransformFinalBlock(toEncryptArray, 0, toEncryptArray.Length);
return Convert.ToBase64String(resultArray, 0, resultArray.Length);
}
请问可以不用下面的代码 修改第一段代码完成AES加密的任务吗?
还有我想知道问题出在哪里
我做过的尝试:
不是内存流不够大MemoryStream我试过给予很大的容量,尝试设置AesCryptoServiceProvider的padding和mode均无效。
如果第一段的代码的AesCryptoServiceProvider该做DESCryptoServiceProvider 那么代码也能运行成功。
请问问题出在哪里?