• AES加密/解密


    /// <summary>
            /// AES加密 
            /// </summary>
            /// <param name="text">加密字符</param>
            /// <param name="password">加密的密码</param>
            /// <param name="iv">密钥</param>
            /// <returns></returns>
            public static string AESEncrypt(string text, string password, string iv)
            {
                RijndaelManaged rijndaelCipher = new RijndaelManaged();
    
                rijndaelCipher.Mode = CipherMode.ECB;
    
                rijndaelCipher.Padding = PaddingMode.PKCS7;
    
                rijndaelCipher.KeySize = 128;
    
                rijndaelCipher.BlockSize = 128;
    
                byte[] pwdBytes = System.Text.Encoding.UTF8.GetBytes(password);
    
                byte[] keyBytes = new byte[16];
    
                int len = pwdBytes.Length;
    
                if (len > keyBytes.Length) len = keyBytes.Length;
    
                System.Array.Copy(pwdBytes, keyBytes, len);
    
                rijndaelCipher.Key = keyBytes;
    
    
                byte[] ivBytes = System.Text.Encoding.UTF8.GetBytes(iv);
                rijndaelCipher.IV = new byte[16];
    
                ICryptoTransform transform = rijndaelCipher.CreateEncryptor();
    
                byte[] plainText = Encoding.UTF8.GetBytes(text);
    
                byte[] cipherBytes = transform.TransformFinalBlock(plainText, 0, plainText.Length);
    
                return Convert.ToBase64String(cipherBytes);
    
            }
    
            /// <summary>
            /// AES解密
            /// </summary>
            /// <param name="text"></param>
            /// <param name="password"></param>
            /// <param name="iv"></param>
            /// <returns></returns>
            public static string AESDecrypt(string text, string password, string iv)
            {
                RijndaelManaged rijndaelCipher = new RijndaelManaged();
    
                rijndaelCipher.Mode = CipherMode.ECB;
    
                rijndaelCipher.Padding = PaddingMode.PKCS7;
    
                rijndaelCipher.KeySize = 128;
    
                rijndaelCipher.BlockSize = 128;
    
                byte[] encryptedData = Convert.FromBase64String(text);
    
                byte[] pwdBytes = System.Text.Encoding.UTF8.GetBytes(password);
    
                byte[] keyBytes = new byte[16];
    
                int len = pwdBytes.Length;
    
                if (len > keyBytes.Length) len = keyBytes.Length;
    
                System.Array.Copy(pwdBytes, keyBytes, len);
    
                rijndaelCipher.Key = keyBytes;
    
                //byte[] ivBytes = System.Text.Encoding.UTF8.GetBytes(iv); //如果使用的是ECB模式,无需iv,注释这两行
                //rijndaelCipher.IV = ivBytes;
    
                ICryptoTransform transform = rijndaelCipher.CreateDecryptor();
    
                byte[] plainText = transform.TransformFinalBlock(encryptedData, 0, encryptedData.Length);
    
                return Encoding.UTF8.GetString(plainText);
    
            }
  • 相关阅读:
    POJ
    Fence Repair POJ
    COGS 2096. 不平凡的许愿树
    COGS 2095. 不平凡的引线
    BZOJ 4392 卡牌游戏
    COGS 2089. 平凡的测试数据
    Slim Span (最小生成树)
    【BZOJ1294】[SCOI2009]围豆豆Bean 射线法+状压DP+SPFA
    排列perm HYSBZ
    I
  • 原文地址:https://www.cnblogs.com/zhangwj/p/11364770.html
Copyright © 2020-2023  润新知