我尝试实现此代码来加密和解密流。一切正常,直到我实现缓冲区。与我尝试解密时相比,会抛出“无效填充”错误。
有谁知道为什么会这样。或者是否有另一种加密/解密流和使用缓冲区的方法?
public static void EncryptStringToBytes_Aes(Stream source, Stream target, byte[] Key, byte[] IV, int iterations, int buffer = 4096)
{
// Check arguments.
if (Key == null || Key.Length <= 0)
throw new ArgumentNullException("Key");
if (IV == null || IV.Length <= 0)
throw new ArgumentNullException("IV");
// Create an Aes object
// with the specified key and IV.
using (System.Security.Cryptography.Aes aesAlg = System.Security.Cryptography.Aes.Create())
{
aesAlg.Key = Key;
aesAlg.IV = IV;
// Create a decryptor to perform the stream transform.
ICryptoTransform encryptor = aesAlg.CreateEncryptor(aesAlg.Key, aesAlg.IV);
Rfc2898DeriveBytes iterator = new Rfc2898DeriveBytes(Encoding.ASCII.GetString(Key), 64, iterations);
BinaryFormatter formatter = new BinaryFormatter();
formatter.Serialize(target, iterations);
formatter.Serialize(target, IV);
formatter.Serialize(target, iterator.Salt);
Key = iterator.GetBytes(64);
// Create the streams used for encryption.
using (CryptoStream csEncrypt = new CryptoStream(target, encryptor, CryptoStreamMode.Write))
using (BinaryReader reader = new BinaryReader(source))
using (BinaryWriter writer = new BinaryWriter(csEncrypt))
//Write all data to the stream.
while (reader.BaseStream.Position != reader.BaseStream.Length)
writer.Write(reader.ReadBytes(buffer));
}
}
public static void DecryptStringFromBytes_Aes(Stream source, Stream target, byte[] Key, int buffer = 4096)
{
// Check arguments.
if (Key == null || Key.Length <= 0)
throw new ArgumentNullException("Key");
// Create an Aes object
// with the specified key and IV.
using (System.Security.Cryptography.Aes aesAlg = System.Security.Cryptography.Aes.Create())
{
BinaryFormatter formatter = new BinaryFormatter();
int iterations = (int)formatter.Deserialize(source);
aesAlg.IV = formatter.Deserialize(source) as byte[];
byte[] salt = formatter.Deserialize(source) as byte[];
Rfc2898DeriveBytes iterator = new Rfc2898DeriveBytes(Key, salt, iterations);
aesAlg.Key = iterator.GetBytes(32);
// Create a decryptor to perform the stream transform.
ICryptoTransform decryptor = aesAlg.CreateDecryptor(aesAlg.Key, aesAlg.IV);
// Create the streams used for decryption.
using (CryptoStream csDecrypt = new CryptoStream(source, decryptor, CryptoStreamMode.Read))
using (BinaryReader reader = new BinaryReader(csDecrypt))
using (BinaryWriter writer = new BinaryWriter(target))
// Read the decrypted bytes from the decrypting stream
// and place them in a string.
while (source.Position != source.Length)
writer.Write(reader.ReadBytes(buffer));
}
}