38 lines
1.3 KiB
C#
38 lines
1.3 KiB
C#
using System.Security.Cryptography;
|
|
using System.Text;
|
|
|
|
namespace General_Ledger_Service.Helpers
|
|
{
|
|
public static class EncryptionHelper
|
|
{
|
|
private static readonly byte[] Key = Convert.FromBase64String("6pY8vR2Nf+Xz9WkLm4TqQzB7vA9uS1nE5jR3hG6fM8o=");
|
|
|
|
public static string Encrypt(string plainText)
|
|
{
|
|
using Aes aes = Aes.Create();
|
|
aes.Key = Key;
|
|
aes.GenerateIV();
|
|
|
|
using var encryptor = aes.CreateEncryptor();
|
|
byte[] plainBytes = Encoding.UTF8.GetBytes(plainText);
|
|
byte[] cipherBytes = encryptor.TransformFinalBlock(plainBytes, 0, plainBytes.Length);
|
|
|
|
return Convert.ToBase64String(aes.IV.Concat(cipherBytes).ToArray());
|
|
}
|
|
|
|
public static string Decrypt(string combinedText)
|
|
{
|
|
byte[] combinedBytes = Convert.FromBase64String(combinedText);
|
|
using Aes aes = Aes.Create();
|
|
aes.Key = Key;
|
|
|
|
byte[] iv = [.. combinedBytes.Take(16)];
|
|
byte[] cipherBytes = [.. combinedBytes.Skip(16)];
|
|
|
|
using var decryptor = aes.CreateDecryptor(aes.Key, iv);
|
|
byte[] plainBytes = decryptor.TransformFinalBlock(cipherBytes, 0, cipherBytes.Length);
|
|
|
|
return Encoding.UTF8.GetString(plainBytes);
|
|
}
|
|
}
|
|
} |