本文實例講述了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.VeVB.COm/password/my_password_safe
高強度密碼生成器:
tools.VeVB.COm/password/CreateStrongPassword
MD5在線加密工具:
tools.VeVB.COm/password/CreateMD5Password
迅雷、快車、旋風URL加密/解密工具:
tools.VeVB.COm/password/urlrethunder
在線散列/哈希算法加密工具:
tools.VeVB.COm/password/hash_encrypt
更多關于C#相關內容還可查看本站專題:《C#加密與解密算法與技巧總結》、《C#窗體操作技巧匯總》、《C#常見控件用法教程》、《WinForm控件用法總結》、《C#程序設計之線程使用技巧總結》、《C#操作Excel技巧總結》、《C#中XML文件操作技巧匯總》、《C#數據結構與算法教程》、《C#數組操作技巧總結》及《C#面向對象程序設計入門教程》
希望本文所述對大家C#程序設計有所幫助。
新聞熱點
疑難解答