[C#]AES加密算法实现 各大神汇总

本文介绍了C#中AES加密算法的实现,包括详细步骤和代码示例,涵盖了不同场景下的加密与解密操作,帮助开发者理解并应用AES加密技术。
摘要由CSDN通过智能技术生成
public static class AesSecret
    {
        #region 秘钥对

        private const string saltString = "Wolfy@home";
        private const string pWDString = "home@Wolfy";

        #endregion

        #region 加/解密算法

        /// <summary>
        /// 解密
        /// </summary>
        /// <param name="sSource">需要解密的内容</param>
        /// <returns></returns>
        public static byte[] DecryptString(string strSource)
        {
            byte[] encryptBytes = Convert.FromBase64String(strSource);
            byte[] salt = Encoding.UTF8.GetBytes(saltString);
            //提供高级加密标准 (AES) 对称算法的托管实现。
            AesManaged aes = new AesManaged();
            //通过使用基于 System.Security.Cryptography.HMACSHA1 的伪随机数生成器,实现基于密码的密钥派生功能 (PBKDF2)。
            Rfc2898DeriveBytes rfc = new Rfc2898DeriveBytes(pWDString, salt);
            // 获取或设置加密操作的块大小(以位为单位)。
            aes.BlockSize = aes.LegalBlockSizes[0].MaxSize;
            //获取或设置用于对称算法的密钥大小(以位为单位)。
            aes.KeySize = aes.LegalKeySizes[0].MaxSize;
            //获取或设置用于对称算法的密钥。
            aes.Key = rfc.GetBytes(aes.KeySize / 8);
            //获取或设置用于对称算法的初始化向量 (IV)。
            aes.IV = rfc.GetBytes(aes.BlockSize / 8);

            // 用当前的 Key 属性和初始化向量 IV 创建对称解密器对象
            System.Security.Cryptography.ICryptoTransform decryptTransform = aes.CreateDecryptor();

            // 解密后的输出流
            MemoryStream decryptStream = new MemoryStream();

            // 将解密后的目标流(decryptStream)与解密转换(decryptTransform)相连接
            CryptoStream decryptor = new CryptoStream(
                decryptStream, decryptTransform, CryptoStreamMode.Write);

            // 将一个字节序列写入当前 CryptoStream (完成解密的过程)
            decryptor.Write(encryptBytes, 0, encryptBytes.Length);
            decryptor.Close();

            // 将解密后所得到的流转换为字符串
            return decryptStream.ToArray();

        }

        /// <summary>
        /// 加密
        /// </summary>
        /// <param name="sSource">需要加密的内容</param>
        /// <returns></returns>
        public static byte[] EncryptString(string strSource)
        {
            byte[] data = UTF8Encoding.UTF8.GetBytes(strSource);
            byte[] salt = UTF8Encoding.UTF8.GetBytes(saltString);

            // AesManaged - 高级加密标准(AES) 对称算法的管理类
            AesManaged aes = new AesManaged();

            // Rfc2898DeriveBytes - 通过使用基于 HMACSHA1 的伪随机数生成器,实现基于密码的密钥派生功能 (PBKDF2 - 一种基于密码的密钥派生函数)
            // 通过 密码 和 salt 派生密钥
            Rfc2898DeriveBytes rfc = new Rfc2898DeriveBytes(pWDString, salt);

            /*
            * AesManaged.BlockSize - 加密操作的块大小(单位:bit)
            * AesManaged.LegalBlockSizes - 对称算法支持的块大小(单位:bit)
            * AesManaged.KeySize - 对称算法的密钥大小(单位:bit)
            * AesManaged.LegalKeySizes - 对称算法支持的密钥大小(单位:bit)
            * AesManaged.Key - 对称算法的密钥
            * AesManaged.IV - 对称算法的密钥大小
            * Rfc2898DeriveBytes.GetBytes(int 需要生成的伪随机密钥字节数) - 生成密钥
            */

            aes.BlockSize = aes.LegalBlockSizes[0].MaxSize;
            aes.KeySize = aes.LegalKeySizes[0].MaxSize;
            aes.Key = rfc.GetBytes(aes.KeySize / 8);
            aes.IV = rfc.GetBytes(aes.BlockSize / 8);

            // 用当前的 Key 属性和初始化向量 IV 创建对称加密器对象
            ICryptoTransform encryptTransform = aes.CreateEncryptor();

            // 加密后的输出流
            MemoryStream encryptStr
  • 4
    点赞
  • 26
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值