如何解密AES-256-CBC加密字符串

 mobiledu2502931077 发布于 2023-02-13 17:52

我是C#的新手,我真的需要帮助.我需要在C#中使用AES-256-CBC加密/解密字符串,我发现这是为了加密字符串:

    public static string EncryptString(string message, string KeyString, string IVString)
    {
        byte[] Key = ASCIIEncoding.UTF8.GetBytes(KeyString);
        byte[] IV = ASCIIEncoding.UTF8.GetBytes(IVString);

        string encrypted = null;
        RijndaelManaged rj = new RijndaelManaged();
        rj.Key = Key;
        rj.IV = IV;
        rj.Mode = CipherMode.CBC;

        try
        {
            MemoryStream ms = new MemoryStream();

            using (CryptoStream cs = new CryptoStream(ms, rj.CreateEncryptor(Key, IV), CryptoStreamMode.Write))
            {
                using (StreamWriter sw = new StreamWriter(cs))
                {
                    sw.Write(message);
                    sw.Close();
                }
                cs.Close();
            }
            byte[] encoded = ms.ToArray();
            encrypted = Convert.ToBase64String(encoded);

            ms.Close();
        }
        catch (CryptographicException e)
        {
            Console.WriteLine("A Cryptographic error occurred: {0}", e.Message);
            return null;
        }
        catch (UnauthorizedAccessException e)
        {
            Console.WriteLine("A file error occurred: {0}", e.Message);
            return null;
        }
        catch (Exception e)
        {
            Console.WriteLine("An error occurred: {0}", e.Message);
        }
        finally
        {
            rj.Clear();
        }
        return encrypted;
    }

我试着写一个基于上面代码的解密函数,下面的代码是我做的:

  // Decrypt a byte array into a byte array using a key and an IV 
        private byte[] Decrypt(byte[] cipherData, byte[] Key, byte[] IV)
        {
            byte[] decryptedData;
            //string plaintext = null;
            //MemoryStream ms = new MemoryStream(cipherData);

            RijndaelManaged alg = new RijndaelManaged();
   alg.KeySize = 256;
            alg.BlockSize = 128;
            alg.Key = Key;
            alg.IV = IV;
            alg.Mode = CipherMode.CBC;
            alg.Padding = PaddingMode.Zeros;

            //Array.Copy(Key, 0, IV, 0, IV.Length);

            ICryptoTransform decryptor = alg.CreateDecryptor(alg.Key, alg.IV);

            using(MemoryStream ms = new MemoryStream(cipherData))
            {
                using (CryptoStream csDecrypt = new CryptoStream(ms, decryptor, CryptoStreamMode.Read))
                {
                    using (StreamReader sw = new StreamReader(csDecrypt))
                    {
                        sw.ReadToEnd();
                        sw.Close();
                    }

                    csDecrypt.Close();
                    decryptedData = ms.ToArray();
                }
            }

            //byte[] decryptedData = System.Text.Encoding.Unicode.GetBytes(plaintext);
            return decryptedData; 
        }

但这是无稽之谈,它无法解密任何东西.我真的很困惑,需要帮助.感谢您的任何帮助!

P/s:请不要给我其他类似的回答问题,我已经看过了.它们的加密功能与上述加密功能不具有相同的输出,而我需要解密必须由上述功能加密的字符串.我有两个用PHP编写解密函数的朋友和Objective-C,它与上面的加密函数相匹配,让他们再做一次是不好的.

1 个回答
  • 看看你的加密,这样的事情应该这样做,从你的加密传递结果字符串应该给回原始字符串;

    // Decrypt a string into a string using a key and an IV 
    public static string Decrypt(string cipherData, string keyString, string ivString)
    {
        byte[] key = Encoding.UTF8.GetBytes(keyString);
        byte[] iv  = Encoding.UTF8.GetBytes(ivString);
    
        try
        {
            using (var rijndaelManaged =
                   new RijndaelManaged {Key = key, IV = iv, Mode = CipherMode.CBC})
            using (var memoryStream = 
                   new MemoryStream(Convert.FromBase64String(cipherData)))
            using (var cryptoStream =
                   new CryptoStream(memoryStream,
                       rijndaelManaged.CreateDecryptor(key, iv),
                       CryptoStreamMode.Read))
            {
                return new StreamReader(cryptoStream).ReadToEnd();
            }
        }
        catch (CryptographicException e)
        {
            Console.WriteLine("A Cryptographic error occurred: {0}", e.Message);
            return null;
        }
        // You may want to catch more exceptions here...
    }
    

    小记; 如果您使用密钥字符串中的UTF8编码获取密钥,UTF8编码可能会为国际字符提供多个字节,这可能会给密钥或IV加密/解密的长度错误.此外,使用8个字符和可打印字符的小范围密码/密钥将无法提供非常安全的加密,您可能希望在将字符串作为密钥使用之前通过SHA1或类似字符串运行该字符串(这将令人遗憾地使其与当前加密)

    2023-02-13 17:54 回答
撰写答案
今天,你开发时遇到什么问题呢?
立即提问
热门标签
PHP1.CN | 中国最专业的PHP中文社区 | PNG素材下载 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有