Advertisement
mrAnderson33

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

Jun 14th, 2018
142
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 5.12 KB | None | 0 0
  1. using System;
  2. using System.IO;
  3. using System.Security.Cryptography;
  4. using System.Text;
  5.  
  6. namespace Lab03_cryptoAPI_
  7. {
  8.     class TripleDESManagedExample
  9.     {
  10.         public static void Main()
  11.         {
  12.  
  13.             Console.WriteLine("Введите строку для шифрования:");
  14.             var original = Console.ReadLine();
  15.  
  16.                 // Create a new instance of the TripleDESCryptoServiceProvider
  17.                 // class.  This generates a new key and initialization
  18.                 // vector (IV).
  19.             try
  20.             {
  21.                 using (var myTripleDES = new DESCryptoServiceProvider())
  22.                 {
  23.                     // Encrypt the string to an array of bytes.
  24.                     var encrypted = EncryptStringToBytes(original, myTripleDES.Key, myTripleDES.IV);
  25.  
  26.                     Console.WriteLine($"Зашифрованное сообщение: {Encoding.ASCII.GetString(encrypted)}");
  27.  
  28.                     // Decrypt the bytes to a string.
  29.                     var roundtrip = DecryptStringFromBytes(encrypted, myTripleDES.Key, myTripleDES.IV);
  30.  
  31.                     Console.WriteLine($"Рвсшифрованное сообщение: {roundtrip}");
  32.  
  33.                     //Display the original data and the decrypted data.
  34.                    // Console.WriteLine("Original:   {0}", original);
  35.                     //Console.WriteLine("Round Trip: {0}", roundtrip);
  36.                 }
  37.  
  38.             }
  39.             catch (Exception e)
  40.             {
  41.                 Console.WriteLine("Error: {0}", e.Message);
  42.             }
  43.         }
  44.         static byte[] EncryptStringToBytes(string plainText, byte[] Key, byte[] IV)
  45.         {
  46.             // Check arguments.
  47.             if (plainText == null || plainText.Length <= 0)
  48.                 throw new ArgumentNullException("plainText");
  49.             if (Key == null || Key.Length <= 0)
  50.                 throw new ArgumentNullException("Key");
  51.             if (IV == null || IV.Length <= 0)
  52.                 throw new ArgumentNullException("Key");
  53.             byte[] encrypted;
  54.             // Create an TripleDESCryptoServiceProvider object
  55.             // with the specified key and IV.
  56.             using (var tdsAlg = new DESCryptoServiceProvider())
  57.             {
  58.                 tdsAlg.Key = Key;
  59.                 tdsAlg.IV = IV;
  60.  
  61.                 // Create a decrytor to perform the stream transform.
  62.                 ICryptoTransform encryptor = tdsAlg.CreateEncryptor(tdsAlg.Key, tdsAlg.IV);
  63.  
  64.                 // Create the streams used for encryption.
  65.                 using (MemoryStream msEncrypt = new MemoryStream())
  66.                 {
  67.                     using (CryptoStream csEncrypt = new CryptoStream(msEncrypt, encryptor, CryptoStreamMode.Write))
  68.                     {
  69.                        // var data = Encoding.ASCII.GetBytes(plainText);
  70.  
  71.                         //csEncrypt.Write(data, 0,data.Length);
  72.                         using (StreamWriter swEncrypt = new StreamWriter(csEncrypt))
  73.                         {
  74.                             //Write all data to the stream.
  75.                             swEncrypt.Write(plainText);
  76.                         }
  77.                         encrypted = msEncrypt.ToArray();
  78.                     }
  79.                 }
  80.             }
  81.  
  82.             // Return the encrypted bytes from the memory stream.
  83.             return encrypted;
  84.  
  85.         }
  86.  
  87.         static string DecryptStringFromBytes(byte[] cipherText, byte[] Key, byte[] IV)
  88.         {
  89.             // Check arguments.
  90.             if (cipherText == null || cipherText.Length <= 0)
  91.                 throw new ArgumentNullException("cipherText");
  92.             if (Key == null || Key.Length <= 0)
  93.                 throw new ArgumentNullException("Key");
  94.             if (IV == null || IV.Length <= 0)
  95.                 throw new ArgumentNullException("Key");
  96.  
  97.             // Declare the string used to hold
  98.             // the decrypted text.
  99.             string plaintext = null;
  100.  
  101.             // Create an TripleDESCryptoServiceProvider object
  102.             // with the specified key and IV.
  103.             using (var tdsAlg = new DESCryptoServiceProvider())
  104.             {
  105.                 tdsAlg.Key = Key;
  106.                 tdsAlg.IV = IV;
  107.  
  108.                 // Create a decrytor to perform the stream transform.
  109.                 ICryptoTransform decryptor = tdsAlg.CreateDecryptor(tdsAlg.Key, tdsAlg.IV);
  110.  
  111.                 // Create the streams used for decryption.
  112.                 using (MemoryStream msDecrypt = new MemoryStream(cipherText))
  113.                 {
  114.                     using (CryptoStream csDecrypt = new CryptoStream(msDecrypt, decryptor, CryptoStreamMode.Read))
  115.                     {
  116.                         using (StreamReader srDecrypt = new StreamReader(csDecrypt))
  117.                         {
  118.  
  119.                             // Read the decrypted bytes from the decrypting stream
  120.                             // and place them in a string.
  121.                             plaintext = srDecrypt.ReadToEnd();
  122.                         }
  123.                     }
  124.                 }
  125.  
  126.             }
  127.  
  128.             return plaintext;
  129.  
  130.         }
  131.     }
  132. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement