Guest User

Untitled

a guest
Jun 20th, 2020
108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 5.10 KB | None | 0 0
  1. private const int Keysize = 256;
  2.         private const int DerivationIterations = 1000;
  3.         public static string Encrypt(string plainText, string passPhrase)
  4.         {
  5.             try
  6.             {
  7.                 // Salt and IV is randomly generated each time, but is preprended to encrypted cipher text
  8.                 // so that the same Salt and IV values can be used when decrypting.  
  9.                 var saltStringBytes = Generate256BitsOfRandomEntropy();
  10.                 var ivStringBytes = Generate256BitsOfRandomEntropy();
  11.                 var plainTextBytes = Encoding.UTF8.GetBytes(plainText);
  12.                 using (var password = new Rfc2898DeriveBytes(passPhrase, saltStringBytes, DerivationIterations))
  13.                 {
  14.                     var keyBytes = password.GetBytes(Keysize / 8);
  15.                     using (var symmetricKey = new RijndaelManaged())
  16.                     {
  17.                         symmetricKey.BlockSize = 256;
  18.                         symmetricKey.Mode = CipherMode.CBC;
  19.                         symmetricKey.Padding = PaddingMode.PKCS7;
  20.                         using (var encryptor = symmetricKey.CreateEncryptor(keyBytes, ivStringBytes))
  21.                         {
  22.                             using (var memoryStream = new MemoryStream())
  23.                             {
  24.                                 using (var cryptoStream = new CryptoStream(memoryStream, encryptor, CryptoStreamMode.Write))
  25.                                 {
  26.                                     cryptoStream.Write(plainTextBytes, 0, plainTextBytes.Length);
  27.                                     cryptoStream.FlushFinalBlock();
  28.                                     // Create the final bytes as a concatenation of the random salt bytes, the random iv bytes and the cipher bytes.
  29.                                     var cipherTextBytes = saltStringBytes;
  30.                                     cipherTextBytes = cipherTextBytes.Concat(ivStringBytes).ToArray();
  31.                                     cipherTextBytes = cipherTextBytes.Concat(memoryStream.ToArray()).ToArray();
  32.                                     memoryStream.Close();
  33.                                     cryptoStream.Close();
  34.                                     return Convert.ToBase64String(cipherTextBytes);
  35.                                 }
  36.                             }
  37.                         }
  38.                     }
  39.                 }
  40.             }
  41.             catch (Exception ex)
  42.             {
  43.                 Clipboard.SetText(ex.Message);
  44.                 MessageBox.Show("Message not sent due encryption error.\n\n" + ex.Message, "Anoca - Encryption error", MessageBoxButtons.OK, MessageBoxIcon.Error);
  45.                 return "ENC ERROR";
  46.             }
  47.         }
  48.        
  49.         public static string Decrypt(string cipherText)
  50.         {
  51.             string passPhrase = Properties.Settings.Default.localID;
  52.             // Get the complete stream of bytes that represent:
  53.             // [32 bytes of Salt] + [32 bytes of IV] + [n bytes of CipherText]
  54.             var cipherTextBytesWithSaltAndIv = Convert.FromBase64String(cipherText);
  55.             // Get the saltbytes by extracting the first 32 bytes from the supplied cipherText bytes.
  56.             var saltStringBytes = cipherTextBytesWithSaltAndIv.Take(Keysize / 8).ToArray();
  57.             // Get the IV bytes by extracting the next 32 bytes from the supplied cipherText bytes.
  58.             var ivStringBytes = cipherTextBytesWithSaltAndIv.Skip(Keysize / 8).Take(Keysize / 8).ToArray();
  59.             // Get the actual cipher text bytes by removing the first 64 bytes from the cipherText string.
  60.             var cipherTextBytes = cipherTextBytesWithSaltAndIv.Skip((Keysize / 8) * 2).Take(cipherTextBytesWithSaltAndIv.Length - ((Keysize / 8) * 2)).ToArray();
  61.  
  62.  
  63.             using (var password = new Rfc2898DeriveBytes(passPhrase, saltStringBytes, DerivationIterations))
  64.             {
  65.                 var keyBytes = password.GetBytes(Keysize / 8);
  66.                 using (var symmetricKey = new RijndaelManaged())
  67.                 {
  68.                     symmetricKey.BlockSize = 256;
  69.                     symmetricKey.Mode = CipherMode.CBC;
  70.                     symmetricKey.Padding = PaddingMode.PKCS7;
  71.                     using (var decryptor = symmetricKey.CreateDecryptor(keyBytes, ivStringBytes))
  72.                     {
  73.                         using (var memoryStream = new MemoryStream(cipherTextBytes))
  74.                         {
  75.                             using (var cryptoStream = new CryptoStream(memoryStream, decryptor, CryptoStreamMode.Read))
  76.                             {
  77.                                 var plainTextBytes = new byte[cipherTextBytes.Length];
  78.                                 var decryptedByteCount = cryptoStream.Read(plainTextBytes, 0, plainTextBytes.Length);
  79.                                 memoryStream.Close();
  80.                                 cryptoStream.Close();
  81.                                 return Encoding.UTF8.GetString(plainTextBytes, 0, decryptedByteCount);
  82.                             }
  83.                         }
  84.                     }
  85.                 }
  86.             }
  87.         }
Add Comment
Please, Sign In to add comment