Guest User

Untitled

a guest
Jul 19th, 2018
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.60 KB | None | 0 0
  1. // Created by Chroniccommand! AES Decrypter. This encrypts then decrypts some words!
  2. using System;
  3. using System.IO;
  4. using System.Text;
  5. using System.Security.Cryptography;
  6.  
  7. public class RijndaelSimple
  8. {
  9. public static string Encrypt(string plainText,
  10. string passPhrase,
  11. string saltValue,
  12. string hashAlgorithm,
  13. int passwordIterations,
  14. string initVector,
  15. int keySize)
  16. {
  17.  
  18. byte[] initVectorBytes = Encoding.ASCII.GetBytes(initVector);
  19. byte[] saltValueBytes = Encoding.ASCII.GetBytes(saltValue);
  20. byte[] plainTextBytes = Encoding.UTF8.GetBytes(plainText);
  21. PasswordDeriveBytes password = new PasswordDeriveBytes(
  22. passPhrase,
  23. saltValueBytes,
  24. hashAlgorithm,
  25. passwordIterations);
  26.  
  27. byte[] keyBytes = password.GetBytes(keySize / 8);
  28.  
  29.  
  30. RijndaelManaged symmetricKey = new RijndaelManaged();
  31.  
  32.  
  33. symmetricKey.Mode = CipherMode.CBC;
  34.  
  35. ICryptoTransform encryptor = symmetricKey.CreateEncryptor(
  36. keyBytes,
  37. initVectorBytes);
  38.  
  39. MemoryStream memoryStream = new MemoryStream();
  40.  
  41. CryptoStream cryptoStream = new CryptoStream(memoryStream,
  42. encryptor,
  43. CryptoStreamMode.Write);
  44. // Start encrypting.
  45. cryptoStream.Write(plainTextBytes, 0, plainTextBytes.Length);
  46.  
  47. // Finish encrypting.
  48. cryptoStream.FlushFinalBlock();
  49. byte[] cipherTextBytes = memoryStream.ToArray();
  50.  
  51. // Close both streams.
  52. memoryStream.Close();
  53. cryptoStream.Close();
  54. string cipherText = Convert.ToBase64String(cipherTextBytes);
  55.  
  56. // Return encrypted string.
  57. return cipherText;
  58. }
  59. public static string Decrypt(string cipherText,
  60. string passPhrase,
  61. string saltValue,
  62. string hashAlgorithm,
  63. int passwordIterations,
  64. string initVector,
  65. int keySize)
  66. {
  67.  
  68. byte[] initVectorBytes = Encoding.ASCII.GetBytes(initVector);
  69. byte[] saltValueBytes = Encoding.ASCII.GetBytes(saltValue);
  70.  
  71.  
  72. byte[] cipherTextBytes = Convert.FromBase64String(cipherText);
  73.  
  74.  
  75. PasswordDeriveBytes password = new PasswordDeriveBytes(
  76. passPhrase,
  77. saltValueBytes,
  78. hashAlgorithm,
  79. passwordIterations);
  80. byte[] keyBytes = password.GetBytes(keySize / 8);
  81. RijndaelManaged symmetricKey = new RijndaelManaged();
  82. symmetricKey.Mode = CipherMode.CBC;
  83. ICryptoTransform decryptor = symmetricKey.CreateDecryptor(
  84. keyBytes,
  85. initVectorBytes);
  86. MemoryStream memoryStream = new MemoryStream(cipherTextBytes);
  87. CryptoStream cryptoStream = new CryptoStream(memoryStream,
  88. decryptor,
  89. CryptoStreamMode.Read);
  90.  
  91. byte[] plainTextBytes = new byte[cipherTextBytes.Length];
  92. int decryptedByteCount = cryptoStream.Read(plainTextBytes,
  93. 0,
  94. plainTextBytes.Length);
  95.  
  96. memoryStream.Close();
  97. cryptoStream.Close();
  98.  
  99. string plainText = Encoding.UTF8.GetString(plainTextBytes,
  100. 0,
  101. decryptedByteCount);
  102.  
  103. return plainText;
  104. }
  105.  
  106. public class RijndaelSimpleTest
  107. {
  108. [STAThread]
  109. static void Main(string[] args)
  110. {
  111. string plainText = "My name is chroniccommand and this is a highly advanced AES encrypted file for a test to try to decrypt this very long plain text code and see how good my AES decryper can work"; // Setting plain text to encrypt
  112. string passPhrase = "Pas5ph@se"; // can be any string just change to your pass phrse
  113. string saltValue = "s@ltValue"; // Can be any salt value. Setting to example s@ltValue value
  114. string hashAlgorithm = "SHA1"; // can also be "MD5" instead of SHA1 but this depends on your hash
  115. int passwordIterations = 2; // can be any number... Setting to 2 by default
  116. string initVector = "@1B2c3D4e5F6g7h8"; // HASSSSS to be 16 wonderfull bytes
  117. int keySize = 256; // Can be 192, or 256
  118.  
  119. Console.WriteLine(string.Format("Plaintext : {0}", plainText));
  120.  
  121. string cipherText = RijndaelSimple.Encrypt(plainText,
  122. passPhrase,
  123. saltValue,
  124. hashAlgorithm,
  125. passwordIterations,
  126. initVector,
  127. keySize);
  128.  
  129. Console.WriteLine(String.Format("Encrypted : {0}", cipherText));
  130.  
  131. plainText = RijndaelSimple.Decrypt(cipherText,
  132. passPhrase,
  133. saltValue,
  134. hashAlgorithm,
  135. passwordIterations,
  136. initVector,
  137. keySize);
  138.  
  139. Console.WriteLine(String.Format("Decrypted : {0}", plainText));
  140.  
  141. //End of Decryption unit
  142. }
  143. }
  144. }
Add Comment
Please, Sign In to add comment