使用【System.Security.Cryptography】中的 AES 驗算法來為字串加解密
實作靜態類別 StringEncrypt 來提供字串加解密功能。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Security.Cryptography;
using System.IO;
namespace StringEncrypt
{
public static class StringEncrypt
{
/// <summary>
/// 字串加密(非對稱式)
/// </summary>
/// <param name="Source">加密前字串</param>
/// <param name="CryptoKey">加密金鑰</param>
/// <returns>加密後字串</returns>
public static string aesEncryptBase64(string SourceStr, string CryptoKey)
{
string encrypt = "";
try
{
AesCryptoServiceProvider aes = new AesCryptoServiceProvider();
MD5CryptoServiceProvider md5 = new MD5CryptoServiceProvider();
SHA256CryptoServiceProvider sha256 = new SHA256CryptoServiceProvider();
byte[] key = sha256.ComputeHash(Encoding.UTF8.GetBytes(CryptoKey));
byte[] iv = md5.ComputeHash(Encoding.UTF8.GetBytes(CryptoKey));
aes.Key = key;
aes.IV = iv;
byte[] dataByteArray = Encoding.UTF8.GetBytes(SourceStr);
using (MemoryStream ms = new MemoryStream())
using (CryptoStream cs = new CryptoStream(ms, aes.CreateEncryptor(), CryptoStreamMode.Write))
{
cs.Write(dataByteArray, 0, dataByteArray.Length);
cs.FlushFinalBlock();
encrypt = Convert.ToBase64String(ms.ToArray());
}
}
catch (Exception e)
{
System.Windows.Forms.MessageBox.Show(e.Message);
}
return encrypt;
}
/// <summary>
/// 字串解密(非對稱式)
/// </summary>
/// <param name="Source">解密前字串</param>
/// <param name="CryptoKey">解密金鑰</param>
/// <returns>解密後字串</returns>
public static string aesDecryptBase64(string SourceStr, string CryptoKey)
{
string decrypt = "";
try
{
AesCryptoServiceProvider aes = new AesCryptoServiceProvider();
MD5CryptoServiceProvider md5 = new MD5CryptoServiceProvider();
SHA256CryptoServiceProvider sha256 = new SHA256CryptoServiceProvider();
byte[] key = sha256.ComputeHash(Encoding.UTF8.GetBytes(CryptoKey));
byte[] iv = md5.ComputeHash(Encoding.UTF8.GetBytes(CryptoKey));
aes.Key = key;
aes.IV = iv;
byte[] dataByteArray = Convert.FromBase64String(SourceStr);
using (MemoryStream ms = new MemoryStream())
{
using (CryptoStream cs = new CryptoStream(ms, aes.CreateDecryptor(), CryptoStreamMode.Write))
{
cs.Write(dataByteArray, 0, dataByteArray.Length);
cs.FlushFinalBlock();
decrypt = Encoding.UTF8.GetString(ms.ToArray());
}
}
}
catch (Exception e)
{
System.Windows.Forms.MessageBox.Show(e.Message);
}
return decrypt;
}
}
}
應用範例:
【加密按鈕】的 Click 事件程式碼如下:
private void btn_Encrypt_Click(object sender, EventArgs e)
{
try
{
tb_Encrypt_After.Text = StringEncrypt.aesEncryptBase64(tb_Encrypt_Before.Text, tb_Encrypt_Key.Text);
tb_Decrypt_Before.Text = tb_Encrypt_After.Text;
tb_Decrypt_Key.Text = tb_Encrypt_Key.Text;
tb_Decrypt_After.Text = "";
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
按下【加密按鈕】後,產生加密後的字串,畫面如下:
【解密按鈕】的 Click 事件程式碼如下:
private void btn_Decrypt_Click(object sender, EventArgs e)
{
try
{
tb_Decrypt_After.Text = StringEncrypt.aesDecryptBase64(tb_Decrypt_Before.Text, tb_Decrypt_Key.Text);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
按下【解密按鈕】後,產生加密後的字串,畫面如下:
輕鬆簡單快速完成字串加解密功能。
參考資料:
- 王者歸來 C# 完全開發範例集(第三版) - 實例 046 對字串進行加密與解密 ..... P.3-19
- 余小章 @ 大內殿堂 - [C#.NET] 字串及檔案 利用 DES / AES 演算法加解密