mrAnderson33

Шифрование симметричным ключом RSA c#(CryptoAPI)

Jun 14th, 2018
157
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.07 KB | None | 0 0
  1. using System;
  2. using System.Security.Cryptography;
  3. using System.Text;
  4.  
  5. namespace Lab04_cryptoAPI_
  6. {
  7.     class RSACSPSample
  8.     {
  9.  
  10.         static void Main()
  11.         {
  12.             Console.WriteLine("Введите строку для шифрования:");
  13.             var original = Console.ReadLine();
  14.             var dataToEncrypt = Encoding.Unicode.GetBytes(original);
  15.  
  16.             try
  17.             {
  18.                 using (RSACryptoServiceProvider RSA = new RSACryptoServiceProvider())
  19.                 {
  20.  
  21.                     //Pass the data to ENCRYPT, the public key information
  22.                     //(using RSACryptoServiceProvider.ExportParameters(false),
  23.                     //and a boolean flag specifying no OAEP padding.
  24.                     var encryptedData = RSAEncrypt(dataToEncrypt, RSA.ExportParameters(false), false);
  25.  
  26.                     Console.WriteLine($"Зашифрованное сообщение: {Encoding.Unicode.GetString(encryptedData)}");
  27.  
  28.                     //Pass the data to DECRYPT, the private key information
  29.                     //(using RSACryptoServiceProvider.ExportParameters(true),
  30.                     //and a boolean flag specifying no OAEP padding.
  31.                     var decryptedData = RSADecrypt(encryptedData, RSA.ExportParameters(true), false);
  32.  
  33.                     //Display the decrypted plaintext to the console.
  34.                     Console.WriteLine($"Рвсшифрованное сообщение: {Encoding.Unicode.GetString(decryptedData)}");
  35.                 }
  36.             }
  37.             catch (ArgumentNullException)
  38.             {
  39.                 //Catch this exception in case the encryption did
  40.                 //not succeed.
  41.                 Console.WriteLine("Encryption failed.");
  42.  
  43.             }
  44.         }
  45.  
  46.         static public byte[] RSAEncrypt(byte[] DataToEncrypt, RSAParameters RSAKeyInfo, bool DoOAEPPadding)
  47.         {
  48.             try
  49.             {
  50.                 byte[] encryptedData;
  51.                 //Create a new instance of RSACryptoServiceProvider.
  52.                 using (RSACryptoServiceProvider RSA = new RSACryptoServiceProvider())
  53.                 {
  54.  
  55.                     //Import the RSA Key information. This only needs
  56.                     //toinclude the public key information.
  57.                     RSA.ImportParameters(RSAKeyInfo);
  58.  
  59.                     //Encrypt the passed byte array and specify OAEP padding.  
  60.                     //OAEP padding is only available on Microsoft Windows XP or
  61.                     //later.  
  62.                     encryptedData = RSA.Encrypt(DataToEncrypt, DoOAEPPadding);
  63.                 }
  64.                 return encryptedData;
  65.             }
  66.             //Catch and display a CryptographicException  
  67.             //to the console.
  68.             catch (CryptographicException e)
  69.             {
  70.                 Console.WriteLine(e.Message);
  71.  
  72.                 return null;
  73.             }
  74.  
  75.         }
  76.  
  77.         static public byte[] RSADecrypt(byte[] DataToDecrypt, RSAParameters RSAKeyInfo, bool DoOAEPPadding)
  78.         {
  79.             try
  80.             {
  81.                 byte[] decryptedData;
  82.                 //Create a new instance of RSACryptoServiceProvider.
  83.                 using (RSACryptoServiceProvider RSA = new RSACryptoServiceProvider())
  84.                 {
  85.                     //Import the RSA Key information. This needs
  86.                     //to include the private key information.
  87.                     RSA.ImportParameters(RSAKeyInfo);
  88.  
  89.                     //Decrypt the passed byte array and specify OAEP padding.  
  90.                     //OAEP padding is only available on Microsoft Windows XP or
  91.                     //later.  
  92.                     decryptedData = RSA.Decrypt(DataToDecrypt, DoOAEPPadding);
  93.                 }
  94.                 return decryptedData;
  95.             }
  96.             //Catch and display a CryptographicException  
  97.             //to the console.
  98.             catch (CryptographicException e)
  99.             {
  100.                 Console.WriteLine(e.ToString());
  101.  
  102.                 return null;
  103.             }
  104.  
  105.         }
  106.     }
  107.  
  108. }
Advertisement
Add Comment
Please, Sign In to add comment