AES:输入数据不是完整的块

时间:2016-03-14 11:23:36

标签: c# cryptography aes aescryptoserviceprovider

使用AesCryptoServiceProvider解密内容时出现以下错误。我需要使用的模式是CipherMode.CFB,PaddingMode是PKCS7。我可以使用相同的密钥,IV,PaddingMode和Cipher模式在iOS中解密相同的内容。

我尝试以下列方式使用CryptoStream,但无法解密内容。

public byte[] DecryptWithAES(byte[] content, byte[] key, byte[] iv, int mode, int paddingMode)
{
    byte[] plainBytes = null;
    using (AesCryptoServiceProvider aes = new AesCryptoServiceProvider())
    {
        aes.BlockSize = 128;
        aes.KeySize = 256;

        aes.IV = iv;
        aes.Key = key;

        aes.Padding = (PaddingMode)paddingMode;
        aes.Mode = (CipherMode)mode;

        ICryptoTransform crypto = aes.CreateDecryptor(aes.Key, aes.IV);
        using (var input = new MemoryStream(content))
        {
            using (var output = new MemoryStream())
            {
                using (var cryptStream = new CryptoStream(input, crypto, CryptoStreamMode.Read))
                {
                    var buffer = new byte[1024];
                    var read = cryptStream.Read(buffer, 0, buffer.Length);
                    while (read > 0)
                    {
                        output.Write(buffer, 0, read);
                        read = cryptStream.Read(buffer, 0, buffer.Length);
                    }
                }

                    plainBytes = output.ToArray();
            }
        }

        return plainBytes;
    }
}

我也尝试过以下方式来解密内容但不起作用,

public byte[] DecryptWithAES(byte[] content, byte[] key, byte[] iv, int mode, int paddingMode)
{
    byte[] plainBytes = null;
    using (AesCryptoServiceProvider aes = new AesCryptoServiceProvider())
    {
        aes.BlockSize = 128;
        aes.KeySize = 256;

        aes.IV = iv;
        aes.Key = key;

        aes.Padding = (PaddingMode)paddingMode;
        aes.Mode = (CipherMode)mode;

        ICryptoTransform crypto = aes.CreateDecryptor(aes.Key, aes.IV);
        plainBytes = crypto.TransformFinalBlock(content, 0, content.Length);
    }

    return plainBytes;
}

在这两种情况下,填充模式都是PKCS7,而密码模式是CFB。

我在长度为22的加密内容上试了这个。 密钥长度:32, IV长度:16。

我昨天遇到了这个问题。请帮帮我。

2 个答案:

答案 0 :(得分:0)

最后,我最终使用Bouncy城​​堡库,并使用" AES / CFB / NoPadding"解密上述内容。解密内容无需CTR模式。

using Org.BouncyCastle.Crypto.Parameters;
using Org.BouncyCastle.Security;

private byte[] Decrypt(byte[] content, byte[] key, byte[] iv, string mode)
        {

            var cipher = CipherUtilities.GetCipher(mode);
            cipher.Init(false, new ParametersWithIV(new KeyParameter(key), iv));
            var blockBytes = cipher.ProcessBytes(content, 0, content.Length);
            var finalBytes = cipher.DoFinal();

            var plainBytes = new byte[content.Length];

            var counter = 0;
            if (blockBytes != null)
            {
                for (var i = 0; i < blockBytes.Length; i++)
                    plainBytes[counter++] = blockBytes[i];
            }

            if (finalBytes != null)
            {
                for (var i = 0; i < finalBytes.Length; i++)
                    plainBytes[counter++] = finalBytes[i];
            }

            return plainBytes;
        }

答案 1 :(得分:-1)

尝试

encrypted = encrypted.Replace(" ", "+");