C#代碼實(shí)現(xiàn)對AES加密解密
ES(The Advanced Encryption Standard)是美國國家標(biāo)準(zhǔn)與技術(shù)研究所用于加密電子數(shù)據(jù)的規(guī)范。它被預(yù)期能成為人們公認(rèn)的加密包括金融、電信和政府?dāng)?shù)字信息的方法。
本文實(shí)例為大家介紹C#實(shí)現(xiàn)對AES加密解密的詳細(xì)代碼,分享給大家供大家參考,具體內(nèi)容如下
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Security.Cryptography;
using System.IO;
namespace AESDemo
{
public static class AESHelper
{
/// <summary>
/// AES加密
/// </summary>
/// <param name="Data">被加密的明文</param>
/// <param name="Key">密鑰</param>
/// <param name="Vector">向量</param>
/// <returns>密文</returns>
public static Byte[] AESEncrypt(Byte[] Data, String Key, String Vector)
{
Byte[] bKey = new Byte[32];
Array.Copy(Encoding.UTF8.GetBytes(Key.PadRight(bKey.Length)), bKey, bKey.Length);
Byte[] bVector = new Byte[16];
Array.Copy(Encoding.UTF8.GetBytes(Vector.PadRight(bVector.Length)), bVector, bVector.Length);
Byte[] Cryptograph = null; // 加密后的密文
Rijndael Aes = Rijndael.Create();
try
{
// 開辟一塊內(nèi)存流
using (MemoryStream Memory = new MemoryStream())
{
// 把內(nèi)存流對象包裝成加密流對象
using (CryptoStream Encryptor = new CryptoStream(Memory,
Aes.CreateEncryptor(bKey, bVector),
CryptoStreamMode.Write))
{
// 明文數(shù)據(jù)寫入加密流
Encryptor.Write(Data, 0, Data.Length);
Encryptor.FlushFinalBlock();
Cryptograph = Memory.ToArray();
}
}
}
catch
{
Cryptograph = null;
}
return Cryptograph;
}
/// <summary>
/// AES解密
/// </summary>
/// <param name="Data">被解密的密文</param>
/// <param name="Key">密鑰</param>
/// <param name="Vector">向量</param>
/// <returns>明文</returns>
public static Byte[] AESDecrypt(Byte[] Data, String Key, String Vector)
{
Byte[] bKey = new Byte[32];
Array.Copy(Encoding.UTF8.GetBytes(Key.PadRight(bKey.Length)), bKey, bKey.Length);
Byte[] bVector = new Byte[16];
Array.Copy(Encoding.UTF8.GetBytes(Vector.PadRight(bVector.Length)), bVector, bVector.Length);
Byte[] original = null; // 解密后的明文
Rijndael Aes = Rijndael.Create();
try
{
// 開辟一塊內(nèi)存流,存儲密文
using (MemoryStream Memory = new MemoryStream(Data))
{
// 把內(nèi)存流對象包裝成加密流對象
using (CryptoStream Decryptor = new CryptoStream(Memory,
Aes.CreateDecryptor(bKey, bVector),
CryptoStreamMode.Read))
{
// 明文存儲區(qū)
using (MemoryStream originalMemory = new MemoryStream())
{
Byte[] Buffer = new Byte[1024];
Int32 readBytes = 0;
while ((readBytes = Decryptor.Read(Buffer, 0, Buffer.Length)) > 0)
{
originalMemory.Write(Buffer, 0, readBytes);
}
original = originalMemory.ToArray();
}
}
}
}
catch
{
original = null;
}
return original;
}
}
}
不使用向量的方式:
public static class AESCrypto
{
/// <summary>
/// IV向量為固定值
/// </summary>
//private static byte[] _iV = {
// 85, 60, 12, 116,
// 99, 189, 173, 19,
// 138, 183, 232, 248,
// 82, 232, 200, 242
//};
public static byte[] Decrypt(byte[] encryptedBytes, byte[] key)
{
MemoryStream mStream = new MemoryStream( encryptedBytes );
//mStream.Write( encryptedBytes, 0, encryptedBytes.Length );
//mStream.Seek( 0, SeekOrigin.Begin );
RijndaelManaged aes = new RijndaelManaged( );
aes.Mode = CipherMode.ECB;
aes.Padding = PaddingMode.PKCS7;
aes.KeySize = 128;
aes.Key = key;
//aes.IV = _iV;
CryptoStream cryptoStream = new CryptoStream( mStream, aes.CreateDecryptor( ), CryptoStreamMode.Read );
try {
byte[] tmp = new byte[ encryptedBytes.Length + 32 ];
int len = cryptoStream.Read( tmp, 0, encryptedBytes.Length + 32 );
byte[] ret = new byte[ len ];
Array.Copy( tmp, 0, ret, 0, len );
return ret;
}
finally {
cryptoStream.Close( );
mStream.Close( );
aes.Clear( );
}
}
public static byte[] Encrypt(byte[] plainBytes, byte[] key)
{
MemoryStream mStream = new MemoryStream();
RijndaelManaged aes = new RijndaelManaged();
aes.Mode = CipherMode.ECB;
aes.Padding = PaddingMode.PKCS7;
aes.KeySize = 128;
//aes.Key = _key;
aes.Key = key;
//aes.IV = _iV;
CryptoStream cryptoStream = new CryptoStream(mStream, aes.CreateEncryptor(), CryptoStreamMode.Write);
try
{
cryptoStream.Write(plainBytes, 0, plainBytes.Length);
cryptoStream.FlushFinalBlock();
return mStream.ToArray();
}
finally
{
cryptoStream.Close();
mStream.Close();
aes.Clear();
}
}
}
希望通過這篇文章大家對AES加密解密有所了解,對C#程序設(shè)計有所幫助。
相關(guān)文章
C#文件內(nèi)容檢索的功能實(shí)現(xiàn)代碼
本文詳細(xì)介紹了如何構(gòu)建和優(yōu)化一個高效的文件內(nèi)容檢索系統(tǒng),包括索引構(gòu)建、多線程處理、文件監(jiān)控、搜索優(yōu)化、錯誤處理和日志記錄等方面的內(nèi)容,并提供了一個簡化的C#demo示例,感興趣的朋友一起看看吧2024-10-10
C#多線程學(xué)習(xí)之(五)使用定時器進(jìn)行多線程的自動管理
這篇文章主要介紹了C#多線程學(xué)習(xí)之使用定時器進(jìn)行多線程的自動管理,實(shí)例分析了C#使用timer定時器類實(shí)現(xiàn)針對多線程的自動管理功能,非常具有實(shí)用價值,需要的朋友可以參考下2015-04-04
C#實(shí)現(xiàn)文件讀寫到SQLite數(shù)據(jù)庫
這篇文章主要為大家詳細(xì)介紹了使用?C#?將文件讀寫到?SQLite?數(shù)據(jù)庫的幾種方法,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以參考一下2025-01-01

