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

我有 C# 代码正在写入文本文件,其中用户输入的密码使用 EasyEncryption 通过 AES 进行加密。我将密钥和 iv 都设置为 128 位,并且加密工作正常。但是,当我尝试解密密码时,我不断收到不完整的块消息。

        //PASSWORD IS BEING ENCRYPTED BEFORE SAVING TO TEXT FILE
        var keyValue = "/A?D(G+KbPeShVkY";
        var ivValue = "*G-KaPdSgVkYp3s5";
        var pEncryption = EasyEncryption.AES.Encrypt(PASSWORD, keyValue, ivValue);
        Console.WriteLine("\nPassword encrypted= " + pEncryption);
        file.WriteLine("password=" + pEncryption);

这是给我块错误的解密部分

        //PASSWORD DECRYPTION
        var keyValue = "/A?D(G+KbPeShVkY";
        var ivValue = "*G-KaPdSgVkYp3s5";
        var dcryPassword = EasyEncryption.AES.Decrypt(data["password"],keyValue, ivValue);

【问题讨论】:

  • 这个:stackoverflow.com/questions/19614178/… 有帮助吗?
  • 尝试使用@Charles 共享的代码,如果可行,请检查您的数据[“密码”] 的转换位置,就像我的其他评论中的示例一样。

标签: c# encryption aes incomplete-type


【解决方案1】:

我遇到过类似的问题,并通过观察 decryt() 方法的输入文本有时它格式错误来解决。

你也可以检查一下。

【讨论】:

    【解决方案2】:

    好的,我终于想出了一个解决方案。我试图直接从默认为 UTF8 的记事本文件中解密密码。我使用名为 rpass(读取密码)的字符串读取加密密码,然后使用 rpass 进行解密。现在可以正常工作。

    //Need to read from text UTF8 before trying to decrypt, == is padding
                string rpass = data["password"] + "==";
                Console.WriteLine(rpass);
                string dpass = Decrypt(rpass);
                Console.WriteLine("decryption= " + dpass);
    

    【讨论】:

    • +== 是添加填充。我正在读取的文本文件是用 = 分隔值。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-01-02
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多