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 ConsoleApplication1
- {
- class RSA_Test1
- {
- public void GenRSA() {
- ////Generate a public/private key pair.
- //RSACryptoServiceProvider RSA = new RSACryptoServiceProvider();
- ////Save the public key information to an RSAParameters structure.
- //RSAParameters RSAKeyInfo = RSA.ExportParameters(false);
- #region Sinh khoa
- CspParameters cspParams = null;
- RSACryptoServiceProvider rsaProvider = null;
- StreamWriter publicKeyFile = null;
- StreamWriter privateKeyFile = null;
- string publicKey = "";
- string privateKey = "";
- try
- {
- // Create a new key pair on target CSP
- //cspParams = new CspParameters();
- //cspParams.ProviderType = 1; // PROV_RSA_FULL
- ////cspParams.ProviderName; // CSP name
- //cspParams.Flags = CspProviderFlags.UseArchivableKey;
- //cspParams.KeyNumber = (int)KeyNumber.Exchange;
- // c1. rsaProvider = new RSACryptoServiceProvider(cspParams);
- // c2. rsaProvider = new RSACryptoServiceProvider(2048);
- // c2 rsaProvider = new RSACryptoServiceProvider(2048,cspParams);
- // c4.
- rsaProvider = new RSACryptoServiceProvider();
- // Export public key
- publicKey = rsaProvider.ToXmlString(false);
- // Write public key to file
- //publicKeyFile = File.CreateText(publicKeyFileName);
- //publicKeyFile.Write(publicKey);
- // Export private/public key pair
- privateKey = rsaProvider.ToXmlString(true);
- Console.WriteLine("private key----------------");
- Console.WriteLine(privateKey);
- Console.WriteLine("public key-----------------");
- Console.WriteLine(publicKey);
- // Write private/public key pair to file
- //privateKeyFile = File.CreateText(privateKeyFileName);
- //privateKeyFile.Write(privateKey);
- string _message = "Syria sẽ gây ngạc nhiên cho những kẻ xâm lược như đã làm .";
- Console.WriteLine("Message: ");
- Console.WriteLine(_message);
- #endregion
- #region ma hoa
- byte[] plainBytes = null;
- byte[] encryptedBytes = null;
- byte[] decryptedBytes = null;
- plainBytes = Encoding.Unicode.GetBytes(_message);
- encryptedBytes = rsaProvider.Encrypt(plainBytes, false);
- string encrypt_message = Encoding.UTF8.GetString(encryptedBytes);
- Console.WriteLine("encrypted...");
- Console.WriteLine(encrypt_message);
- #endregion
- #region giai ma
- decryptedBytes = rsaProvider.Decrypt(encryptedBytes, false);
- string decrypted_string = Encoding.Unicode.GetString(decryptedBytes);
- Console.WriteLine("decrypted...");
- Console.WriteLine(decrypted_string);
- Console.ReadKey();
- #endregion
- }
- catch (Exception ex)
- {
- // Any errors? Show them
- Console.WriteLine("Exception generating a new key pair! More info:");
- Console.WriteLine(ex.Message);
- Console.ReadKey();
- }
- finally
- {
- // Do some clean up if needed
- if (publicKeyFile != null)
- {
- publicKeyFile.Close();
- }
- if (privateKeyFile != null)
- {
- privateKeyFile.Close();
- }
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment