Guest User

Untitled

a guest
Jun 21st, 2018
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.15 KB | None | 0 0
  1. using System;
  2. using System.IO;
  3. using System.Security.Cryptography;
  4. using System.Text;
  5.  
  6. namespace AES
  7. {
  8. class Program
  9. {
  10. public static byte[] salt;
  11. public static byte[] saltBytes;
  12.  
  13. static void Main(string[] args)
  14. {
  15. var a = "Hello";
  16. var bytes = Encoding.ASCII.GetBytes(a);
  17.  
  18. var secure = new Program();
  19.  
  20. var encrypted = secure.EncryptText("abc", "123");
  21.  
  22. Console.WriteLine(encrypted);
  23.  
  24. Console.WriteLine(secure.DecryptText(encrypted, "123"));
  25. }
  26.  
  27. public string EncryptText(string input, string password)
  28. {
  29. // Get the bytes of the string
  30. byte[] bytesToBeEncrypted = Encoding.UTF8.GetBytes(input);
  31. byte[] passwordBytes = Encoding.UTF8.GetBytes(password);
  32.  
  33. // Hash the password with SHA256
  34. passwordBytes = SHA256.Create().ComputeHash(passwordBytes);
  35.  
  36. byte[] bytesEncrypted = AES_Encrypt(bytesToBeEncrypted, passwordBytes);
  37.  
  38. string result = Convert.ToBase64String(bytesEncrypted);
  39.  
  40. return result;
  41. }
  42.  
  43. public string DecryptText(string input, string password)
  44. {
  45. // Get the bytes of the string
  46. byte[] bytesToBeDecrypted = Convert.FromBase64String(input);
  47. byte[] passwordBytes = Encoding.UTF8.GetBytes(password);
  48. passwordBytes = SHA256.Create().ComputeHash(passwordBytes);
  49.  
  50. byte[] bytesDecrypted = AES_Decrypt(bytesToBeDecrypted, passwordBytes);
  51.  
  52. string result = Encoding.UTF8.GetString(bytesDecrypted);
  53.  
  54. return result;
  55. }
  56.  
  57. public byte[] AES_Encrypt(byte[] bytesToBeEncrypted, byte[] passwordBytes)
  58. {
  59. byte[] encryptedBytes = null;
  60.  
  61. // Set your salt here, change it to meet your flavor:
  62. // The salt bytes must be at least 8 bytes.
  63.  
  64. new RNGCryptoServiceProvider().GetBytes(salt = new byte[32]);
  65. saltBytes = salt;
  66.  
  67. using (MemoryStream ms = new MemoryStream())
  68. {
  69. using (RijndaelManaged AES = new RijndaelManaged())
  70. {
  71.  
  72. AES.KeySize = 256;
  73. AES.BlockSize = 128;
  74.  
  75. var key = new Rfc2898DeriveBytes(passwordBytes, saltBytes, 1000);
  76. AES.Key = key.GetBytes(AES.KeySize / 8);
  77. var iv = new Rfc2898DeriveBytes(passwordBytes, saltBytes, 1000);
  78. AES.IV = iv.GetBytes(AES.BlockSize / 8);
  79.  
  80. AES.Mode = CipherMode.CBC;
  81.  
  82. using (var cs = new CryptoStream(ms, AES.CreateEncryptor(), CryptoStreamMode.Write))
  83. {
  84. cs.Write(bytesToBeEncrypted, 0, bytesToBeEncrypted.Length);
  85. cs.Close();
  86. }
  87. encryptedBytes = ms.ToArray();
  88. }
  89. }
  90.  
  91. return encryptedBytes;
  92. }
  93.  
  94. public byte[] AES_Decrypt(byte[] bytesToBeDecrypted, byte[] passwordBytes)
  95. {
  96.  
  97. byte[] decryptedBytes = null;
  98.  
  99. // Set your salt here, change it to meet your flavor:
  100. // The salt bytes must be at least 8 bytes.
  101.  
  102. using (MemoryStream ms = new MemoryStream())
  103. {
  104. using (RijndaelManaged AES = new RijndaelManaged())
  105. {
  106. AES.KeySize = 256;
  107. AES.BlockSize = 128;
  108.  
  109. var key = new Rfc2898DeriveBytes(passwordBytes, saltBytes, 1000);
  110. AES.Key = key.GetBytes(AES.KeySize / 8);
  111. var iv = new Rfc2898DeriveBytes(passwordBytes, saltBytes, 1000);
  112. AES.IV = iv.GetBytes(AES.BlockSize / 8);
  113.  
  114. AES.Mode = CipherMode.CBC;
  115.  
  116. using (var cs = new CryptoStream(ms, AES.CreateDecryptor(), CryptoStreamMode.Write))
  117. {
  118. cs.Write(bytesToBeDecrypted, 0, bytesToBeDecrypted.Length);
  119. cs.Close();
  120. }
  121. decryptedBytes = ms.ToArray();
  122. }
  123. }
  124.  
  125. return decryptedBytes;
  126. }
  127.  
  128. }
  129. }
Add Comment
Please, Sign In to add comment