国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > C# > 正文

C#實(shí)現(xiàn)的AES加密解密完整實(shí)例

2020-01-24 01:03:35
字體:
供稿:網(wǎng)友

本文實(shí)例講述了C#實(shí)現(xiàn)的AES加密解密。分享給大家供大家參考,具體如下:

/****************************************************************** * 創(chuàng)建人: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:關(guān)于加密解密感興趣的朋友還可以參考本站在線工具:

密碼安全性在線檢測:

tools.VeVB.COm/password/my_password_safe

高強(qiáng)度密碼生成器:

tools.VeVB.COm/password/CreateStrongPassword

MD5在線加密工具:

tools.VeVB.COm/password/CreateMD5Password

迅雷、快車、旋風(fēng)URL加密/解密工具:

tools.VeVB.COm/password/urlrethunder

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

tools.VeVB.COm/password/hash_encrypt

更多關(guān)于C#相關(guān)內(nèi)容還可查看本站專題:《C#加密與解密算法與技巧總結(jié)》、《C#窗體操作技巧匯總》、《C#常見控件用法教程》、《WinForm控件用法總結(jié)》、《C#程序設(shè)計(jì)之線程使用技巧總結(jié)》、《C#操作Excel技巧總結(jié)》、《C#中XML文件操作技巧匯總》、《C#數(shù)據(jù)結(jié)構(gòu)與算法教程》、《C#數(shù)組操作技巧總結(jié)》及《C#面向?qū)ο蟪绦蛟O(shè)計(jì)入門教程

希望本文所述對(duì)大家C#程序設(shè)計(jì)有所幫助。

發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 常宁市| 阜南县| 瑞安市| 元阳县| 阜新市| 民乐县| 荆州市| 五大连池市| 无锡市| 获嘉县| 麦盖提县| 九龙县| 承德市| 岳池县| 三穗县| 宣化县| 武鸣县| 双峰县| 浦城县| 巨鹿县| 芜湖市| 福海县| 昌都县| 许昌市| 台南市| 闽清县| 达孜县| 社会| 调兵山市| 赤峰市| 通化县| 鲁山县| 西华县| 香河县| 搜索| 陆川县| 临西县| 乌审旗| 尉氏县| 五家渠市| 辛集市|