我正在使用AES加密,解密时对DB进行加密和写入时没有问题,它返回null。
密钥和IV在加密时相同,并且还检查了填充,在执行加密和解密时它们相同。
public byte[] Encrypt(string plainText, byte[] Key, byte[] IV)
{
byte[] password;
// Create a new AesManaged.
using (AesManaged aes = new AesManaged())
{
// Create encryptor
ICryptoTransform encryptor = aes.CreateEncryptor(Key, IV);
// Create MemoryStream
using (MemoryStream ms = new MemoryStream())
{
// Create crypto stream using the CryptoStream class. This class is the key to encryption
// and encrypts and decrypts data from any given stream. In this case, we will pass a memory stream
// to encrypt
using (CryptoStream cs = new CryptoStream(ms, encryptor, CryptoStreamMode.Write))
{
// Create StreamWriter and write data to a stream
using (StreamWriter sw = new StreamWriter(cs))
sw.Write(plainText);
password = ms.ToArray();
}
}
}
}
public static string Decrypt(byte[] cipherText, byte[] Key, byte[] IV)
{
string plaintext = null;
// Create AesManaged
using (AesManaged aes = new AesManaged())
{
// Create a decryptor
ICryptoTransform decryptor = aes.CreateDecryptor(Key, IV);
// Create the streams used for decryption.
using (MemoryStream ms = new MemoryStream(cipherText))
{
// Create crypto stream
using (CryptoStream cs = new CryptoStream(ms, decryptor, CryptoStreamMode.Read))
{
// Read crypto stream
using (StreamReader reader = new StreamReader(cs))
plaintext = reader.ReadToEnd(); // Error is here , Throws exception "Padding is invalid and cannot be removed."
}
}
}
return plaintext;
}
Result : Decryption is achieved as expected
答案 0 :(得分:3)
您的代码有一些语法错误。除此之外,它还在工作。
public static void Main(string[] args)
{
string plainText = "Here is somewe data to encrypt!";
using (Aes myAes = Aes.Create())
{
byte[] cipherText = Encrypt(plainText, myAes.Key, myAes.IV);
plainText = Decrypt(cipherText, myAes.Key, myAes.IV);
Console.WriteLine(plainText);
}
}
public static byte[] Encrypt(string plainText, byte[] Key, byte[] IV)
{
byte[] encrypted;
// Create a new AesManaged.
using (AesManaged aes = new AesManaged())
{
// Create encryptor
ICryptoTransform encryptor = aes.CreateEncryptor(Key, IV);
// Create MemoryStream
using (MemoryStream ms = new MemoryStream())
{
// Create crypto stream using the CryptoStream class. This class is the key to encryption
// and encrypts and decrypts data from any given stream. In this case, we will pass a memory stream
// to encrypt
using (CryptoStream cs = new CryptoStream(ms, encryptor, CryptoStreamMode.Write))
{
// Create StreamWriter and write data to a stream
using (StreamWriter sw = new StreamWriter(cs))
{
//Write all data to the stream.
sw.Write(plainText);
}
encrypted = ms.ToArray();
}
}
}
// Return the encrypted bytes from the memory stream.
return encrypted;
}
public static string Decrypt(byte[] cipherText, byte[] Key, byte[] IV)
{
string plaintext = string.Empty;
// Create AesManaged
using (AesManaged aes = new AesManaged())
{
// Create a decryptor
ICryptoTransform decryptor = aes.CreateDecryptor(Key, IV);
// Create the streams used for decryption.
using (MemoryStream ms = new MemoryStream(cipherText))
{
// Create crypto stream
using (CryptoStream cs = new CryptoStream(ms, decryptor, CryptoStreamMode.Read))
{
// Read crypto stream
using (StreamReader reader = new StreamReader(cs))
{
plaintext = reader.ReadToEnd();
}
}
}
}
return plaintext;
}
在线演示:https://rextester.com/NMJZ99435
有关更多信息,请参阅此文档:https://docs.microsoft.com/en-us/dotnet/api/system.security.cryptography.aes?view=netframework-4.7.2