Files
Core_Be/Helpers/EncryptionHelper.cs
T
Dhananjaya99 bffc9b1686 first commit
2026-09-28 11:44:26 +05:30

35 lines
1.2 KiB
C#

using System.Security.Cryptography;
using System.Text;
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).ToArray();
byte[] cipherBytes = combinedBytes.Skip(16).ToArray();
using var decryptor = aes.CreateDecryptor(aes.Key, iv);
byte[] plainBytes = decryptor.TransformFinalBlock(cipherBytes, 0, cipherBytes.Length);
return Encoding.UTF8.GetString(plainBytes);
}
}