Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Simple encryption and decryption functions that make the process easy to use.
/// <summary>
/// Decrypts a string that was encrypted using the encrypt method in this library.
/// </summary>
/// <param name="input">Encrypted string in base 64</param>
/// <param name="key">Salt key used in encryption</param>
/// <returns>Decrypted string data</returns>
public static string Decrypt(string input, string key)
{
byte[] inputArray = Convert.FromBase64String(input);
TripleDESCryptoServiceProvider tripleDES = new TripleDESCryptoServiceProvider() {
Key = System.Text.UTF8Encoding.UTF8.GetBytes(key.PadRight(24, '-')),
Mode = CipherMode.ECB,
Padding = PaddingMode.PKCS7
};
ICryptoTransform cTransform = tripleDES.CreateDecryptor();
byte[] resultArray = cTransform.TransformFinalBlock(inputArray, 0, inputArray.Length);
tripleDES.Clear();
return System.Text.UTF8Encoding.UTF8.GetString(resultArray);
}
/// <summary>
/// Encrypts a string for password storage
/// </summary>
/// <param name="input">String to encrypt</param>
/// <param name="key">Salt key</param>
/// <returns>String of encrypted data in base 64</returns>
public static string Encrypt(string input, string key)
{
byte[] inputArray = System.Text.UTF8Encoding.UTF8.GetBytes(input);
TripleDESCryptoServiceProvider tripleDES = new TripleDESCryptoServiceProvider() {
Key = System.Text.UTF8Encoding.UTF8.GetBytes(key.PadRight(24, '-')),
Mode = CipherMode.ECB,
Padding = PaddingMode.PKCS7
};
ICryptoTransform cTransform = tripleDES.CreateEncryptor();
byte[] resultArray = cTransform.TransformFinalBlock(inputArray, 0, inputArray.Length);
tripleDES.Clear();
return Convert.ToBase64String(resultArray, 0, resultArray.Length);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.