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 Lớp cryptogaraphy và một số loại mã hóa được hỗ trợ bởi NET (Trang 45 - 51)

.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 :

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

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

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 :

static void Generatakey(string szKeyName, int nKeySize) {

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 { } }

-Mã hóa với khóa công khai

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); (adsbygoogle = window.adsbygoogle || []).push({});

RSACryptoServiceProvider myRSA = new RSACryptoServiceProvider();

byte[] bytePubkey = new byte[fsPubkey.Length]; fsPubkey.Read(bytePubkey, 0, bytePubkey.Length);

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);} }

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 Lớp cryptogaraphy và một số loại mã hóa được hỗ trợ bởi NET (Trang 45 - 51)