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

C#实现的AES加密解密完整实例,第1张

概述本文实例讲述了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",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,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 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,len); rijndaelCipher.Key = keyBytes; byte[] ivBytes = System.Text.EnCoding.UTF8.GetBytes(iv); rijndaelCipher.IV = ivBytes; ICryptotransform transform = rijndaelCipher.CreateDecryptor(); byte[] plainText = transform.transformFinalBlock(encryptedData,encryptedData.Length); return EnCoding.UTF8.GetString(plainText); }}

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

密码安全性在线检测:

tools.jb51.net/password/my_password_safe

高强度密码生成器:

tools.jb51.net/password/CreateStrongPassword

MD5在线加密工具:

tools.jb51.net/password/CreateMD5Password

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

tools.jb51.net/password/urlrethunder

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

tools.jb51.net/password/hash_encrypt

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

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

总结

以上是内存溢出为你收集整理的C#实现的AES加密解密完整实例全部内容,希望文章能够帮你解决C#实现的AES加密解密完整实例所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/langs/1258608.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-06-07
下一篇 2022-06-07

发表评论

登录后才能评论

评论列表(0条)

保存