【问题标题】:System.Security.Cryptography.CryptographicException: 'The input data is not a complete block.'System.Security.Cryptography.CryptographicException:'输入数据不是一个完整的块。'
【发布时间】:2021-04-22 04:09:40
【问题描述】:

我是解密新手,尝试像以前版本的 ColdFusion 一样解密。我收到了这个错误,不知道是什么意思。

string cipherText = "text";
//this is from coldfusion
String key = "key";
//var AES_algorithm = "AES";
//var AES_encoding = "hex";
//<cfset Result = #Decrypt(val, AES_Private_Key, AES_algorithm, AES_encoding)#>

if (cipherText == null || cipherText.Length <= 0)
    throw new ArgumentNullException("plainText");

//not sure what this is?
String iv = "1020304050607080";

if (key == null || key.Length <= 0)
    throw new ArgumentNullException("Key");

if (iv == null || iv.Length <= 0)
    throw new ArgumentNullException("IV");

byte[] bytearraytodecrypt = Convert.FromBase64String(cipherText);

AesCryptoServiceProvider keydecrypt = new AesCryptoServiceProvider();
keydecrypt.BlockSize = 128;
keydecrypt.KeySize = 128;
keydecrypt.Key = System.Text.Encoding.UTF8.GetBytes(key);
keydecrypt.IV = System.Text.Encoding.UTF8.GetBytes(iv);
keydecrypt.Padding = PaddingMode.PKCS7;
keydecrypt.Mode = CipherMode.CBC;

ICryptoTransform crypto1 = keydecrypt.CreateDecryptor(keydecrypt.Key, keydecrypt.IV);

byte[] returnbytearray = crypto1.TransformFinalBlock(bytearraytodecrypt, 0, bytearraytodecrypt.Length);
crypto1.Dispose();
return System.Text.Encoding.UTF8.GetString(returnbytearray);

【问题讨论】:

  • 请查看我编辑中的代码格式,以供以后的帖子参考。 Stack Overflow 使用反引号字符进行代码格式化,3 用于块,1 用于段落内。
  • 我认为我做得对。下次我会更加努力的。

标签: c# encryption


【解决方案1】:

它的意思是它所说的。在 CBC 模式下,明文首先填充到完整数量的块中。这总是发生在 PKCS#7 填充中。然后将每个明文块依次加密,以之前加密的块为向量;该块与明文进行异或运算。

需要提供初始向量 (IV),对于 CBC 模式,它需要对攻击者完全不可预测。这通常意味着它应该由随机字节组成。一个常量的编码字符串不满足这个要求。

当然,结果由密文块组成。现在,如果解密例程遇到的字节数不是块大小的倍数,您将收到错误消息。这意味着消息要么有额外的数据,要么没有正确编码。这也可能意味着使用了与 CBC 不同的模式,甚至是带有密文窃取的 CBC。

【讨论】:

  • 非常感谢。
  • 不客气。对 SO 表示感谢的最佳方式是接受答案 - 如果不接受,请向回答者说明。很高兴详细介绍。
  • 我不知道如何接受这个答案。我在任何地方都没有看到。我可能只是瞎了,哈哈。
  • @CharlotteWilliams 这是答案旁边的灰色 V,在左边 :) 当然也出现在 help of SO
猜你喜欢
  • 2020-06-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-01-02
  • 1970-01-01
  • 1970-01-01
  • 2019-12-22
相关资源
最近更新 更多