【发布时间】:2018-05-13 11:52:14
【问题描述】:
我得到错误“输入数据不是一个完整的块”,我不知道我的代码是错误的还是缺少什么。我尝试用相同的长度加密/解密字节。
=byte[] plain => MyEnc(plain) => byte[] encrypted => MyDec(encrypt) => byte[] plain
明文和加密的长度相同。
这是我的加密代码:
public static byte[] MyEnc(byte[] Input)
{
byte[] inputencdec = Input;
byte[] encrypted;
using (MemoryStream mstream = new MemoryStream())
{
using (AesCryptoServiceProvider encdec = new AesCryptoServiceProvider())
{
encdec.BlockSize = 128;
encdec.KeySize = 256;
encdec.Key = ASCIIEncoding.ASCII.GetBytes(Key);
encdec.IV = ASCIIEncoding.ASCII.GetBytes(IV);
ICryptoTransform icrypt = encdec.CreateEncryptor(encdec.Key, encdec.IV);
using (CryptoStream cryptoStream = new CryptoStream(mstream,
icrypt, CryptoStreamMode.Write))
{
cryptoStream.Write(inputencdec, 0, inputencdec.Length);
}
}
encrypted = mstream.ToArray();
}
return encrypted;
}
这是我的解密代码:
public static byte[] MyDec(byte[] Input)
{
byte[] inputencdec = Input;
byte[] buffer = new byte[Input.Length];
int totalRead = 0;
byte[] plain;
MemoryStream plainStream = new MemoryStream();
using (MemoryStream mStream = new MemoryStream(inputencdec))
{
using (AesCryptoServiceProvider encdec = new AesCryptoServiceProvider())
{
encdec.BlockSize = 128;
encdec.KeySize = 256;
encdec.Key = ASCIIEncoding.ASCII.GetBytes(Key);
encdec.IV = ASCIIEncoding.ASCII.GetBytes(IV);
ICryptoTransform icrypt = encdec.CreateDecryptor(encdec.Key, encdec.IV);
using (CryptoStream cryptoStream = new CryptoStream(mStream, icrypt, CryptoStreamMode.Read))
{
while (true)
{
int read = cryptoStream.Read(buffer, 0, inputencdec.Length);
if (read == 0)
break;
else
plainStream.Write(buffer, totalRead, read);
totalRead += read;
}
}
}
plain = plainStream.ToArray();
}
return plain;
}
【问题讨论】:
-
最好(甚至策略)使用 C# 作为标签而不是标题。它将提高您问题的曝光率,并自动突出显示您的代码。请始终提供错误的完整堆栈跟踪以及发生异常的代码中的提示,因为我们没有行号。
-
int read = cryptoStream.Read(buffer, 0, inputencdec.Length);处的错误代码如果明文和密码的长度不同。所以我不能让两者的长度相同?或使用另一种密码模式?我想尝试什么,例如,如果我有 50k 字节并且我解析 20k-40k 的字节。并且只需加密/解密解析字节并再次添加到该文件。 -
您可以使用流式操作模式,例如计数器模式。这是最常见的流媒体模式,但由于某些脑死亡的原因,它不适用于 .NET(很多 Mickeysoft 加密 API 以这种方式脑死亡,我猜他们缺少与开发人员、开发人员、开发人员的联系......但是有 CFB 模式,这是一种不需要填充的旧模式。但我现在可以看到缓冲区与密文的大小相同;这意味着您的密文大小错误;这意味着密文的生成很可能搞砸了。
-
所以 CFB 模式会使plain 和 cipher 具有相同的字节长度?
-
是的,如果您可以导出 IV 或更改每条消息的密钥。例如,IV 可以兼作消息编号以避免重放攻击。请注意,CBC 和 CFB 都不提供真实性/完整性;你需要例如GCM 为此,但它在密文中添加了一个身份验证标签。
标签: c# encryption cryptography aes bytebuffer