-1

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

我尝试通过以下方式使用 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;
}

在这两种情况下,Padding Mode 都是 PKCS7,CipherMode 是 CFB。

我对长度为 22 的加密内容进行了尝试。密钥长度:32,IV 长度:16。

我从昨天开始就被这个问题困住了。请帮我。

4

2 回答 2

0

最后我最终使用了 Bouncy castle 库,上面的内容是使用“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;
        }
于 2016-03-20T08:09:14.980 回答
-1

尝试

encrypted = encrypted.Replace(" ", "+");
于 2019-07-08T15:15:05.197 回答