【问题标题】:AES decrypted message not matching the original messageAES 解密消息与原始消息不匹配
【发布时间】:2018-05-27 05:36:34
【问题描述】:

我正在尝试使用AES256EncryptDecrypt 一个字符串。但是解密的字符串与原始字符串不匹配。我不确定,但也许我弄错了Encoding 部分。

我正在使用 CSPRNG 生成 IVPBDKF2 以生成用于 AES 加密的密钥

Program.cs

using System;
using System.Text;

namespace AESEncryptionUtility
{
    class Program
    {
        private static string _pass = "MasterPass";
        private static string _msg = "Mohit";
        private static byte[] key = EncryptionUtility.GenerateKey(_pass, 32);
        private static byte[] IV = EncryptionUtility.GenerateSalt(16);
        static void Main(string[] args)
        {
            Console.WriteLine("Hello World!");
            byte[] encrypted = Encrypt(_msg);
            byte[] decrypted = Decrypt(Encoding.ASCII.GetString(encrypted));
        }

        public static byte[] Encrypt(string msg)
        {
            byte[] asciiBytesOriginal = Encoding.ASCII.GetBytes(_msg);
            byte[] encrypted = EncryptionUtility.Encrypt(asciiBytesOriginal, key, IV);
            Console.WriteLine("encrypted started");
            foreach(var b in encrypted)
            {
                Console.Write(b + " ");
            }
            Console.WriteLine("\nencrypted ended");
            return encrypted;
        }

        public static byte[] Decrypt(string cipher)
        {
            byte[] asciiBytes = Encoding.ASCII.GetBytes(cipher);
            byte[] originalBytes = EncryptionUtility.Decrypt(asciiBytes, key, IV);
            Console.WriteLine("decrypted started");
            foreach(var b in originalBytes)
            {
                Console.Write(b + " ");
            }
            Console.WriteLine("\ndecrypted ended");
            string original = Encoding.ASCII.GetString(originalBytes);
            Console.WriteLine("original string: " + original);
            return originalBytes;
        }
    }
}

EncryptionUtility.cs

using System;
using System.Collections.Generic;
using System.IO;
using System.Security.Cryptography;
using System.Text;

namespace AESEncryptionUtility
{
    public static class EncryptionUtility
    {
        public static byte[] Encrypt(byte[] plainBytes, byte[] key, byte[] IV)
        {
            byte[] encrypted = null;
            using (AesCryptoServiceProvider aesAlgo = new AesCryptoServiceProvider())
            {
                aesAlgo.Key = key;
                aesAlgo.BlockSize = 128;
                aesAlgo.Mode = CipherMode.CBC;
                //aesAlgo.Padding = PaddingMode.PKCS7;
                aesAlgo.Padding = PaddingMode.Zeros;
                aesAlgo.IV = IV;
                ICryptoTransform encryptor = aesAlgo.CreateEncryptor();
                encrypted = encryptor.TransformFinalBlock(plainBytes, 0, plainBytes.Length);

            }
            return encrypted;
        }

        public static byte[] Decrypt(byte[] cipherBytes, byte[] key, byte[] IV)
        {
            byte[] decrypted = null;
            using (AesCryptoServiceProvider aesAlgo = new AesCryptoServiceProvider())
            {
                aesAlgo.Key = key;
                aesAlgo.BlockSize = 128;
                aesAlgo.Mode = CipherMode.CBC;
                //aesAlgo.Padding = PaddingMode.PKCS7;
                aesAlgo.Padding = PaddingMode.Zeros;
                aesAlgo.IV = IV;
                ICryptoTransform decryptor = aesAlgo.CreateDecryptor();
                decrypted = decryptor.TransformFinalBlock(cipherBytes, 0, cipherBytes.Length);
            }
            return decrypted;

        }
        public static byte[] GenerateKey(string masterPassword, int size) //size in bytes
        {
            byte[] salt = GenerateSalt(size);
            Rfc2898DeriveBytes pbfdk = new Rfc2898DeriveBytes(masterPassword, salt, 20000);
            return pbfdk.GetBytes(size);

        }

        public static byte[] GenerateSalt(int size) //size in bytes
        {
            RNGCryptoServiceProvider generator = new RNGCryptoServiceProvider();
            byte[] salt = new byte[size];
            generator.GetNonZeroBytes(salt);
            return salt;
        }

    }
}

【问题讨论】:

    标签: c# encryption aes encryption-symmetric


    【解决方案1】:

    您不能将任意二进制数据转换为字符串:

    byte[] decrypted = Decrypt(Encoding.ASCII.GetString(encrypted));
    

    并且希望它只是偶然地作为您选择的特定字符编码有意义。它不是那样工作的。加密算法对字节而不是字符串进行操作。如果您更改以下内容,您的代码将起作用:

    ...
    public static byte[] Decrypt(byte[] cipher)
    {
        byte[] asciiBytes = cipher;
        ...
    

    【讨论】:

      【解决方案2】:

      您是否尝试过将倒数第二行更改为:

      byte [] decrypted = Decrypt ( encrypted);
      

      【讨论】:

      • 是的,这是正确的,但没有解释为什么它是必要的,也没有解释你需要更改代码的其他部分才能做到这一点。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-07-22
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多