zoukankan      html  css  js  c++  java
  • C# AES加密解密

    完整代码:
    /******************************************************************
     * 创建人:HTL
     * 创建时间:2015-04-17 17:36:35
     * 说明:C# AES加密解密
     * Email:huangyuan413026@163.com
     *******************************************************************/
    using System;
    using System.Security.Cryptography;
    using System.Text;
    using System.IO;
    public class Test
    {
        public static void Main()
        {
            //密码
            string password="1234567890123456";
            //加密初始化向量
            string iv="                ";     
            string message=AESEncrypt("abcdefghigklmnopqrstuvwxyz0123456789",password,iv);
            Console.WriteLine(message);
     
            message=AESDecrypt("8Z3dZzqn05FmiuBLowExK0CAbs4TY2GorC2dDPVlsn/tP+VuJGePqIMv1uSaVErr",password,iv);
     
            Console.WriteLine(message);
        }
     
     
        /// <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.CBC;
     
            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.CBC;
     
            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);
            rijndaelCipher.IV = ivBytes;
     
            ICryptoTransform transform = rijndaelCipher.CreateDecryptor();
     
            byte[] plainText = transform.TransformFinalBlock(encryptedData, 0, encryptedData.Length);
     
            return Encoding.UTF8.GetString(plainText);
     
        }
     
    }
    参考:



  • 相关阅读:
    python-- socket介绍
    Vue--element实现删除会员功能
    Vue--element实现编辑会员功能
    git clone速度过慢!
    Codeforces Round #730 (Div. 2) A/B/C/D1 解题思路
    KYOCERA Programming Contest 2021(AtCoder Beginner Contest 200) E
    Codeforces Round #719 (Div. 3) 解题报告
    ZJNU 1265
    PTA L3
    Codeforces 1503B/1504D
  • 原文地址:https://www.cnblogs.com/huangtailang/p/4435765.html
Copyright © 2011-2022 走看看