Advertisement
Guest User

UTHER<3

a guest
Mar 29th, 2017
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.52 KB | None | 0 0
  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 BoxyBot.Util
  10. {
  11. public static class StringCipher
  12. {
  13. // This constant is used to determine the keysize of the encryption algorithm in bits.
  14. // We divide this by 8 within the code below to get the equivalent number of bytes.
  15. private const int Keysize = 256;
  16.  
  17. // This constant determines the number of iterations for the password bytes generation function.
  18. private const int DerivationIterations = 1000;
  19.  
  20. public static string Encrypt(string plainText, string passPhrase)
  21. {
  22. // Salt and IV is randomly generated each time, but is preprended to encrypted cipher text
  23. // so that the same Salt and IV values can be used when decrypting.
  24. var saltStringBytes = Generate256BitsOfRandomEntropy();
  25. var ivStringBytes = Generate256BitsOfRandomEntropy();
  26. var plainTextBytes = Encoding.UTF8.GetBytes(plainText);
  27. using (var password = new Rfc2898DeriveBytes(passPhrase, saltStringBytes, DerivationIterations))
  28. {
  29. var keyBytes = password.GetBytes(Keysize / 8);
  30. using (var symmetricKey = new RijndaelManaged())
  31. {
  32. symmetricKey.BlockSize = 256;
  33. symmetricKey.Mode = CipherMode.CBC;
  34. symmetricKey.Padding = PaddingMode.PKCS7;
  35. using (var encryptor = symmetricKey.CreateEncryptor(keyBytes, ivStringBytes))
  36. {
  37. using (var memoryStream = new MemoryStream())
  38. {
  39. using (var cryptoStream = new CryptoStream(memoryStream, encryptor, CryptoStreamMode.Write))
  40. {
  41. cryptoStream.Write(plainTextBytes, 0, plainTextBytes.Length);
  42. cryptoStream.FlushFinalBlock();
  43. // Create the final bytes as a concatenation of the random salt bytes, the random iv bytes and the cipher bytes.
  44. var cipherTextBytes = saltStringBytes;
  45. cipherTextBytes = cipherTextBytes.Concat(ivStringBytes).ToArray();
  46. cipherTextBytes = cipherTextBytes.Concat(memoryStream.ToArray()).ToArray();
  47. memoryStream.Close();
  48. cryptoStream.Close();
  49. return Convert.ToBase64String(cipherTextBytes);
  50. }
  51. }
  52. }
  53. }
  54. }
  55. }
  56.  
  57. public static string Decrypt(string cipherText, string passPhrase)
  58. {
  59. // Get the complete stream of bytes that represent:
  60. // [32 bytes of Salt] + [32 bytes of IV] + [n bytes of CipherText]
  61. var cipherTextBytesWithSaltAndIv = Convert.FromBase64String(cipherText);
  62. // Get the saltbytes by extracting the first 32 bytes from the supplied cipherText bytes.
  63. var saltStringBytes = cipherTextBytesWithSaltAndIv.Take(Keysize / 8).ToArray();
  64. // Get the IV bytes by extracting the next 32 bytes from the supplied cipherText bytes.
  65. var ivStringBytes = cipherTextBytesWithSaltAndIv.Skip(Keysize / 8).Take(Keysize / 8).ToArray();
  66. // Get the actual cipher text bytes by removing the first 64 bytes from the cipherText string.
  67. var cipherTextBytes = cipherTextBytesWithSaltAndIv.Skip((Keysize / 8) * 2).Take(cipherTextBytesWithSaltAndIv.Length - ((Keysize / 8) * 2)).ToArray();
  68.  
  69. using (var password = new Rfc2898DeriveBytes(passPhrase, saltStringBytes, DerivationIterations))
  70. {
  71. var keyBytes = password.GetBytes(Keysize / 8);
  72. using (var symmetricKey = new RijndaelManaged())
  73. {
  74. symmetricKey.BlockSize = 256;
  75. symmetricKey.Mode = CipherMode.CBC;
  76. symmetricKey.Padding = PaddingMode.PKCS7;
  77. using (var decryptor = symmetricKey.CreateDecryptor(keyBytes, ivStringBytes))
  78. {
  79. using (var memoryStream = new MemoryStream(cipherTextBytes))
  80. {
  81. using (var cryptoStream = new CryptoStream(memoryStream, decryptor, CryptoStreamMode.Read))
  82. {
  83. var plainTextBytes = new byte[cipherTextBytes.Length];
  84. var decryptedByteCount = cryptoStream.Read(plainTextBytes, 0, plainTextBytes.Length);
  85. memoryStream.Close();
  86. cryptoStream.Close();
  87. return Encoding.UTF8.GetString(plainTextBytes, 0, decryptedByteCount);
  88. }
  89. }
  90. }
  91. }
  92. }
  93. }
  94.  
  95. private static byte[] Generate256BitsOfRandomEntropy()
  96. {
  97. var randomBytes = new byte[32]; // 32 Bytes will give us 256 bits.
  98. using (var rngCsp = new RNGCryptoServiceProvider())
  99. {
  100. // Fill the array with cryptographically secure random bytes.
  101. rngCsp.GetBytes(randomBytes);
  102. }
  103. return randomBytes;
  104. }
  105. }
  106. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement