使用AES加密的帮助类

在开发中经常使用加密/解密对一些内容进行处理,比如密码在存入数据库之前先经过加密处理等等,这里就把一个加密帮助类代码贴出来,供以后查找使用。

这个帮助类主要功能是对字符串和字节数组进行加密解密处理。

public class EncryptionHelper
    {
        //默认密钥向量 
        private static readonly byte[] DefaultKey = {0x12, 0x34, 0x56, 120, 0x90, 0xab, 0xcd, 0xef};

        /// <summary>
        ///     解密字节数组
        /// </summary>
        /// <param name="cipherBytes">密文字节数组</param>
        /// <param name="password">密钥</param>
        /// <returns>解密后字节数组</returns>
        public static byte[] DecryptBytes(byte[] cipherBytes, string password)
        {
            byte[] buffer;
            try
            {
                using (Aes aes = new AesManaged())
                {
                    //设置密钥及密钥向量
                    aes.Key = new Rfc2898DeriveBytes(password, DefaultKey).GetBytes(0x10);
                    aes.IV = aes.Key;
                    using (var memoryStream = new MemoryStream())
                    {
                        using (
                            var cryptoStream = new CryptoStream(memoryStream, aes.CreateDecryptor(),
                                CryptoStreamMode.Write))
                        {
                            cryptoStream.Write(cipherBytes, 0, cipherBytes.Length);
                            cryptoStream.Flush();
                        }
                        //得到解密后的字节数组
                        buffer = memoryStream.ToArray();
                    }
                }
            }
            catch
            {
                buffer = null;
            }
            return buffer;
        }

        /// <summary>
        ///     解密字符串
        /// </summary>
        /// <param name="cipherText">密文</param>
        /// <param name="password">密钥</param>
        /// <returns>解密后字符串</returns>
        public static string DecryptString(string cipherText, string password)
        {
            byte[] decryptBytes = DecryptBytes(Convert.FromBase64String(cipherText), password);
            if (decryptBytes == null)
            {
                return null;
            }
            return Encoding.UTF8.GetString(decryptBytes, 0, decryptBytes.Length);
        }

        /// <summary>
        ///     加密字节数组
        /// </summary>
        /// <param name="plainBytes">明文字节数组</param>
        /// <param name="password">密钥</param>
        /// <returns>加密后字节数组</returns>
        public static byte[] EncryptBytes(byte[] plainBytes, string password)
        {
            byte[] buffer;
            try
            {
                using (Aes aes = new AesManaged())
                {
                    //设置密钥及密钥向量
                    aes.Key = new Rfc2898DeriveBytes(password, DefaultKey).GetBytes(0x10);
                    aes.IV = aes.Key;
                    using (var memoryStream = new MemoryStream())
                    {
                        using (
                            var cryptoStream = new CryptoStream(memoryStream, aes.CreateEncryptor(),
                                CryptoStreamMode.Write))
                        {
                            cryptoStream.Write(plainBytes, 0, plainBytes.Length);
                            cryptoStream.FlushFinalBlock();
                        }
                        //得到加密后的字节数组
                        buffer = memoryStream.ToArray();
                    }
                }
            }
            catch
            {
                buffer = null;
            }
            return buffer;
        }

        /// <summary>
        ///     加密字符串
        /// </summary>
        /// <param name="plainText">明文</param>
        /// <param name="password">密钥</param>
        /// <returns>加密后字符串</returns>
        public static string EncryptString(string plainText, string password)
        {
            byte[] cipherBytes = EncryptBytes(Encoding.UTF8.GetBytes(plainText), password);
            if (cipherBytes == null)
            {
                return null;
            }
            return Convert.ToBase64String(cipherBytes);
        }
    }

转载于:https://www.cnblogs.com/candyzkn/p/3566252.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值