Các thuật toán mã hóa bất đối xứng trong NET

Một phần của tài liệu Quá trình tìm hiểu tổng quan về mã hóa và lớp Crytography trong .NET trong việc bảo vệ thông tin (Trang 45 - 50)

.NET Framework thực thi thuật toán mã hóa bất đối xứng thông qua lớp cơ sở AsymmetricAlgorithm cũng giống như việc sử dụng các thuật toán mã hóa đối xứng thông qua lớp SymmetricAlgorithm. Sau đây là sơ đồ các lớp trong lớp AsymmetricAlgorithm :

Hình 4.3 : Thuật toán mã hóa bất đối xứng trong lớp SymmetricAlgorithm

Bảng các phương thức trong lớp AsymmetricAlgorithm :

Phương thức chung Ý nghĩa

KeySize Kích thước của khóa tính theo bits

LegalKeySizes Giá trị kích thước khóa hợp lệ tính theo byte của thuật toán mã hóa bất đối xứng hiện tại

KeyExchangeAlgorithm Chỉ định thuật toán trao đổi khóa được sử dụng và cách thức trao đổi khóa công khai và khóa bí mật

SignatureAlgorithm Chỉ định tên thuật toán được sử dụng để ký trên đối tượng hiện thời

FromXmlString() Tái tạo lại đối tượng thuật toán mã hóa bất đối xứng từ 1 file XML

ToXmlString() Trả về một thể hiện XML cho đối tượng bất đối xứng đang sử dụng

Chúng ta cùng xem một ví dụ về cách sử dụng thuật toán mã hóa bất đối xứng trong .NET :

-Sinh cặp khóa công khai và khóa bí mật :

{

try {

RSACryptoServiceProvider myRSA = new RSACryptoServiceProvider(nKeySize);

myRSA.PersistKeyInCsp = false; string szPrivkey, szPubkey;

szPrivkey = myRSA.ToXmlString(true); szPubkey = myRSA.ToXmlString(false);

FileStream fsPub = new FileStream(szKeyName+"Pub.key", FileMode.Create,FileAccess.Write);

byte[] bytePubkey = ASCIIEncoding.ASCII.GetBytes(szPubkey); fsPub.Write(bytePubkey, 0, bytePubkey.Length);

fsPub.Close();

FileStream fsPriv = new

FileStream(szKeyName+"Priv.key",FileMode.Create, FileAccess.Write); byte[] bytePrivkey = ASCIIEncoding.ASCII.GetBytes(szPrivkey);

fsPriv.Write(bytePrivkey, 0, bytePrivkey.Length); fsPriv.Close();

} catch { } }

static void EncryptFile(string szFileInput, string szFileEnc, string szPubKey) {

try {

FileStream fsPubkey = new FileStream(szPubKey, FileMode.Open, FileAccess.Read);

FileStream fsInput = new FileStream(szFileInput, FileMode.Open, FileAccess.Read);

FileStream fsOutput = new FileStream(szFileEnc, FileMode.Create, FileAccess.Write);

RSACryptoServiceProvider myRSA = new RSACryptoServiceProvider(); byte[] bytePubkey = new byte[fsPubkey.Length];

fsPubkey.Read(bytePubkey, 0, bytePubkey.Length); (adsbygoogle = window.adsbygoogle || []).push({});

myRSA.FromXmlString(ASCIIEncoding.ASCII.GetString(bytePubkey, 0, bytePubkey.Length));

byte[] byteInput = new byte[fsInput.Length]; fsInput.Read(byteInput, 0, byteInput.Length); byte[] byteEnc;

byteEnc = myRSA.Encrypt(byteInput, false); fsOutput.Write(byteEnc, 0, byteEnc.Length);

fsInput.Close(); fsPubkey.Close(); fsOutput.Close(); }

catch (CryptographicException ex) {Console.WriteLine(ex.Message);} }

-Giải mã với khóa bí mật :

static void DecryptFile(string szFileInput, string szFileEnc, string szPrivKey) {

try {

FileStream fsPrivkey = new FileStream(szPrivKey, FileMode.Open, FileAccess.Read);

FileStream fsInput = new FileStream(szFileInput, FileMode.Open, FileAccess.Read);

FileStream fsOutput = new FileStream(szFileEnc, FileMode.Create, FileAccess.Write);

RSACryptoServiceProvider myRSA = new RSACryptoServiceProvider(); byte[] bytePrivkey = new byte[fsPrivkey.Length];

fsPrivkey.Read(bytePrivkey, 0, bytePrivkey.Length);

myRSA.FromXmlString(ASCIIEncoding.ASCII.GetString(bytePrivkey, 0, bytePrivkey.Length));

byte[] byteInput = new byte[fsInput.Length]; fsInput.Read(byteInput, 0, byteInput.Length); byte[] byteDec;

byteDec = myRSA.Decrypt(byteInput, false); fsOutput.Write(byteDec, 0, byteDec.Length); fsInput.Close();

fsPrivkey.Close(); fsOutput.Close(); }

catch (CryptographicException ex) { Console.WriteLine(ex.Message);}

}

Một phần của tài liệu Quá trình tìm hiểu tổng quan về mã hóa và lớp Crytography trong .NET trong việc bảo vệ thông tin (Trang 45 - 50)