Created
September 19, 2018 21:12
-
-
Save karoltheguy/a6aa05fb907fe8ec4590568257a5a696 to your computer and use it in GitHub Desktop.
TripleDES
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
| class Program | |
| { | |
| static void Main(string[] args) | |
| { | |
| string mainkey = "ghRag3Svho6nMYBbyCnEJc4wVRTkGX82"; | |
| string result = Encrypt("EncryptMe", mainkey); | |
| Console.WriteLine(result); | |
| Console.ReadKey(); | |
| result = Decrypt(result, mainkey); | |
| Console.WriteLine(result); | |
| Console.ReadKey(); | |
| } | |
| static string Encrypt(string data, string key) | |
| { | |
| MemoryStream ms = new MemoryStream(); | |
| byte[] keyb = Convert.FromBase64String(key); | |
| TripleDESCryptoServiceProvider tdes = new TripleDESCryptoServiceProvider(); | |
| CryptoStream encStream = new CryptoStream(ms, tdes.CreateEncryptor(keyb, tdes.IV), CryptoStreamMode.Write); | |
| StreamWriter sw = new StreamWriter(encStream); | |
| sw.Write(data); | |
| sw.Flush(); | |
| encStream.FlushFinalBlock(); | |
| ms.Seek(0, SeekOrigin.Begin); | |
| string encText = Convert.ToBase64String(ms.ToArray()); | |
| string ivs = Convert.ToBase64String(tdes.IV); | |
| string EncryptedData = ivs + ":" + encText; | |
| return EncryptedData; | |
| } | |
| static string Decrypt(string EncryptedData, string Key) | |
| { | |
| string Data = string.Empty; | |
| string[] parts = EncryptedData.Split(':'); | |
| if ((parts.Length != 2)) | |
| { | |
| throw new ApplicationException("Invalid data"); | |
| } | |
| byte[] iv = Convert.FromBase64String(parts[0]); | |
| byte[] datab = Convert.FromBase64String(parts[1]); | |
| byte[] keyb = Convert.FromBase64String(Key); | |
| TripleDESCryptoServiceProvider tdes = new TripleDESCryptoServiceProvider(); | |
| MemoryStream ms = new MemoryStream(datab); | |
| CryptoStream encStream = new CryptoStream(ms, tdes.CreateDecryptor(keyb, iv), CryptoStreamMode.Read); | |
| using (StreamReader sr = new StreamReader(encStream)) | |
| { | |
| Data = sr.ReadToEnd(); | |
| } | |
| return Data; | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment