欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

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

程序员文章站 2023-02-02 15:26:40
本文实例讲述了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);
 }
}

ps:关于加密解密感兴趣的朋友还可以参考本站在线工具:

密码安全性在线检测:


高强度密码生成器:

tools.jb51.net/password/createstrongpassword

md5在线加密工具:

tools.jb51.net/password/createmd5password

迅雷、快车、旋风url加密/解密工具:

在线散列/哈希算法加密工具:

更多关于c#相关内容还可查看本站专题:《c#加密与解密算法与技巧总结》、《c#窗体操作技巧汇总》、《c#常见控件用法教程》、《winform控件用法总结》、《c#程序设计之线程使用技巧总结》、《c#操作excel技巧总结》、《c#中xml文件操作技巧汇总》、《c#数据结构与算法教程》、《c#数组操作技巧总结》及《c#面向对象程序设计入门教程

希望本文所述对大家c#程序设计有所帮助。