using System.Globalization;
using System.Security.Cryptography;
using System.Threading.Tasks;
public async static Task Main()
var guid = "1ab4e20c-a2d1-40dd-a47e-b7da9c8a6e4d";
var encryptedPassword = await RijndaelSimple.EncryptAsync(guid, "resetPassword", "salesForceId");
var encryptClientData = Convert.ToBase64String(encryptedPassword);
var testArr = Convert.FromBase64String(encryptClientData.Replace(" ", "+"));
var originalPlainText = await RijndaelSimple.DecryptAsync(testArr, "resetPassword", "salesForceId");
var isEqual = string.Equals(guid, originalPlainText);
Console.WriteLine(isEqual);
public static class RijndaelSimple
private static string HashAlgorithm
private static int PasswordIterations
private static string InitVector
return "@1B2c3D4e5F6g7H8";
private static int KeySize
public static byte[] Encrypt(string plainText,
byte[] initVectorBytes = Encoding.ASCII.GetBytes(initVector);
byte[] saltValueBytes = Encoding.ASCII.GetBytes(saltValue);
byte[] plainTextBytes = Encoding.UTF8.GetBytes(plainText);
PasswordDeriveBytes password = new PasswordDeriveBytes(
byte[] keyBytes = password.GetBytes(keySize / 8);
RijndaelManaged symmetricKey = new RijndaelManaged()
ICryptoTransform encryptor = symmetricKey.CreateEncryptor(
MemoryStream memoryStream = null;
CryptoStream cryptoStream = null;
memoryStream = new MemoryStream();
cryptoStream = new CryptoStream(memoryStream, encryptor, CryptoStreamMode.Write);
cryptoStream.Write(plainTextBytes, 0, plainTextBytes.Length);
cryptoStream.FlushFinalBlock();
byte[] cipherTextBytes = memoryStream.ToArray();
if (memoryStream != null) memoryStream.Dispose();
if (cryptoStream != null) cryptoStream.Dispose();
public static async Task<byte[]> EncryptAsync(string plainText, string passPhrase, string saltValue, string hashAlgorithm, int passwordIterations, string initVector, int keySize, CancellationToken cancellationToken = default)
byte[] initVectorBytes = Encoding.UTF8.GetBytes(initVector);
byte[] saltValueBytes = Encoding.UTF8.GetBytes(saltValue);
byte[] plainTextBytes = Encoding.UTF8.GetBytes(plainText);
using PasswordDeriveBytes password = new PasswordDeriveBytes(passPhrase, saltValueBytes, hashAlgorithm, passwordIterations);
byte[] keyBytes = password.GetBytes(keySize / 8);
using RijndaelManaged symmetricKey = new RijndaelManaged
using ICryptoTransform encryptor = symmetricKey.CreateEncryptor(keyBytes, initVectorBytes);
using MemoryStream memoryStream = new MemoryStream();
using CryptoStream cryptoStream = new CryptoStream(memoryStream, encryptor, CryptoStreamMode.Write);
await cryptoStream.WriteAsync(plainTextBytes, 0, plainTextBytes.Length, cancellationToken).ConfigureAwait(false);
await cryptoStream.FlushFinalBlockAsync(cancellationToken).ConfigureAwait(false);
await memoryStream.FlushAsync(cancellationToken).ConfigureAwait(false);
return memoryStream.ToArray();
await cryptoStream.DisposeAsync().ConfigureAwait(false);
await memoryStream.DisposeAsync().ConfigureAwait(false);
public static string Decrypt(byte[] cipherTextBytes,
byte[] initVectorBytes = Encoding.ASCII.GetBytes(initVector);
byte[] saltValueBytes = Encoding.ASCII.GetBytes(saltValue);
PasswordDeriveBytes password = new PasswordDeriveBytes(
byte[] keyBytes = password.GetBytes(keySize / 8);
RijndaelManaged symmetricKey = new RijndaelManaged()
ICryptoTransform decryptor = symmetricKey.CreateDecryptor(
MemoryStream memoryStream = null;
memoryStream = new MemoryStream(cipherTextBytes);
using (CryptoStream cryptoStream = new CryptoStream(memoryStream, decryptor, CryptoStreamMode.Read))
byte[] plainTextBytes = new byte[cipherTextBytes.Length];
int decryptedByteCount = cryptoStream.Read(plainTextBytes, 0, plainTextBytes.Length);
plainText = Encoding.UTF8.GetString(plainTextBytes, 0, decryptedByteCount);
if (memoryStream != null) memoryStream.Dispose();
public static async Task<string> DecryptAsync(byte[] cipherTextBytes,
byte[] initVectorBytes = Encoding.UTF8.GetBytes(initVector);
byte[] saltValueBytes = Encoding.UTF8.GetBytes(saltValue);
using PasswordDeriveBytes password = new PasswordDeriveBytes(passPhrase, saltValueBytes, hashAlgorithm, passwordIterations);
byte[] keyBytes = password.GetBytes(keySize / 8);
using RijndaelManaged symmetricKey = new RijndaelManaged
using ICryptoTransform decryptor = symmetricKey.CreateDecryptor(keyBytes, initVectorBytes);
using MemoryStream memoryStream = new MemoryStream(cipherTextBytes);
using CryptoStream cryptoStream = new CryptoStream(memoryStream, decryptor, CryptoStreamMode.Read);
using StreamReader streamReader = new StreamReader(cryptoStream);
return await streamReader.ReadToEndAsync().ConfigureAwait(false);
await cryptoStream.DisposeAsync().ConfigureAwait(false);
await memoryStream.DisposeAsync().ConfigureAwait(false);
public static byte[] Encrypt(string message, string passPhrase, string salt)
return Encrypt(message, passPhrase, salt, HashAlgorithm, PasswordIterations, InitVector, KeySize);
public static byte[] Encrypt(string plainText, DateTime registrationDate)
return Encrypt(plainText, registrationDate.ToString(CultureInfo.InvariantCulture),
registrationDate.ToString(CultureInfo.InvariantCulture));
public static string Decrypt(byte[] encryptedMessage, string passPhrase, string salt)
return Decrypt(encryptedMessage, passPhrase, salt, HashAlgorithm, PasswordIterations, InitVector, KeySize);
public static string Decrypt(byte[] encryptedMessage, DateTime registrationDate)
return Decrypt(encryptedMessage, registrationDate.ToString(CultureInfo.InvariantCulture),
registrationDate.ToString(CultureInfo.InvariantCulture));
public static async Task<string> DecryptAsync(byte[] encryptedMessage, string passPhrase, string salt)
return await DecryptAsync(encryptedMessage, passPhrase, salt, HashAlgorithm, PasswordIterations, InitVector, KeySize).ConfigureAwait(false);
public static async Task<string> DecryptAsync(byte[] encryptedMessage, DateTime registrationDate)
return await DecryptAsync(encryptedMessage, registrationDate.ToString(CultureInfo.InvariantCulture),
registrationDate.ToString(CultureInfo.InvariantCulture)).ConfigureAwait(false);
public static async Task<byte[]> EncryptAsync(string message, string passPhrase, string salt, CancellationToken cancellationToken = default)
return await EncryptAsync(message, passPhrase, salt, HashAlgorithm, PasswordIterations, InitVector, KeySize, cancellationToken).ConfigureAwait(false);
public static async Task<byte[]> EncryptAsync(string plainText, DateTime registrationDate, CancellationToken cancellationToken = default)
string registrationDateStr = registrationDate.ToString(CultureInfo.InvariantCulture);
return await EncryptAsync(plainText, registrationDateStr, registrationDateStr, cancellationToken).ConfigureAwait(false);