diff options
| author | Lucas Faria Mendes <lucas.fariamo08@gmail.com> | 2026-03-30 13:35:25 +0000 |
|---|---|---|
| committer | Lucas Faria Mendes <lucas.fariamo08@gmail.com> | 2026-03-30 13:35:25 +0000 |
| commit | 674ca83ba9243a9e95a7568c797668dab6aee26a (patch) | |
| tree | 4a905b3fb1d827665a34d63f67bc5559f8e7235b /Gestor.Common/Gestor.Common.Helpers/EncryptionHelper.cs | |
| download | gestor-674ca83ba9243a9e95a7568c797668dab6aee26a.tar.gz gestor-674ca83ba9243a9e95a7568c797668dab6aee26a.zip | |
feat: upload files
Diffstat (limited to 'Gestor.Common/Gestor.Common.Helpers/EncryptionHelper.cs')
| -rw-r--r-- | Gestor.Common/Gestor.Common.Helpers/EncryptionHelper.cs | 106 |
1 files changed, 106 insertions, 0 deletions
diff --git a/Gestor.Common/Gestor.Common.Helpers/EncryptionHelper.cs b/Gestor.Common/Gestor.Common.Helpers/EncryptionHelper.cs new file mode 100644 index 0000000..b2102d2 --- /dev/null +++ b/Gestor.Common/Gestor.Common.Helpers/EncryptionHelper.cs @@ -0,0 +1,106 @@ +using System;
+using System.IO;
+using System.Runtime.CompilerServices;
+using System.Security.Cryptography;
+using System.Text;
+
+namespace Gestor.Common.Helpers
+{
+ public static class EncryptionHelper
+ {
+ private readonly static byte[] Salt;
+
+ private readonly static string EncryptionKey;
+
+ static EncryptionHelper()
+ {
+ EncryptionHelper.Salt = new byte[] { 38, 220, 255, 0, 173, 237, 122, 238, 197, 254, 7, 175, 77, 8, 34, 60 };
+ EncryptionHelper.EncryptionKey = string.Format("aGG3r{0}#w3BDz$", 1012);
+ }
+
+ public static string Decrypt(this string cipher)
+ {
+ string str;
+ if (string.IsNullOrEmpty(cipher))
+ {
+ return null;
+ }
+ try
+ {
+ byte[] numArray = Convert.FromBase64String(cipher);
+ if ((int)numArray.Length >= 16)
+ {
+ byte[] numArray1 = numArray.DecryptBytes();
+ str = (numArray1 == null ? cipher : Encoding.UTF8.GetString(numArray1));
+ }
+ else
+ {
+ str = cipher;
+ }
+ }
+ catch (Exception exception)
+ {
+ str = cipher;
+ }
+ return str;
+ }
+
+ public static byte[] DecryptBytes(this byte[] plainTextBytes)
+ {
+ byte[] array;
+ try
+ {
+ using (Rfc2898DeriveBytes rfc2898DeriveByte = new Rfc2898DeriveBytes(EncryptionHelper.EncryptionKey, EncryptionHelper.Salt))
+ {
+ using (Rijndael bytes = Rijndael.Create())
+ {
+ bytes.Key = rfc2898DeriveByte.GetBytes(32);
+ bytes.IV = rfc2898DeriveByte.GetBytes(16);
+ using (MemoryStream memoryStream = new MemoryStream())
+ {
+ using (CryptoStream cryptoStream = new CryptoStream(memoryStream, bytes.CreateDecryptor(), CryptoStreamMode.Write))
+ {
+ cryptoStream.Write(plainTextBytes, 0, (int)plainTextBytes.Length);
+ cryptoStream.FlushFinalBlock();
+ array = memoryStream.ToArray();
+ }
+ }
+ }
+ }
+ }
+ catch (Exception exception)
+ {
+ array = null;
+ }
+ return array;
+ }
+
+ public static string Encrypt(this string plain)
+ {
+ return Convert.ToBase64String(Encoding.UTF8.GetBytes(plain).EncryptBytes());
+ }
+
+ public static byte[] EncryptBytes(this byte[] plainTextBytes)
+ {
+ byte[] array;
+ using (Rfc2898DeriveBytes rfc2898DeriveByte = new Rfc2898DeriveBytes(EncryptionHelper.EncryptionKey, EncryptionHelper.Salt))
+ {
+ using (Rijndael bytes = Rijndael.Create())
+ {
+ bytes.Key = rfc2898DeriveByte.GetBytes(32);
+ bytes.IV = rfc2898DeriveByte.GetBytes(16);
+ using (MemoryStream memoryStream = new MemoryStream())
+ {
+ using (CryptoStream cryptoStream = new CryptoStream(memoryStream, bytes.CreateEncryptor(), CryptoStreamMode.Write))
+ {
+ cryptoStream.Write(plainTextBytes, 0, (int)plainTextBytes.Length);
+ cryptoStream.FlushFinalBlock();
+ array = memoryStream.ToArray();
+ }
+ }
+ }
+ }
+ return array;
+ }
+ }
+}
\ No newline at end of file |