C#實(shí)現(xiàn)的AES加密解密完整實(shí)例
本文實(shí)例講述了C#實(shí)現(xiàn)的AES加密解密。分享給大家供大家參考,具體如下:
/******************************************************************
* 創(chuàng)建人:HTL
* 說(shuō)明: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)于加密解密感興趣的朋友還可以參考本站在線工具:
密碼安全性在線檢測(cè):
tools.jb51.net/password/my_password_safe
高強(qiáng)度密碼生成器:
tools.jb51.net/password/CreateStrongPassword
MD5在線加密工具:
tools.jb51.net/password/CreateMD5Password
迅雷、快車(chē)、旋風(fēng)URL加密/解密工具:
tools.jb51.net/password/urlrethunder
在線散列/哈希算法加密工具:
tools.jb51.net/password/hash_encrypt
更多關(guān)于C#相關(guān)內(nèi)容還可查看本站專(zhuān)題:《C#加密與解密算法與技巧總結(jié)》、《C#窗體操作技巧匯總》、《C#常見(jiàn)控件用法教程》、《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ì)入門(mén)教程》
希望本文所述對(duì)大家C#程序設(shè)計(jì)有所幫助。
相關(guān)文章
C#實(shí)現(xiàn)的微信網(wǎng)頁(yè)授權(quán)操作邏輯封裝示例
這篇文章主要介紹了C#實(shí)現(xiàn)的微信網(wǎng)頁(yè)授權(quán)操作邏輯封裝,分析了微信網(wǎng)頁(yè)授權(quán)操作的原理、步驟并給出了C#實(shí)現(xiàn)的網(wǎng)頁(yè)授權(quán)操作邏輯封裝類(lèi),需要的朋友可以參考下2016-10-10
C#更新文本框textbox數(shù)據(jù)同時(shí)刪除舊數(shù)據(jù)問(wèn)題
這篇文章主要介紹了C#更新文本框textbox數(shù)據(jù)同時(shí)刪除舊數(shù)據(jù)問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-04-04
C#編寫(xiě)Windows服務(wù)程序詳細(xì)步驟詳解(圖文)
本文介紹了如何用C#創(chuàng)建、安裝、啟動(dòng)、監(jiān)控、卸載簡(jiǎn)單的Windows Service 的內(nèi)容步驟和注意事項(xiàng),需要的朋友可以參考下2017-09-09
C#實(shí)現(xiàn)上位機(jī)與歐姆龍PLC通訊(FINS)
這篇文章主要介紹了C#實(shí)現(xiàn)上位機(jī)與歐姆龍PLC通訊(FINS)方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-05-05
C#使用csvhelper實(shí)現(xiàn)csv的基本操作
CsvHelper 是一個(gè)用于讀寫(xiě) CSV 文件的.NET庫(kù),極其快速,靈活且易于使用,CsvHelper 建立在.NET Standard 2.0 之上,幾乎可以在任何地方運(yùn)行,本文給大家介紹了C#使用csvhelper實(shí)現(xiàn)csv的基本操作,需要的朋友可以參考下2024-07-07
關(guān)于C#泛型列表List<T>的基本用法總結(jié)
本篇文章主要是對(duì)C#中泛型列表List<T>的基本用法進(jìn)行了詳細(xì)的總結(jié)介紹,需要的朋友可以過(guò)來(lái)參考下,希望對(duì)大家有所幫助2014-01-01

