【问题标题】:How to code a decryption method for the encryption method here in c#如何在c#中为这里的加密方法编写解密方法
【发布时间】:2012-02-09 11:35:40
【问题描述】:

我能够将如下所示的加密示例放在一起,但在解密期间我得到无效数据(异常)。我应该如何解密

加密方式

public static string EncryptWithAes(string plainText, byte[] key, byte[] initiationVector)
        {

            byte[] cryptoBytes = Encoding.UTF8.GetBytes(plainText);

            using (RijndaelManaged aesAlgorithm = new RijndaelManaged())
            {
                aesAlgorithm.Key = key;
                aesAlgorithm.IV = initiationVector;
                aesAlgorithm.Mode = CipherMode.ECB;
                using (ICryptoTransform encryptoTransform = aesAlgorithm.CreateEncryptor(aesAlgorithm.Key, aesAlgorithm.IV))
                {
                    cryptoBytes = encryptoTransform.TransformFinalBlock(cryptoBytes, 0, cryptoBytes.Length);
                }
            }
            return Convert.ToBase64String(cryptoBytes);
        }

解密方法

 public static string DecryptAesCryptoString(string cipherText, byte[] key, byte[] initiationVector)
        {
            byte[] decryptedByte;
            using (RijndaelManaged aesAlgorithm = new RijndaelManaged())
            {
                aesAlgorithm.Key = key;
                aesAlgorithm.IV = initiationVector;
                aesAlgorithm.Mode = CipherMode.ECB;

                using (ICryptoTransform decryptoTransform = aesAlgorithm.CreateDecryptor(aesAlgorithm.Key, aesAlgorithm.IV))
                {
                    byte[] cipherBytes = Convert.FromBase64String(cipherText);
                    decryptedByte = decryptoTransform.TransformFinalBlock(cipherBytes, 0, cipherBytes.Length);

                }
            }
            return Encoding.UTF8.GetString(decryptedByte);
        }

我认为问题在于这些方法中的所有编码

样本数据

plainText = 堆栈溢出

base64encoded Key = B8Y/6doxwqU870C6jzYWhsr3hKSLokAOkkLCDiy+TS4= (应该很容易转换成字节吧)

base64encoded IV = NZIpD60eBmdsOFFhA2bfvw==

encryptedValue = 77+977+977+977+977+9Ce+/ve+/vQ3vv70F77+9UzHvv73vv70=

我向 Stackoverflow 提供相同的加密值、IV 和密钥来解密

【问题讨论】:

  • 也许是愚蠢的问题,但你用什么值测试它?
  • 不不,我指的是我自己的问题
  • 使用堆栈跟踪发布异常并标记错误行

标签: c# .net aes sample rijndael


【解决方案1】:

我认为你的问题是你的 IV 的长度,也许是关键。我记得 IV 应该是 16 字节长,密钥有不同的选项,你应该查一下。

//测试:

        RijndaelManaged alg = new RijndaelManaged();

        alg.GenerateKey();
        alg.GenerateIV();

        byte[] key = alg.Key;
        byte[] iv = alg.IV;

        string text = "teststring";

        string encrypted = EncryptWithAes(text, key, iv);

        MessageBox.Show(encrypted);

        String result = DecryptAesCryptoString(encrypted, key, iv);

        MessageBox.Show(result);

【讨论】:

  • 这就是我生成密钥的方式所以实现是错误的?public static byte[] GenerateAesKey() { byte[] key = null; using (RijndaelManaged aesAlgorithm = new RijndaelManaged()) { aesAlgorithm.GenerateKey(); key = aesAlgorithm.Key; } return key; }
  • 不,应该可以。如果您以同样的方式生成 IV,那么它也应该可以工作
  • 我也是这样做的。我确信这与编码有关。你能不能花点时间快速放一个样品
  • 查看 RijndaelManaged 类的源代码,特别是 Encrypt 方法。您基本上是在复制课堂上已经存在的一些步骤。尝试默认实现(加密/解密),看看它是否有效。如果是,请开始进行所需的更改。
【解决方案2】:

很遗憾,这肯定是由于编码问题。现在像下面这样解决它

加密

public static string EncryptWithAes(string plainText, byte[] key, byte[] initiationVector)
        {
            byte[] cryptoBytes = Convert.FromBase64String(plainText);
            using (RijndaelManaged aesAlgorithm = new RijndaelManaged())
            {
                aesAlgorithm.Key = key;
                aesAlgorithm.IV = initiationVector;
                aesAlgorithm.Mode = CipherMode.ECB;
                using (ICryptoTransform encryptoTransform = aesAlgorithm.CreateEncryptor(aesAlgorithm.Key, aesAlgorithm.IV))
                {
                    cryptoBytes = encryptoTransform.TransformFinalBlock(cryptoBytes, 0, cryptoBytes.Length);
                }
            }
            return Convert.ToBase64String(cryptoBytes);
        }

解密

public static string DecryptAesCryptoString(string cipherText, byte[] key, byte[] initiationVector)
{

    byte[] decryptedByte;
    using (RijndaelManaged aesAlgorithm = new RijndaelManaged())
    {
        aesAlgorithm.Key = key;
        aesAlgorithm.IV = initiationVector;
        aesAlgorithm.Mode = CipherMode.ECB;
        using (ICryptoTransform decryptoTransform = aesAlgorithm.CreateDecryptor(aesAlgorithm.Key, aesAlgorithm.IV))
        {
            byte[] cipherBytes = Convert.FromBase64String(cipherText);
            decryptedByte = decryptoTransform.TransformFinalBlock(cipherBytes, 0, cipherBytes.Length);

        }
    }
    return Convert.ToBase64String(decryptedByte);
}

【讨论】:

    【解决方案3】:

    你为什么不试试去掉 Encoding ? Here's a simple implementation

    public class RijndaelSimpleTest
    {
        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [STAThread]
        static void Main(string[] args)
        {
            string   plainText          = "Hello, World!";    // original plaintext
    
            string   passPhrase         = "Pas5pr@se";        // can be any string
            string   saltValue          = "s@1tValue";        // can be any string
            string   hashAlgorithm      = "SHA1";             // can be "MD5"
            int      passwordIterations = 2;                  // can be any number
            string   initVector         = "@1B2c3D4e5F6g7H8"; // must be 16 bytes
            int      keySize            = 256;                // can be 192 or 128
    
            Console.WriteLine(String.Format("Plaintext : {0}", plainText));
    
            string  cipherText = RijndaelSimple.Encrypt(plainText,
                                                        passPhrase,
                                                        saltValue,
                                                        hashAlgorithm,
                                                        passwordIterations,
                                                        initVector,
                                                        keySize);
    
            Console.WriteLine(String.Format("Encrypted : {0}", cipherText));
    
            plainText          = RijndaelSimple.Decrypt(cipherText,
                                                        passPhrase,
                                                        saltValue,
                                                        hashAlgorithm,
                                                        passwordIterations,
                                                        initVector,
                                                        keySize);
    
            Console.WriteLine(String.Format("Decrypted : {0}", plainText));
        }
    }
    

    【讨论】:

      猜你喜欢
      • 2016-01-10
      • 1970-01-01
      • 1970-01-01
      • 2012-02-27
      • 1970-01-01
      • 1970-01-01
      • 2019-06-15
      • 2012-12-21
      • 1970-01-01
      相关资源
      最近更新 更多