源码网商城,靠谱的源码在线交易网站 我的订单 购物车 帮助

源码网商城

C#实现的AES加密解密完整实例

  • 时间:2021-12-04 04:26 编辑: 来源: 阅读:
  • 扫一扫,手机访问
摘要:C#实现的AES加密解密完整实例
本文实例讲述了C#实现的AES加密解密。分享给大家供大家参考,具体如下:
/******************************************************************
 * 创建人:HTL
 * 说明:C# AES加密解密
 *******************************************************************/
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);
 }
}

[b]PS:关于加密解密感兴趣的朋友还可以参考本站在线工具:[/b] [b]密码安全性在线检测: [/b] [url=http://tools.jb51.net/password/my_password_safe]tools.jb51.net/password/my_password_safe[/url] [b]高强度密码生成器:[/b][b] [/b] [url=http://tools.jb51.net/password/CreateStrongPassword]tools.jb51.net/password/CreateStrongPassword[/url] [b]MD5在线加密工具:[/b][b] [/b] [url=http://tools.jb51.net/password/CreateMD5Password]tools.jb51.net/password/CreateMD5Password[/url] [b]迅雷、快车、旋风URL加密/解密工具:[/b][b] [/b] [url=http://tools.jb51.net/password/urlrethunder]tools.jb51.net/password/urlrethunder[/url] [b]在线散列/哈希算法加密工具:[/b] [url=http://tools.jb51.net/password/hash_encrypt]tools.jb51.net/password/hash_encrypt[/url] 更多关于C#相关内容还可查看本站专题:《[url=http://www.1sucai.cn/Special/173.htm]C#加密与解密算法与技巧总结[/url]》、《[url=http://www.1sucai.cn/Special/611.htm]C#窗体操作技巧汇总[/url]》、《[url=http://www.1sucai.cn/Special/165.htm]C#常见控件用法教程[/url]》、《[url=http://www.1sucai.cn/Special/125.htm]WinForm控件用法总结[/url]》、《[url=http://www.1sucai.cn/Special/227.htm]C#程序设计之线程使用技巧总结[/url]》、《[url=http://www.1sucai.cn/Special/805.htm]C#操作Excel技巧总结[/url]》、《[url=http://www.1sucai.cn/Special/266.htm]C#中XML文件操作技巧汇总[/url]》、《[url=http://www.1sucai.cn/Special/116.htm]C#数据结构与算法教程[/url]》、《[url=http://www.1sucai.cn/Special/265.htm]C#数组操作技巧总结[/url]》及《[url=http://www.1sucai.cn/Special/478.htm]C#面向对象程序设计入门教程[/url]》 希望本文所述对大家C#程序设计有所帮助。
  • 全部评论(0)
联系客服
客服电话:
400-000-3129
微信版

扫一扫进微信版
返回顶部