Created
June 1, 2017 22:11
-
-
Save Martyr2/9f9e9dbfbffc76b45db68099c225a410 to your computer and use it in GitHub Desktop.
Simple encryption and decryption functions that make the process easy to use.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/// <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