thieumao

RSA

Sep 3rd, 2013
140
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.27 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text;
  4. using System.Security.Cryptography;
  5. namespace RSA
  6. {
  7.     class Program
  8.     {
  9.         static void Main(string[] args)
  10.         {
  11.  
  12.             RSACryptoServiceProvider myrsa = new RSACryptoServiceProvider();
  13.  
  14.             System.Text.ASCIIEncoding encoding = new System.Text.ASCIIEncoding();//Encode String to Convert to Bytes
  15.  
  16.             string data = "Thieu Mao is here";//whatever you want to encrypt
  17.  
  18.             Byte[] newdata = encoding.GetBytes(data);//convert to Bytes
  19.  
  20.             Byte[] encrypted = myrsa.Encrypt(newdata, false);
  21.  
  22.             Console.WriteLine("Encrypted Data:  ");
  23.             for (int i = 0; i < encrypted.Length; i++)
  24.             {
  25.                 Console.Write("{0} ", encrypted[i]);
  26.             }
  27.             Console.WriteLine();
  28.             Console.WriteLine();
  29.  
  30.             Byte[] decrypted = myrsa.Decrypt(encrypted, false);//decrypt
  31.             Console.WriteLine("Decrypted Data:  ");
  32.  
  33.             string dData = encoding.GetString(decrypted); //encode bytes back to string
  34.             for (int i = 0; i < decrypted.Length; i++)
  35.             {
  36.                 Console.Write("{0}", dData[i]);
  37.             }
  38.  
  39.             Console.ReadKey();
  40.         }
  41.     }
  42. }
Advertisement
Add Comment
Please, Sign In to add comment