Last active
May 15, 2019 06:56
-
-
Save ZhenDeng/7fdae720f0eaafe9346ac51ae02bd496 to your computer and use it in GitHub Desktop.
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
string decryptPassword = MD5DESEncryption.Decrypt(model.Password, true); | |
private static string securityKey = "Dicker183654729Data"; | |
public static string Encrypt(string toEncrypt, bool useHashing) | |
{ | |
string retVal = string.Empty; | |
try | |
{ | |
byte[] keyArray; | |
byte[] toEncryptArray = UTF8Encoding.UTF8.GetBytes(toEncrypt); | |
// Validate inputs | |
//ValidateInput(toEncrypt); | |
//ValidateInput(securityKey); | |
// If hashing use get hashcode regards to your key | |
if (useHashing) | |
{ | |
MD5CryptoServiceProvider hashmd5 = new MD5CryptoServiceProvider(); | |
keyArray = hashmd5.ComputeHash(UTF8Encoding.UTF8.GetBytes(securityKey)); | |
// Always release the resources and flush data | |
// of the Cryptographic service provide. Best Practice | |
hashmd5.Clear(); | |
} | |
else | |
{ | |
keyArray = UTF8Encoding.UTF8.GetBytes(securityKey); | |
} | |
TripleDESCryptoServiceProvider tdes = new TripleDESCryptoServiceProvider(); | |
// Set the secret key for the tripleDES algorithm | |
tdes.Key = keyArray; | |
// Mode of operation. there are other 4 modes. | |
// We choose ECB (Electronic code Book) | |
tdes.Mode = CipherMode.ECB; | |
// Padding mode (if any extra byte added) | |
tdes.Padding = PaddingMode.PKCS7; | |
ICryptoTransform cTransform = tdes.CreateEncryptor(); | |
// Transform the specified region of bytes array to resultArray | |
byte[] resultArray = cTransform.TransformFinalBlock(toEncryptArray, 0, toEncryptArray.Length); | |
// Release resources held by TripleDes Encryptor | |
tdes.Clear(); | |
// Return the encrypted data into unreadable string format | |
retVal = Convert.ToBase64String(resultArray, 0, resultArray.Length); | |
} | |
catch (Exception ex) | |
{ | |
} | |
return retVal; | |
} | |
public static string Decrypt(string cipherString, bool useHashing) | |
{ | |
string retVal = string.Empty; | |
int len = cipherString.Length % 4; | |
if (len > 0) | |
cipherString.PadRight(cipherString.Length + (4 - len), '='); | |
cipherString = cipherString.Replace(" ", "+"); | |
try | |
{ | |
byte[] keyArray; | |
byte[] toEncryptArray = Convert.FromBase64String(cipherString); | |
// Validate inputs | |
//ValidateInput(cipherString); | |
//ValidateInput(securityKey); | |
if (useHashing) | |
{ | |
// If hashing was used get the hash code with regards to your key | |
MD5CryptoServiceProvider hashmd5 = new MD5CryptoServiceProvider(); | |
keyArray = hashmd5.ComputeHash(UTF8Encoding.UTF8.GetBytes(securityKey)); | |
// Release any resource held by the MD5CryptoServiceProvider | |
hashmd5.Clear(); | |
} | |
else | |
{ | |
// If hashing was not implemented get the byte code of the key | |
keyArray = UTF8Encoding.UTF8.GetBytes(securityKey); | |
} | |
TripleDESCryptoServiceProvider tdes = new TripleDESCryptoServiceProvider(); | |
// Set the secret key for the tripleDES algorithm | |
tdes.Key = keyArray; | |
// Mode of operation. there are other 4 modes. | |
// We choose ECB(Electronic code Book) | |
tdes.Mode = CipherMode.ECB; | |
tdes.Padding = PaddingMode.PKCS7; | |
ICryptoTransform cTransform = tdes.CreateDecryptor(); | |
byte[] resultArray = cTransform.TransformFinalBlock(toEncryptArray, 0, toEncryptArray.Length); | |
// Release resources held by TripleDes Encryptor | |
tdes.Clear(); | |
// Return the Clear decrypted TEXT | |
retVal = UTF8Encoding.UTF8.GetString(resultArray); | |
} | |
catch (Exception ex) | |
{ | |
//throw new EncryptionException(EncryptionException.Code.DecryptionFailure, ex, MethodBase.GetCurrentMethod()); | |
} | |
return retVal; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment