【问题标题】:Specified padding mode is not valid for this algorithm when using AES & PKCS#7 padding in .Net core 2.0在 .Net core 2.0 中使用 AES 和 PKCS#7 填充时,指定的填充模式对此算法无效
【发布时间】:2018-06-23 22:18:40
【问题描述】:

我花了一整天的时间对此进行调查,并在 Stack Overflow 上搜索所有相关问题以查找此问题,因此请不要提及可能的重复项。

下面的代码给了我一个 System.Security.Cryptography.CryptographicException: 'Specified padding mode is not valid for this algorithm。'

虽然在本网站上使用相同的参数:http://aes.online-domain-tools.com,但它完美地解密为“Hello world”,然后填充五个“x05”字节用于填充(PKCS#7 填充)。

但是下面的代码在调用TransformFinalBlock()时总是会产生异常

上下文: 使用 .NET Core 2.0 在 Win8.1 上运行的控制台应用程序 / 算法是 AES / CBC / padding PKCS#7

我也在这里尝试了建议的解决方案:Specified padding mode is not valid for this algorithm - c# - System.Security.Cryptography 但没有成功(我也不明白为什么如果已经在 SymmetricAlgorithm 实例中设置了 IV,则应该稍后在解密时使用它?

    static void Main(string[] args)
    {
        string encryptedStr = "e469acd421dd71ade4937736c06fdc9d";
        string passphraseStr = "1e089e3c5323ad80a90767bdd5907297b4138163f027097fd3bdbeab528d2d68";
        string ivStr = "07dfd3f0b90e25e83fd05ba338d0be68";

        // Convert hex strings to their ASCII representation
        ivStr = HexStringToString(ivStr);
        passphraseStr = HexStringToString(passphraseStr);
        encryptedStr = HexStringToString(encryptedStr);

        // Convert our ASCII strings to byte arrays
        byte[] encryptedBytes = Encoding.ASCII.GetBytes(encryptedStr);
        byte[] key = Encoding.ASCII.GetBytes(passphraseStr);
        byte[] iv = Encoding.ASCII.GetBytes(ivStr);

        // Configure our AES decryptor
        SymmetricAlgorithm algorithm = Aes.Create();
        algorithm.Mode = CipherMode.CBC;
        algorithm.Padding = PaddingMode.PKCS7;
        algorithm.KeySize = 256;
        //algorithm.BlockSize = 128;
        algorithm.Key = key;
        algorithm.IV = iv;

        Console.WriteLine("IV length " + iv.Length);            // 16
        Console.WriteLine("Key length " + key.Length);          // 32

        ICryptoTransform transform = algorithm.CreateDecryptor(algorithm.Key, algorithm.IV);

        // Perform decryption
        byte[] outputBuffer = transform.TransformFinalBlock(encryptedBytes, 0, encryptedBytes.Length);

        // Convert it back to a string
        string result = Encoding.ASCII.GetString(outputBuffer);

        Console.WriteLine(result);
        Console.ReadLine();
    }

    public static string HexStringToString(string hexString)
    {
        var sb = new StringBuilder();
        for (var i = 0; i < hexString.Length; i += 2)
        {
            var hexChar = hexString.Substring(i, 2);
            sb.Append((char)Convert.ToByte(hexChar, 16));
        }

        return sb.ToString();
    }

【问题讨论】:

    标签: c# .net encryption .net-core aes


    【解决方案1】:

    问题在于如何将十六进制字符串转换为字节数组。尝试调试您的代码并检查数组encryptedBytes 的值。您将看到以下数组:

    { 0x3f, 0x69, 0x3f, 0x3f, 0x21, 0x3f, 0x71, 0x3f, 0x3f, 0x3f, 0x77, 0x36, 0x3f, 0x6f, 0x3f, 0x3f }
    

    这与输入 e469acd421dd71ade4937736c06fdc9d 相差甚远。

    您不应将 System.String 对象仅用作二进制字符代码的持有者,因为 .Net 字符串是 UTF16 编码的。

    现在,当根本原因明确后,修复就非常简单了。更改您的 HexStringToString 方法,使其直接将十六进制字符串转换为字节数组:

    public static byte[] HexStringToByteArray(string hexString)
    {
        if (hexString.Length % 2 != 0)
        {
            throw new InvalidOperationException($"Inalid hex string '{hexString}'");
        }
    
        byte[] bytes = new byte[hexString.Length / 2];
        for (var i = 0; i < hexString.Length; i += 2)
        {
            var hexChar = hexString.Substring(i, 2);
            bytes[i / 2] = Convert.ToByte(hexChar, 16);
        }
    
        return bytes;
    }
    

    然后调整Main()中的代码:

    byte[] encryptedBytes = HexStringToByteArray(encryptedStr);
    byte[] key = HexStringToByteArray(passphraseStr);
    byte[] iv = HexStringToByteArray(ivStr);
    

    这将在result 变量中为您提供所需的Hello world

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-03-13
      • 2017-04-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-01-27
      • 2013-11-14
      相关资源
      最近更新 更多