Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Security.Cryptography;
- using System.IO;
- namespace RSA_Demo
- {
- class RSA_Test1
- {
- public void GenRSA()
- {
- try
- {
- #region Sinh khoa
- RSACryptoServiceProvider rsaProvider = new RSACryptoServiceProvider();
- // Export public key
- string publicKey = rsaProvider.ToXmlString(false);
- // Export private/public key pair
- string privateKey = rsaProvider.ToXmlString(true);
- // in ra man hinh
- Console.WriteLine("private key----------------");
- Console.WriteLine(privateKey);
- Console.WriteLine("public key-----------------");
- Console.WriteLine(publicKey);
- // Ban ro
- string _message = "Noi dung ban ro";
- Console.WriteLine("Message: ");
- Console.WriteLine(_message);
- #endregion
- #region ma hoa
- byte[] plainBytes = Encoding.Unicode.GetBytes(_message);
- byte[] encryptedBytes = rsaProvider.Encrypt(plainBytes, false);
- string encrypt_message = Encoding.UTF8.GetString(encryptedBytes);
- Console.WriteLine("encrypted...");
- Console.WriteLine(encrypt_message);
- #endregion
- #region giai ma
- byte[] decryptedBytes = rsaProvider.Decrypt(encryptedBytes, false);
- string decrypted_string = Encoding.Unicode.GetString(decryptedBytes);
- Console.WriteLine("decrypted...");
- Console.WriteLine(decrypted_string);
- #endregion
- }
- catch (Exception ex)
- {
- // Any errors? Show them
- Console.WriteLine("Exception generating a new key pair! More info:");
- Console.WriteLine(ex.Message);
- }
- Console.ReadKey();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment