C#实现AES-ECB加解密

using System;
using System.Text;
using System.Security.Cryptography;

namespace Common_API_Test.Test_Demo
{
    class AES_ecb_Encrypt
    {
        static void Main(string[] args)
        {
            string key = "1234567812345678";// 密钥
            string encryptStr = "12345678"; // 明文
            string enStr = AESEncrypt(encryptStr, key);
            Console.Write(enStr);
            Console.ReadLine();
        }

        /// 
        /// AES-ecb加密
        /// 
        /// 明文
        /// 密钥
        /// 
        public static string AESEncrypt(string encryptStr, string Key)
        {
            byte[] keyArray = UTF8Encoding.UTF8.GetBytes(Key);
            byte[] toEncryptArray = UTF8Encoding.UTF8.GetBytes(encryptStr);
            RijndaelManaged rDel = new RijndaelManaged();
            rDel.Key = keyArray;
            rDel.Mode = CipherMode.ECB;
            rDel.Padding = PaddingMode.PKCS7;
            ICryptoTransform cTransform = rDel.CreateEncryptor();
            byte[] resultArray = cTransform.TransformFinalBlock(toEncryptArray, 0, toEncryptArray.Length);
            return Convert.ToBase64String(resultArray, 0, resultArray.Length);
        }

        /// 
        /// AES-ecb解密
        /// 
        /// 密文
        /// 密钥
        /// 
        public static string AESDEncrypt(string encryptStr, string Key)
        {
            byte[] keyArray = UTF8Encoding.UTF8.GetBytes(Key);
            byte[] toEncryptArray = Convert.FromBase64String(encryptStr);
            RijndaelManaged rDel = new RijndaelManaged();
            rDel.Key = keyArray;
            rDel.Mode = CipherMode.ECB;
            rDel.Padding = PaddingMode.PKCS7;
            ICryptoTransform cTransform = rDel.CreateDecryptor();
            byte[] resultArray = cTransform.TransformFinalBlock(toEncryptArray, 0, toEncryptArray.Length);
            return UTF8Encoding.UTF8.GetString(resultArray);
        }
    }
}

你可能感兴趣的:(数学建模,java,算法)