Advertisement
toko214

rsa

May 28th, 2016
255
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.94 KB | None | 0 0
  1. using System;
  2. using System.Security.Cryptography;
  3. using System.Text;
  4. using System.Collections.Generic;
  5. using System.IO;
  6.  
  7. class RSACSPSample
  8. {
  9.  
  10.     static void Main()
  11.     {
  12.  
  13.         try
  14.         {
  15.             //Create a UnicodeEncoder to convert between byte array and string.
  16.             UnicodeEncoding ByteConverter = new UnicodeEncoding();
  17.  
  18.             Console.Write("Enter A Text To Encrypt: ");
  19.             string onevs1 = Console.ReadLine();
  20.             //Create byte arrays to hold original, encrypted, and decrypted data.
  21.             byte[] dataToEncrypt = ByteConverter.GetBytes(onevs1);
  22.             byte[] encryptedData;
  23.             byte[] decryptedData;
  24.  
  25.             //Create a new instance of RSACryptoServiceProvider to generate
  26.             //public and private key data.
  27.             using (RSACryptoServiceProvider RSA = new RSACryptoServiceProvider())
  28.             {
  29.                 encryptedData = RSAEncrypt(dataToEncrypt, RSA.ExportParameters(false), false);
  30.                 // decryptedData = RSADecrypt(encryptedData, RSA.ExportParameters(true), false);
  31.                 var cypherText = Convert.ToBase64String(encryptedData);
  32.                 Console.WriteLine("Eecrypted plaintext: {0}", cypherText);
  33.                 Console.WriteLine();
  34.                 var ChyperUser = Console.ReadLine();
  35.                 byte[] decData = Base64Decode(ChyperUser);
  36.                 decryptedData = RSADecrypt(decData, RSA.ExportParameters(true), false);
  37.                 Console.WriteLine("Decrypted plaintext: {0}", ByteConverter.GetString(decryptedData));
  38.  
  39.  
  40.             }
  41.         }
  42.         catch (Exception e)
  43.         {
  44.             //Catch this exception in case the encryption did
  45.             //not succeed.
  46.             Console.WriteLine("Encryption failed.");
  47.  
  48.         }
  49.     }
  50.     public static byte[] Base64Decode(string base64EncodedData)
  51.     {
  52.         return Convert.FromBase64String(base64EncodedData);
  53.  
  54.     }
  55.  
  56.     static public byte[] RSAEncrypt(byte[] DataToEncrypt, RSAParameters RSAKeyInfo, bool DoOAEPPadding)
  57.     {
  58.         try
  59.         {
  60.             byte[] encryptedData;
  61.             //Create a new instance of RSACryptoServiceProvider.
  62.             using (RSACryptoServiceProvider RSA = new RSACryptoServiceProvider())
  63.             {
  64.  
  65.                 //Import the RSA Key information. This only needs
  66.                 //toinclude the public key information.
  67.                 RSA.ImportParameters(RSAKeyInfo);
  68.  
  69.                 //Encrypt the passed byte array and specify OAEP padding.  
  70.                 //OAEP padding is only available on Microsoft Windows XP or
  71.                 //later.  
  72.                 encryptedData = RSA.Encrypt(DataToEncrypt, DoOAEPPadding);
  73.             }
  74.             return encryptedData;
  75.         }
  76.         //Catch and display a CryptographicException  
  77.         //to the console.
  78.         catch (CryptographicException e)
  79.         {
  80.             Console.WriteLine(e.Message);
  81.  
  82.             return null;
  83.         }
  84.  
  85.     }
  86.     static public byte[] RSADecrypt(byte[] DataToDecrypt, RSAParameters RSAKeyInfo, bool DoOAEPPadding)
  87.     {
  88.         try
  89.         {
  90.             byte[] decryptedData;
  91.             //Create a new instance of RSACryptoServiceProvider.
  92.             RSACryptoServiceProvider RSA = new RSACryptoServiceProvider();
  93.             //Import the RSA Key information. This needs
  94.             //to include the private key information.
  95.             RSA.ImportParameters(RSAKeyInfo);
  96.             //Decrypt the passed byte array and specify OAEP padding.  
  97.             //OAEP padding is only available on Microsoft Windows XP or
  98.             //later.  
  99.             decryptedData = RSA.Decrypt(DataToDecrypt, DoOAEPPadding);
  100.             return decryptedData;
  101.         }
  102.         //Catch and display a CryptographicException  
  103.         //to the console.
  104.         catch (CryptographicException e)
  105.         {
  106.             Console.WriteLine(e.ToString());
  107.  
  108.             return null;
  109.         }
  110.  
  111.     }
  112. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement