zoukankan      html  css  js  c++  java
  • C# 实现 AES 加密算法

    开篇日常立个flag....

    代码

    命名空间 System.Security.Cryptography

    public enum enmKeyBit
    {
        KeyBit128 = 128,
        KeyBit192 = 192,
        KeyBit256 = 256,
    }
    
    public class EncryptNew
    {
        /// <summary>
        /// AES ECB模式 加密
        /// </summary>
        /// <param name="data">明文</param>
        /// <param name="key">密钥</param>
        /// <param name="keyBit">密钥位数</param>
        /// <returns></returns>
        public static string AESEncrypt(String data, String key, enmKeyBit keyBit = enmKeyBit.KeyBit256)
        {
            if (string.IsNullOrEmpty(data))
            {
                return string.Empty;
            }
    
            try
            {
                int intKeyBit = (int)keyBit;
                RijndaelManaged aes = new RijndaelManaged();
                byte[] bytsData = Encoding.UTF8.GetBytes(data);
                byte[] bytsKey = new Byte[intKeyBit / 8];
                Array.Copy(Encoding.UTF8.GetBytes(key.PadRight(bytsKey.Length)), bytsKey, bytsKey.Length);
    
                aes.Mode = CipherMode.ECB;
                aes.Padding = PaddingMode.PKCS7;
                aes.KeySize = intKeyBit;
                aes.Key = bytsKey;
                //aes.IV = iv;
                ICryptoTransform ctf = aes.CreateEncryptor();
                byte[] bytsResult = ctf.TransformFinalBlock(bytsData, 0, bytsData.Length);
    
                return Convert.ToBase64String(bytsResult);
            }
            catch
            {
                return string.Empty;
            }
        }
    
        /// <summary>
        /// AES ECB模式 解密
        /// </summary>
        /// <param name="data">密文</param>
        /// <param name="key">密钥</param>
        /// <param name="keyBit">密钥位数</param>
        /// <returns></returns>
        public static string AESDecrypt(String data, String key, enmKeyBit keyBit = enmKeyBit.KeyBit256)
        {
            if (string.IsNullOrEmpty(data))
            {
                return string.Empty;
            }
    
            try
            {
                int intKeyBit = (int)keyBit;
                RijndaelManaged aes = new RijndaelManaged();
                byte[] bytsData = Convert.FromBase64String(data);
                byte[] bytsKey = new Byte[intKeyBit / 8];
                Array.Copy(Encoding.UTF8.GetBytes(key.PadRight(bytsKey.Length)), bytsKey, bytsKey.Length);
    
                aes.Mode = CipherMode.ECB;
                aes.Padding = PaddingMode.PKCS7;
                aes.KeySize = intKeyBit;
                aes.Key = bytsKey;
                //aes.IV = iv;
    
                ICryptoTransform ctf = aes.CreateDecryptor();
                byte[] bytsResult = ctf.TransformFinalBlock(bytsData, 0, bytsData.Length);
    
                return UTF8Encoding.UTF8.GetString(bytsResult);
            }
            catch
            {
                return string.Empty;
            }
        }
    }
    

      

  • 相关阅读:
    27数据结构与算法分析之---二叉排序树
    26数据结构与算法分析之---线索二叉树
    25数据结构与算法分析之---树与森林
    24数据结构与算法分析之---二叉树的概念
    23数据结构与算法分析之---树的基本概念
    22数据结构与算法分析之---特殊矩阵
    21数据结构与算法分析之---多维数组
    20数据结构与算法分析之---串的模式匹配
    17数据结构与算法分析之---串的类型
    16数据结构与算法分析之---链式队列
  • 原文地址:https://www.cnblogs.com/clis/p/14299981.html
Copyright © 2011-2022 走看看