码迷,mamicode.com
首页 > Windows程序 > 详细

C#.NET AES ECB 加密

时间:2020-12-17 12:46:40      阅读:3      评论:0      收藏:0      [点我收藏+]

标签:encoding   block   bytes   system   net   pre   turn   lock   tor   

 

 

加密:

/// <summary>
        /// 加密
        /// </summary>
        /// <param name="content">要加密的串</param>
        /// <param name="aesKey">密钥</param>
        /// <returns></returns>
        public static string AesEncryptECB(string content, string aesKey)
        {
            byte[] byteKEY = Encoding.UTF8.GetBytes(aesKey);

            byte[] byteContnet = Encoding.UTF8.GetBytes(content);

            var _aes = new RijndaelManaged();
            _aes.Padding = PaddingMode.PKCS7;
            _aes.Mode = CipherMode.ECB;
            _aes.Key = byteKEY;

            var _crypto = _aes.CreateEncryptor();
            byte[] decrypted = _crypto.TransformFinalBlock(byteContnet, 0, byteContnet.Length);

            _crypto.Dispose();

            return Convert.ToBase64String(decrypted);
        }

 

解密:

/// <summary>
        /// 解密
        /// </summary>
        /// <param name="decryptStr">要解密的串</param>
        /// <param name="aesKey">密钥</param>        
        /// <returns></returns>
        public static string AesDecryptECB(string decryptStr, string aesKey)
        {
            byte[] byteKEY = Encoding.UTF8.GetBytes(aesKey);
            byte[] byteDecrypt = System.Convert.FromBase64String(decryptStr);

            var _aes = new RijndaelManaged();
            _aes.Padding = PaddingMode.PKCS7;
            _aes.Mode = CipherMode.ECB;
            _aes.Key = byteKEY;

            var _crypto = _aes.CreateDecryptor();
            byte[] decrypted = _crypto.TransformFinalBlock(byteDecrypt, 0, byteDecrypt.Length);

            _crypto.Dispose();

            return Encoding.UTF8.GetString(decrypted);
        }

-

 

C#.NET AES ECB 加密

标签:encoding   block   bytes   system   net   pre   turn   lock   tor   

原文地址:https://www.cnblogs.com/runliuv/p/14124427.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!