using System.Security.Cryptography;
public static void Main()
string token = "4444444444444444";
Console.WriteLine(token);
var encreptedToken = EncryptTextWithPassword(token);
Console.WriteLine(encreptedToken);
var decryptedToken = DecryptTextWithPassword(encreptedToken);
Console.WriteLine(decryptedToken);
public static string EncryptTextWithPassword(string value)
string password = "your-encryption-password";
var salt = Guid.NewGuid().ToByteArray();
using var aes = Aes.Create();
var key = new Rfc2898DeriveBytes(password, salt, 10000, HashAlgorithmName.SHA256).GetBytes(32);
using var encryptor = aes.CreateEncryptor(aes.Key, iv);
using var ms = new MemoryStream();
using (var cs = new CryptoStream(ms, encryptor, CryptoStreamMode.Write))
using (var sw = new StreamWriter(cs))
var encryptedContent = ms.ToArray();
var result = new byte[salt.Length + iv.Length + encryptedContent.Length];
Buffer.BlockCopy(salt, 0, result, 0, salt.Length);
Buffer.BlockCopy(iv, 0, result, salt.Length, iv.Length);
Buffer.BlockCopy(encryptedContent, 0, result, salt.Length + iv.Length, encryptedContent.Length);
return Convert.ToBase64String(result);
public static string DecryptTextWithPassword(string value)
string password = "your-encryption-password";
var fullCipher = Convert.FromBase64String(value);
using var aes = Aes.Create();
var ivLength = aes.BlockSize / 8;
var salt = new byte[saltLength];
var iv = new byte[ivLength];
var cipher = new byte[fullCipher.Length - saltLength - ivLength];
Buffer.BlockCopy(fullCipher, 0, salt, 0, saltLength);
Buffer.BlockCopy(fullCipher, saltLength, iv, 0, ivLength);
Buffer.BlockCopy(fullCipher, saltLength + ivLength, cipher, 0, cipher.Length);
var key = new Rfc2898DeriveBytes(password, salt, 10000, HashAlgorithmName.SHA256).GetBytes(32);
using var decryptor = aes.CreateDecryptor(aes.Key, aes.IV);
using var ms = new MemoryStream(cipher);
using var cs = new CryptoStream(ms, decryptor, CryptoStreamMode.Read);
using var sr = new StreamReader(cs);