.net 6 The Rijndael and RijndaelManaged types are obsolete. Use Aes instead

RijndaelManaged已经过时,升级一下即可,下面是例子

internal class Program
{
    static void Main(string[] args)
    {
        var key = "1234567890123456";
        var iv = "1234567890123456";
        var input = "你好啊";
        var jiami = EncryptByAESNew(input, key, iv);
        Console.WriteLine(jiami);
        Console.WriteLine(DecryptByAESNew(jiami, key, iv));
    }

    #region aes加密解密,RijndaelManaged
    /// <summary>
    /// AES加密算法
    /// </summary>
    /// <param name="input">明文字符串</param>
    /// <param name="key">密钥 长度为:16</param>
    /// <param name="iv">偏移 长度为:16</param>
    /// <returns>字符串</returns>
    public static string EncryptByAES(string input, string key, string iv)
    {
        if (string.IsNullOrWhiteSpace(input))
        {
            return input;
        }
        using (RijndaelManaged rijndaelManaged = new RijndaelManaged())
        {
            rijndaelManaged.Mode = CipherMode.CBC;
            rijndaelManaged.Padding = PaddingMode.PKCS7;
            rijndaelManaged.FeedbackSize = 128;
            rijndaelManaged.Key = Encoding.UTF8.GetBytes(key);
            rijndaelManaged.IV = Encoding.UTF8.GetBytes(iv);
            ICryptoTransform encryptor = rijndaelManaged.CreateEncryptor(rijndaelManaged.Key, rijndaelManaged.IV);
            using (MemoryStream msEncrypt = new MemoryStream())
            {
                using (CryptoStream csEncrypt = new CryptoStream(msEncrypt, encryptor, CryptoStreamMode.Write))
                {
                    using (StreamWriter swEncrypt = new StreamWriter(csEncrypt))
                    {
                        swEncrypt.Write(input);
                    }
                    byte[] bytes = msEncrypt.ToArray();
                    //return BitConverter.ToString(bytes).Replace("-", "").ToLower();//返回hex
                    return Convert.ToBase64String(bytes);
                }
            }
        }
    }
   
        /// <summary>  
        /// AES解密  
        /// </summary>
        /// <param name="input">密文字节数组</param>
    /// <param name="key">密钥 长度为:16</param>
    /// <param name="iv">偏移 长度为:16</param>
        /// <returns>返回解密后的字符串</returns>  
        public static string DecryptByAES(string input, string key, string iv)
    {
        if (string.IsNullOrWhiteSpace(input))
        {
            return input;
        }
        var buffer = Convert.FromBase64String(input);
        //buffer = hexStringToByteArray(input);//如果是hex则用hex方式处理
        using (RijndaelManaged rijndaelManaged = new RijndaelManaged())
        {
            rijndaelManaged.Mode = CipherMode.CBC;
            rijndaelManaged.Padding = PaddingMode.PKCS7;
            rijndaelManaged.FeedbackSize = 128;
            rijndaelManaged.Key = Encoding.UTF8.GetBytes(key);
            rijndaelManaged.IV = Encoding.UTF8.GetBytes(iv);
            ICryptoTransform decryptor = rijndaelManaged.CreateDecryptor(rijndaelManaged.Key, rijndaelManaged.IV);
            using (MemoryStream msEncrypt = new MemoryStream(buffer))
            {
                using (CryptoStream csEncrypt = new CryptoStream(msEncrypt, decryptor, CryptoStreamMode.Read))
                {
                    using (StreamReader srEncrypt = new StreamReader(csEncrypt))
                    {
                        return srEncrypt.ReadToEnd();
                    }
                }
            }
        }
    }
    #endregion

    #region aes加密解密 Aes
    /// <summary>
    /// AES加密算法
    /// </summary>
    /// <param name="input">明文字符串</param>
    /// <param name="key">密钥 长度为:16</param>
    /// <param name="iv">偏移 长度为:16</param>
    /// <returns>字符串</returns>
    public static string EncryptByAESNew(string input, string key, string iv)
    {
        if (string.IsNullOrWhiteSpace(input))
        {
            return input;
        }
        using (var rijndaelManaged = Aes.Create())
        {
            rijndaelManaged.Mode = CipherMode.CBC;
            rijndaelManaged.Padding = PaddingMode.PKCS7;
            rijndaelManaged.FeedbackSize = 128;
            rijndaelManaged.Key = Encoding.UTF8.GetBytes(key);
            rijndaelManaged.IV = Encoding.UTF8.GetBytes(iv);
            ICryptoTransform encryptor = rijndaelManaged.CreateEncryptor(rijndaelManaged.Key, rijndaelManaged.IV);
            using (MemoryStream msEncrypt = new MemoryStream())
            {
                using (CryptoStream csEncrypt = new CryptoStream(msEncrypt, encryptor, CryptoStreamMode.Write))
                {
                    using (StreamWriter swEncrypt = new StreamWriter(csEncrypt))
                    {
                        swEncrypt.Write(input);
                    }
                    byte[] bytes = msEncrypt.ToArray();
                    //return BitConverter.ToString(bytes).Replace("-", "").ToLower();//返回hex
                    return Convert.ToBase64String(bytes);
                }
            }
        }
    }


    /// <summary>  
        /// AES解密  
        /// </summary>
        /// <param name="input">密文字节数组</param>
    /// <param name="key">密钥 长度为:16、24、32</param>
    /// <param name="iv">偏移 长度为:16</param>
        /// <returns>返回解密后的字符串</returns>  
        public static string DecryptByAESNew(string input, string key, string iv)
    {
        if (string.IsNullOrWhiteSpace(input))
        {
            return input;
        }
        var buffer = Convert.FromBase64String(input);
        //buffer = hexStringToByteArray(input);//如果是hex则用hex方式处理
        using (var rijndaelManaged = Aes.Create())
        {
            rijndaelManaged.Mode = CipherMode.CBC;
            rijndaelManaged.Padding = PaddingMode.PKCS7;
            rijndaelManaged.FeedbackSize = 128;
            rijndaelManaged.Key = Encoding.UTF8.GetBytes(key);
            rijndaelManaged.IV = Encoding.UTF8.GetBytes(iv);
            ICryptoTransform decryptor = rijndaelManaged.CreateDecryptor(rijndaelManaged.Key, rijndaelManaged.IV);
            using (MemoryStream msEncrypt = new MemoryStream(buffer))
            {
                using (CryptoStream csEncrypt = new CryptoStream(msEncrypt, decryptor, CryptoStreamMode.Read))
                {
                    using (StreamReader srEncrypt = new StreamReader(csEncrypt))
                    {
                        return srEncrypt.ReadToEnd();
                    }
                }
            }
        }
    }
    #endregion
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

假装我不帅

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值