You are on page 1of 3

Aes Class

Namespace:
System.Security.Cryptography
Assembly:
System.Security.Cryptography.Algorithms.dll
Inheritance
Object
SymmetricAlgorithm
Aes
Derived
System.Security.Cryptography.AesCng
System.Security.Cryptography.AesCryptoServiceProvider
System.Security.Cryptography.AesManaged
Attributes
UnsupportedOSPlatformAttribute

Đoạn chương trình sau mô phỏng cách mã hoá và giải mã dữ liệu sử dụng lớp AES

C#
using System;
using System.IO;
using System.Security.Cryptography;

namespace Aes_Example
{
class AesExample
{
public static void Main()
{
string original = "Here is some data to encrypt!";

// Tạo một thực thể mới cho the Aes


// tạo một khóa mới và khởi tạo
using (Aes myAes = Aes.Create())
{

// Mã hóa chuỗi thành một mảng byte.


byte[] encrypted = EncryptStringToBytes_Aes(original,
myAes.Key, myAes.IV);

// Giải mã các byte thành một chuỗi.


string roundtrip = DecryptStringFromBytes_Aes(encrypted,
myAes.Key, myAes.IV);

// Hiển thị dữ liệu Original và dữ liệu đã giải mã.


Console.WriteLine("Original: {0}", original);
Console.WriteLine("Round Trip: {0}", roundtrip);
}
}
// EncryptStringToBytes_Aes dùng để mã hóa một chuỗi bằng Tiêu
chuẩn Mã hóa Nâng cao (AES).
static byte[] EncryptStringToBytes_Aes(string plainText, byte[] Key,
byte[] IV)
{
// kiểm tra arguments.
if (plainText == null || plainText.Length <= 0)
throw new ArgumentNullException("plainText");
if (Key == null || Key.Length <= 0)
throw new ArgumentNullException("Key");
if (IV == null || IV.Length <= 0)
throw new ArgumentNullException("IV");
byte[] encrypted;

// Tạo Aes object


// với khóa được chỉ định (key) and IV.
using (Aes aesAlg = Aes.Create())
{
aesAlg.Key = Key;
aesAlg.IV = IV;

// Tạo một bộ mã hóa để thực hiện chuyển đổi luồng.


ICryptoTransform encryptor = aesAlg.CreateEncryptor(aesAlg.Key, aesAlg.IV);

// Tạo các luồng được sử dụng để mã hóa.


using (MemoryStream msEncrypt = new MemoryStream())
{
using (CryptoStream csEncrypt = new
CryptoStream(msEncrypt, encryptor, CryptoStreamMode.Write))
{
using (StreamWriter swEncrypt = new
StreamWriter(csEncrypt))
{
//Ghi tất cả dữ liệu vào luồng.
swEncrypt.Write(plainText);
}
encrypted = msEncrypt.ToArray();
}
}
}

// Trả lại các byte được mã hóa từ luồng bộ nhớ.


return encrypted;
}

static string DecryptStringFromBytes_Aes(byte[] cipherText, byte[]


Key, byte[] IV)
{
// kiểm tra arguments.
if (cipherText == null || cipherText.Length <= 0)
throw new ArgumentNullException("cipherText");
if (Key == null || Key.Length <= 0)
throw new ArgumentNullException("Key");
if (IV == null || IV.Length <= 0)
throw new ArgumentNullException("IV");

// khai báo chuỗi


// text được mã hóa
string plaintext = null;

// tạo Aes object


// với khóa được chỉ định (key) and IV.
using (Aes aesAlg = Aes.Create())
{
aesAlg.Key = Key;
aesAlg.IV = IV;

//Tạo một bộ mã hóa để thực hiện chuyển đổi luồng (transform).


ICryptoTransform decryptor =
aesAlg.CreateDecryptor(aesAlg.Key, aesAlg.IV);

//Tạo các luồng được sử dụng để mã hóa.


using (MemoryStream msDecrypt = new MemoryStream(cipherText))
{
using (CryptoStream csDecrypt = new
CryptoStream(msDecrypt, decryptor, CryptoStreamMode.Read))
{
using (StreamReader srDecrypt = new
StreamReader(csDecrypt))
{

// Đọc các byte được giải mã từ luồng giải mã


// đặt chúng trong một chuỗi..
plaintext = srDecrypt.ReadToEnd();
}
}
}
}

return plaintext;
}
}
}

You might also like