MoMoT

Untitled

Nov 23rd, 2016
118
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.68 KB | None | 0 0
  1. using System;
  2. using System.Security.Cryptography;
  3. using System.Text;
  4. using System.IO;
  5.  
  6.  
  7.  
  8. public class Test
  9.  
  10. {
  11.  
  12.  public static void Main()
  13.  
  14.  {
  15.         try
  16.         {
  17.             //Create a UnicodeEncoder to convert between byte array and string.
  18.             UnicodeEncoding ByteConverter = new UnicodeEncoding();
  19.  
  20.             //Create byte arrays to hold original, encrypted, and decrypted data.
  21.             byte[] dataToEncrypt = ByteConverter.GetBytes("Hello Word!!");
  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.  
  30.                 //Pass the data to ENCRYPT, the public key information
  31.                 //(using RSACryptoServiceProvider.ExportParameters(false),
  32.                 //and a boolean flag specifying no OAEP padding.
  33.                 encryptedData = RSAEncrypt(dataToEncrypt, RSA.ExportParameters(false), false);
  34.  
  35.                 //Pass the data to DECRYPT, the private key information
  36.                 //(using RSACryptoServiceProvider.ExportParameters(true),
  37.                 //and a boolean flag specifying no OAEP padding.
  38.                 decryptedData = RSADecrypt(encryptedData, RSA.ExportParameters(true), false);
  39.  
  40.                 //Display the decrypted plaintext to the console.
  41.                 Console.WriteLine("Input word: " + "Hello Word!!");
  42.               // КУСОК ГАВНА СТРОКОЙ НИЖЕ
  43.                 Console.WriteLine("Encrypted plaintext: {0}", ByteConverter.GetString(encryptedData));
  44.                 Console.WriteLine("Decrypted plaintext: {0}", ByteConverter.GetString(decryptedData));
  45.             }
  46.         }
  47.         catch (ArgumentNullException)
  48.         {
  49.             //Catch this exception in case the encryption did
  50.             //not succeed.
  51.             Console.WriteLine("Encryption failed.");
  52.  
  53.         }
  54.  
  55.         string word = ("Hello Word!!");
  56.         Console.Write("Hash: ");
  57.         Console.WriteLine(EncodePassword(word));
  58.         Console.ReadKey();
  59.     }
  60.     static public byte[] RSAEncrypt(byte[] DataToEncrypt, RSAParameters RSAKeyInfo, bool DoOAEPPadding)
  61.     {
  62.         try
  63.         {
  64.             byte[] encryptedData;
  65.             //Create a new instance of RSACryptoServiceProvider.
  66.             using (RSACryptoServiceProvider RSA = new RSACryptoServiceProvider())
  67.             {
  68.  
  69.                 //Import the RSA Key information. This only needs
  70.                 //toinclude the public key information.
  71.                 RSA.ImportParameters(RSAKeyInfo);
  72.  
  73.                 //Encrypt the passed byte array and specify OAEP padding.  
  74.                 //OAEP padding is only available on Microsoft Windows XP or
  75.                 //later.  
  76.                 encryptedData = RSA.Encrypt(DataToEncrypt, DoOAEPPadding);
  77.             }
  78.             return encryptedData;
  79.         }
  80.         //Catch and display a CryptographicException  
  81.         //to the console.
  82.         catch (CryptographicException e)
  83.         {
  84.             Console.WriteLine(e.Message);
  85.  
  86.             return null;
  87.         }
  88.  
  89.     }
  90.  
  91.     static public byte[] RSADecrypt(byte[] DataToDecrypt, RSAParameters RSAKeyInfo, bool DoOAEPPadding)
  92.     {
  93.         try
  94.         {
  95.             byte[] decryptedData;
  96.             //Create a new instance of RSACryptoServiceProvider.
  97.             using (RSACryptoServiceProvider RSA = new RSACryptoServiceProvider())
  98.             {
  99.                 //Import the RSA Key information. This needs
  100.                 //to include the private key information.
  101.                 RSA.ImportParameters(RSAKeyInfo);
  102.  
  103.                 //Decrypt the passed byte array and specify OAEP padding.  
  104.                 //OAEP padding is only available on Microsoft Windows XP or
  105.                 //later.  
  106.                 decryptedData = RSA.Decrypt(DataToDecrypt, DoOAEPPadding);
  107.             }
  108.             return decryptedData;
  109.         }
  110.         //Catch and display a CryptographicException  
  111.         //to the console.
  112.         catch (CryptographicException e)
  113.         {
  114.             Console.WriteLine(e.ToString());
  115.  
  116.             return null;
  117.         }
  118.  
  119.     }
  120.  
  121.     public static string EncodePassword(string originalPassword)
  122.  
  123.     {
  124.         Byte[] originalBytes;
  125.         Byte[] encodedBytes;
  126.         MD5 md5;
  127.         md5 = new MD5CryptoServiceProvider();
  128.         originalBytes = ASCIIEncoding.Default.GetBytes(originalPassword);
  129.         encodedBytes = md5.ComputeHash(originalBytes);
  130.         return System.Text.RegularExpressions.Regex.Replace(BitConverter.ToString(encodedBytes), "-", "").ToLower();
  131.     }
  132.  
  133. }
Advertisement
Add Comment
Please, Sign In to add comment