thieumao

RSA_Test1.cs - ma hoa RSA

Aug 29th, 2013
151
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.11 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Security.Cryptography;
  6. using System.IO;
  7.  
  8. namespace ConsoleApplication1
  9. {
  10.     class RSA_Test1
  11.     {
  12.         public void GenRSA() {
  13.             ////Generate a public/private key pair.
  14.             //RSACryptoServiceProvider RSA = new RSACryptoServiceProvider();
  15.             ////Save the public key information to an RSAParameters structure.
  16.             //RSAParameters RSAKeyInfo = RSA.ExportParameters(false);
  17.             #region Sinh khoa
  18.             CspParameters cspParams = null;
  19.             RSACryptoServiceProvider rsaProvider = null;
  20.             StreamWriter publicKeyFile = null;
  21.             StreamWriter privateKeyFile = null;
  22.             string publicKey = "";
  23.             string privateKey = "";
  24.  
  25.  
  26.             try
  27.             {
  28.                 // Create a new key pair on target CSP
  29.                 //cspParams = new CspParameters();
  30.                 //cspParams.ProviderType = 1; // PROV_RSA_FULL
  31.                 ////cspParams.ProviderName; // CSP name
  32.                 //cspParams.Flags = CspProviderFlags.UseArchivableKey;
  33.                 //cspParams.KeyNumber = (int)KeyNumber.Exchange;
  34.                 // c1. rsaProvider = new RSACryptoServiceProvider(cspParams);
  35.                 // c2. rsaProvider = new RSACryptoServiceProvider(2048);
  36.                 // c2  rsaProvider = new RSACryptoServiceProvider(2048,cspParams);
  37.                 // c4.
  38.                 rsaProvider = new RSACryptoServiceProvider();
  39.  
  40.                 // Export public key
  41.                 publicKey = rsaProvider.ToXmlString(false);
  42.  
  43.                 // Write public key to file
  44.                 //publicKeyFile = File.CreateText(publicKeyFileName);
  45.                 //publicKeyFile.Write(publicKey);
  46.  
  47.                 // Export private/public key pair
  48.                 privateKey = rsaProvider.ToXmlString(true);
  49.  
  50.                 Console.WriteLine("private key----------------");
  51.                 Console.WriteLine(privateKey);
  52.                 Console.WriteLine("public key-----------------");
  53.                 Console.WriteLine(publicKey);
  54.                
  55.                 // Write private/public key pair to file
  56.                 //privateKeyFile = File.CreateText(privateKeyFileName);
  57.                 //privateKeyFile.Write(privateKey);
  58.                 string _message = "Syria sẽ gây ngạc nhiên cho những kẻ xâm lược như đã làm .";
  59.  
  60.                 Console.WriteLine("Message: ");
  61.                 Console.WriteLine(_message);
  62.                 #endregion
  63.  
  64.                 #region ma hoa
  65.                 byte[] plainBytes = null;
  66.                 byte[] encryptedBytes = null;
  67.                 byte[] decryptedBytes = null;
  68.  
  69.                 plainBytes = Encoding.Unicode.GetBytes(_message);
  70.                 encryptedBytes = rsaProvider.Encrypt(plainBytes, false);
  71.  
  72.                 string encrypt_message = Encoding.UTF8.GetString(encryptedBytes);
  73.                 Console.WriteLine("encrypted...");
  74.                 Console.WriteLine(encrypt_message);
  75.                
  76.                 #endregion
  77.  
  78.                 #region giai ma
  79.                 decryptedBytes = rsaProvider.Decrypt(encryptedBytes, false);
  80.                 string decrypted_string = Encoding.Unicode.GetString(decryptedBytes);
  81.                 Console.WriteLine("decrypted...");
  82.                 Console.WriteLine(decrypted_string);
  83.                 Console.ReadKey();
  84.  
  85.                 #endregion
  86.  
  87.             }
  88.             catch (Exception ex)
  89.             {
  90.                 // Any errors? Show them
  91.                 Console.WriteLine("Exception generating a new key pair! More info:");
  92.                 Console.WriteLine(ex.Message);
  93.                 Console.ReadKey();
  94.             }
  95.             finally
  96.             {
  97.                 // Do some clean up if needed
  98.                 if (publicKeyFile != null)
  99.                 {
  100.                     publicKeyFile.Close();
  101.                 }
  102.                 if (privateKeyFile != null)
  103.                 {
  104.                     privateKeyFile.Close();
  105.                 }
  106.             }
  107.  
  108.  
  109.         }
  110.     }
  111. }
Advertisement
Add Comment
Please, Sign In to add comment