输入数据不是 CFB 模式 AES-128 加密 C# 的完整块
The input data is not a complete block for CFB mode AES-128 encryption C#
我正在尝试使用 AES-128、CFB 模式加密数据。下面的代码块适用于 dotnet 5.x
目标框架,但给出
The input data is not a complete block
dotnet 4.7.2
在 bw.write()
处出错。理想情况下,它应该工作,因为 CFB 模式将块密码转换为不需要填充的流密码。我也试过 csEncrypt.Write(dataToBeEncrypted, 0, dataToBeEncrypted.Length)
而不是 bw.Write()
。在这种情况下,它在下一行失败,将内存流转换为数组并出现相同的错误。
using (AesCryptoServiceProvider Aes128 = new AesCryptoServiceProvider())
{
Aes128.BlockSize = 128;
Aes128.KeySize = 128;
//
// Specify CFB8 mode
//
Aes128.Mode = CipherMode.CFB;
Aes128.FeedbackSize = 8;
Aes128.Padding = PaddingMode.None;
//
// Generate and save random key and IV.
//
Aes128.GenerateIV();
Aes128.Key = key;
Aes128.IV.CopyTo(savedIV, 0);
using (var encryptor = Aes128.CreateEncryptor())
using (var msEncrypt = new MemoryStream())
using (var csEncrypt = new CryptoStream(msEncrypt, encryptor, CryptoStreamMode.Write))
using (var bw = new BinaryWriter(csEncrypt, Encoding.UTF8))
{
bw.Write(dataToBeEncrypted);
bw.Close();
cipherBytes = msEncrypt.ToArray();
}
问题已通过删除 Aes128.Padding = PaddingMode.None 得到解决;并让它由 PKC7 填充。正如@Topaco 所回答的那样,我将密文修剪为明文长度以删除填充 post 加密。
更新代码:
using (AesCryptoServiceProvider Aes128 = new AesCryptoServiceProvider())
{
Aes128.BlockSize = 128;
Aes128.KeySize = 128;
//
// Specify CFB8 mode
//
Aes128.Mode = CipherMode.CFB;
Aes128.FeedbackSize = 8;
//
// Generate and save random key and IV.
//
Aes128.GenerateIV();
Aes128.Key = key;
Aes128.IV.CopyTo(savedIV, 0);
using (var encryptor = Aes128.CreateEncryptor())
using (var msEncrypt = new MemoryStream())
using (var csEncrypt = new CryptoStream(msEncrypt, encryptor, CryptoStreamMode.Write))
using (var bw = new BinaryWriter(csEncrypt, Encoding.UTF8))
{
Console.WriteLine("About to binary write.");
bw.Write(dataToBeEncrypted);
bw.Close();
Console.WriteLine("binary write successful.");
cipherBytesWithPadding = msEncrypt.ToArray();
}
Buffer.BlockCopy(cipherBytesWithPadding, 0, cipherBytes, 0, dataToBeEncrypted.Length);
我正在尝试使用 AES-128、CFB 模式加密数据。下面的代码块适用于 dotnet 5.x
目标框架,但给出
The input data is not a complete block
dotnet 4.7.2
在 bw.write()
处出错。理想情况下,它应该工作,因为 CFB 模式将块密码转换为不需要填充的流密码。我也试过 csEncrypt.Write(dataToBeEncrypted, 0, dataToBeEncrypted.Length)
而不是 bw.Write()
。在这种情况下,它在下一行失败,将内存流转换为数组并出现相同的错误。
using (AesCryptoServiceProvider Aes128 = new AesCryptoServiceProvider())
{
Aes128.BlockSize = 128;
Aes128.KeySize = 128;
//
// Specify CFB8 mode
//
Aes128.Mode = CipherMode.CFB;
Aes128.FeedbackSize = 8;
Aes128.Padding = PaddingMode.None;
//
// Generate and save random key and IV.
//
Aes128.GenerateIV();
Aes128.Key = key;
Aes128.IV.CopyTo(savedIV, 0);
using (var encryptor = Aes128.CreateEncryptor())
using (var msEncrypt = new MemoryStream())
using (var csEncrypt = new CryptoStream(msEncrypt, encryptor, CryptoStreamMode.Write))
using (var bw = new BinaryWriter(csEncrypt, Encoding.UTF8))
{
bw.Write(dataToBeEncrypted);
bw.Close();
cipherBytes = msEncrypt.ToArray();
}
问题已通过删除 Aes128.Padding = PaddingMode.None 得到解决;并让它由 PKC7 填充。正如@Topaco
更新代码:
using (AesCryptoServiceProvider Aes128 = new AesCryptoServiceProvider())
{
Aes128.BlockSize = 128;
Aes128.KeySize = 128;
//
// Specify CFB8 mode
//
Aes128.Mode = CipherMode.CFB;
Aes128.FeedbackSize = 8;
//
// Generate and save random key and IV.
//
Aes128.GenerateIV();
Aes128.Key = key;
Aes128.IV.CopyTo(savedIV, 0);
using (var encryptor = Aes128.CreateEncryptor())
using (var msEncrypt = new MemoryStream())
using (var csEncrypt = new CryptoStream(msEncrypt, encryptor, CryptoStreamMode.Write))
using (var bw = new BinaryWriter(csEncrypt, Encoding.UTF8))
{
Console.WriteLine("About to binary write.");
bw.Write(dataToBeEncrypted);
bw.Close();
Console.WriteLine("binary write successful.");
cipherBytesWithPadding = msEncrypt.ToArray();
}
Buffer.BlockCopy(cipherBytesWithPadding, 0, cipherBytes, 0, dataToBeEncrypted.Length);