码迷,mamicode.com
首页 > 其他好文 > 详细

AES加密解密 助手类 CBC加密模式

时间:2018-11-19 12:39:59      阅读:185      评论:0      收藏:0      [点我收藏+]

标签:man   加密   tail   creat   decrypt   space   lock   from   target   

 

string str = "2018";
string result1 = AESHelper.AesEncrypt(str);
string result2 = AESHelper.AesDecrypt(result1);

 

namespace Demo
{
    /// <summary>
    /// AES加密解密 助手类
    /// CBC加密模式
    /// </summary>
    public class AESHelper
    {
        /// <summary>
        /// 默认密钥-长度32位
        /// </summary>
        private const string Key = "32位";

        /// <summary>
        /// 默认向量-长度16位
        /// </summary>
        private const string Iv = "16位";


        /// <summary>  
        /// AES加密  
        /// </summary>  
        /// <param name="str">需要加密字符串</param>  
        /// <returns>加密后字符串</returns> 
        public static string AesEncrypt(string str)
        {
            return Encrypt(str, Key);
        }

        /// <summary>  
        /// AES解密  
        /// </summary>  
        /// <param name="str">需要解密字符串</param>  
        /// <returns>解密后字符串</returns>  
        public static string AesDecrypt(string str)
        {
            return Decrypt(str, Key);
        }


        /// <summary>
        ///  AES 加密
        /// </summary>
        /// <param name="str">明文(待加密)</param>
        /// <param name="key">密文</param>
        /// <returns></returns>
        private static string Encrypt(string str, string key)
        {
            if (string.IsNullOrEmpty(str)) return null;
            Byte[] toEncryptArray = Encoding.UTF8.GetBytes(str);

            RijndaelManaged rm = new RijndaelManaged
            {
                Key = Encoding.UTF8.GetBytes(key),
                Mode = CipherMode.ECB,
                Padding = PaddingMode.PKCS7,
                IV = Encoding.UTF8.GetBytes(Iv)
            };

            ICryptoTransform cTransform = rm.CreateEncryptor();
            Byte[] resultArray = cTransform.TransformFinalBlock(toEncryptArray, 0, toEncryptArray.Length);

            return Convert.ToBase64String(resultArray, 0, resultArray.Length);
        }
        /// <summary>
        ///  AES 解密
        /// </summary>
        /// <param name="str">明文(待解密)</param>
        /// <param name="key">密文</param>
        /// <returns></returns>
        private static string Decrypt(string str, string key)
        {
            if (string.IsNullOrEmpty(str)) return null;
            Byte[] toEncryptArray = Convert.FromBase64String(str);

            RijndaelManaged rm = new RijndaelManaged
            {
                Key = Encoding.UTF8.GetBytes(key),
                Mode = CipherMode.ECB,
                Padding = PaddingMode.PKCS7,
                IV = Encoding.UTF8.GetBytes(Iv)
            };

            ICryptoTransform cTransform = rm.CreateDecryptor();
            Byte[] resultArray = cTransform.TransformFinalBlock(toEncryptArray, 0, toEncryptArray.Length);

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

 

文章内容参考于(原出处):

C#几种常用的加密方式

C#加密解密(AES)-AESHelper

 

AES加密解密 助手类 CBC加密模式

标签:man   加密   tail   creat   decrypt   space   lock   from   target   

原文地址:https://www.cnblogs.com/gilbert/p/9982210.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!