Created
December 7, 2020 06:15
-
-
Save dev-sankhadip/794d77bfb90bcf483df8302af626fd51 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
// DES encryption | |
public string EncryptData(string strData, string strEncDcKey) { | |
byte[] key = {}; //Encryption Key | |
byte[] IV = { | |
10, | |
20, | |
30, | |
40, | |
50, | |
60, | |
70, | |
80 | |
}; | |
byte[] inputByteArray; | |
try { | |
key = Encoding.UTF8.GetBytes(strEncDcKey); | |
// DESCryptoServiceProvider is a cryptography class defind in c#. | |
DESCryptoServiceProvider ObjDES = new DESCryptoServiceProvider(); | |
inputByteArray = Encoding.UTF8.GetBytes(strData); | |
MemoryStream Objmst = new MemoryStream(); | |
CryptoStream Objcs = new CryptoStream(Objmst, ObjDES.CreateEncryptor(key, IV), CryptoStreamMode.Write); | |
Objcs.Write(inputByteArray, 0, inputByteArray.Length); | |
Objcs.FlushFinalBlock(); | |
return Convert.ToBase64String(Objmst.ToArray()); //encrypted string | |
} | |
catch(System.Exception ex) { | |
throw ex; | |
} | |
} | |
// DES decryption | |
public string DecryptData(string strData, string strEndDcKey) { | |
byte[] key = {}; // Key | |
byte[] IV = { | |
10, | |
20, | |
30, | |
40, | |
50, | |
60, | |
70, | |
80 | |
}; | |
byte[] inputByteArray = new byte[strData.Length]; | |
try { | |
key = Encoding.UTF8.GetBytes(strEndDcKey); | |
DESCryptoServiceProvider ObjDES = new DESCryptoServiceProvider(); | |
inputByteArray = Convert.FromBase64String(strData); | |
MemoryStream Objmst = new MemoryStream(); | |
CryptoStream Objcs = new CryptoStream(Objmst, ObjDES.CreateDecryptor(key, IV), CryptoStreamMode.Write); | |
Objcs.Write(inputByteArray, 0, inputByteArray.Length); | |
Objcs.FlushFinalBlock(); | |
Encoding encoding = Encoding.UTF8; | |
return encoding.GetString(Objmst.ToArray()); | |
} | |
catch(System.Exception ex) { | |
throw ex; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment