Advertisement
Guest User

Untitled

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