Facebook
From Chocolate Camel, 5 Years ago, written in C#.
Embed
Download Paste or View Raw
Hits: 224
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Linq;
  5. using System.Security.Cryptography;
  6. using System.Text;
  7. using System.Threading.Tasks;
  8.  
  9. namespace BSK04042019
  10. {
  11.     class Program
  12.     {
  13.         static void Main(string[] args)
  14.         {
  15.             string original = "";
  16.             int caseSwitch = 0;
  17.             int a = 9;
  18.             do
  19.             {
  20.                 Console.WriteLine("Szyfrowanie z pliku wybierz 1\nSzyfrowanie wpisanego tekstu wybierz 2\n");
  21.                 int.TryParse(Console.ReadLine(), out caseSwitch);
  22.                 switch (caseSwitch)
  23.                 {
  24.                     case 1:
  25.                         {
  26.                             original = File.ReadAllText(@"E:\Fabian\Test.txt");
  27.                             Console.WriteLine("Contents of Test.txt = {0}", original);
  28.  
  29.                             // Create a new instance of the Aes
  30.                             // class.  This generates a new key and initialization
  31.                             // vector (IV).
  32.                             using (Aes myAes = Aes.Create())
  33.                             {
  34.  
  35.                                 // Encrypt the string to an array of bytes.
  36.                                 byte[] encrypted = EncryptStringToBytes_Aes(original, myAes.Key, myAes.IV);
  37.  
  38.                                 // Decrypt the bytes to a string.
  39.                                 string roundtrip = DecryptStringFromBytes_Aes(encrypted, myAes.Key, myAes.IV);
  40.                                 var str = Encoding.Default.GetString(encrypted);
  41.                                 //Display the original data and the decrypted data.
  42.                                 Console.WriteLine("Original:   {0}", original);
  43.                                 Console.WriteLine("Encrypted:   {0}", str);
  44.                                 Console.WriteLine("Round Trip: {0}", roundtrip);
  45.                             }
  46.                             break;
  47.                         }
  48.                     case 2:
  49.                         {
  50.                             Console.WriteLine("Podaj text do zaszyfrowania: ");
  51.                             original = Console.ReadLine();
  52.                             using (Aes myAes = Aes.Create())
  53.                             {
  54.  
  55.                                 byte[] encrypted = EncryptStringToBytes_Aes(original, myAes.Key, myAes.IV);
  56.                                 string roundtrip = DecryptStringFromBytes_Aes(encrypted, myAes.Key, myAes.IV);
  57.  
  58.                                 var str = Encoding.Default.GetString(encrypted);
  59.                                 Console.WriteLine("Original:   {0}", original);
  60.                                 Console.WriteLine("Encrypted:   {0}", str);
  61.                                 Console.WriteLine("Round Trip: {0}", roundtrip);
  62.                             }
  63.                             break;
  64.                         }
  65.                     default:
  66.                         {
  67.                             break;
  68.                         }
  69.  
  70.                 }
  71.  
  72.                 Console.WriteLine("Aby kontynuować wybierz '9'");
  73.                 a = Convert.ToInt32(Console.ReadLine());
  74.             } while (a == 9);
  75.  
  76.             Console.ReadKey();
  77.         }
  78.         static byte[] EncryptStringToBytes_Aes(string plainText, byte[] Key, byte[] IV)
  79.         {
  80.             // Check arguments.
  81.             if (plainText == null || plainText.Length <= 0)
  82.                 throw new ArgumentNullException("plainText");
  83.             if (Key == null || Key.Length <= 0)
  84.                 throw new ArgumentNullException("Key");
  85.             if (IV == null || IV.Length <= 0)
  86.                 throw new ArgumentNullException("IV");
  87.             byte[] encrypted;
  88.  
  89.             // Create an Aes object
  90.             // with the specified key and IV.
  91.             using (Aes aesAlg = Aes.Create())
  92.             {
  93.                 aesAlg.Key = Key;
  94.                 aesAlg.IV = IV;
  95.  
  96.                 // Create an encryptor to perform the stream transform.
  97.                 ICryptoTransform encryptor = aesAlg.CreateEncryptor(aesAlg.Key, aesAlg.IV);
  98.  
  99.                 // Create the streams used for encryption.
  100.                 using (MemoryStream msEncrypt = new MemoryStream())
  101.                 {
  102.                     using (CryptoStream csEncrypt = new CryptoStream(msEncrypt, encryptor, CryptoStreamMode.Write))
  103.                     {
  104.                         using (StreamWriter swEncrypt = new StreamWriter(csEncrypt))
  105.                         {
  106.                             //Write all data to the stream.
  107.                             swEncrypt.Write(plainText);
  108.                         }
  109.                         encrypted = msEncrypt.ToArray();
  110.                     }
  111.                 }
  112.             }
  113.  
  114.             // Return the encrypted bytes from the memory stream.
  115.             return encrypted;
  116.         }
  117.  
  118.         static string DecryptStringFromBytes_Aes(byte[] cipherText, byte[] Key, byte[] IV)
  119.         {
  120.             // Check arguments.
  121.             if (cipherText == null || cipherText.Length <= 0)
  122.                 throw new ArgumentNullException("cipherText");
  123.             if (Key == null || Key.Length <= 0)
  124.                 throw new ArgumentNullException("Key");
  125.             if (IV == null || IV.Length <= 0)
  126.                 throw new ArgumentNullException("IV");
  127.  
  128.             // Declare the string used to hold
  129.             // the decrypted text.
  130.             string plaintext = null;
  131.  
  132.             // Create an Aes object
  133.             // with the specified key and IV.
  134.             using (Aes aesAlg = Aes.Create())
  135.             {
  136.                 aesAlg.Key = Key;
  137.                 aesAlg.IV = IV;
  138.  
  139.                 // Create a decryptor to perform the stream transform.
  140.                 ICryptoTransform decryptor = aesAlg.CreateDecryptor(aesAlg.Key, aesAlg.IV);
  141.  
  142.                 // Create the streams used for decryption.
  143.                 using (MemoryStream msDecrypt = new MemoryStream(cipherText))
  144.                 {
  145.                     using (CryptoStream csDecrypt = new CryptoStream(msDecrypt, decryptor, CryptoStreamMode.Read))
  146.                     {
  147.                         using (StreamReader srDecrypt = new StreamReader(csDecrypt))
  148.                         {
  149.  
  150.                             // Read the decrypted bytes from the decrypting stream
  151.                             // and place them in a string.
  152.                             plaintext = srDecrypt.ReadToEnd();
  153.                         }
  154.                     }
  155.                 }
  156.  
  157.             }
  158.             return plaintext;
  159.  
  160.         }
  161.  
  162.     }
  163. }
  164.