fork download
  1. using System;
  2. using System.Security.Cryptography;
  3.  
  4. public class Test
  5. {
  6. public static void Main()
  7. {
  8. Console.WriteLine(EncryptString("test", "123456"));
  9. }
  10.  
  11. public static string EncryptString(string Message, string Passphrase)
  12. {
  13. byte[] Results;
  14. System.Text.UTF8Encoding UTF8 = new System.Text.UTF8Encoding();
  15.  
  16. // Step 1. We hash the passphrase using MD5
  17. // We use the MD5 hash generator as the result is a 128 bit byte array
  18. // which is a valid length for the TripleDES encoder we use below
  19.  
  20. SHA256CryptoServiceProvider HashProvider = new SHA256CryptoServiceProvider();
  21. byte[] temp = HashProvider.ComputeHash(UTF8.GetBytes(Passphrase));
  22. byte[] key = new byte[24];
  23. Array.Copy(temp, key, 24);
  24.  
  25. // Step 2. Create a new RijndaelManaged object
  26. TripleDESCryptoServiceProvider crypt = new TripleDESCryptoServiceProvider();
  27.  
  28. // Step 3. Setup the encoder
  29. crypt.Key = key;
  30. crypt.Mode = CipherMode.ECB;
  31. crypt.Padding = PaddingMode.PKCS7;
  32.  
  33. // Step 4. Convert the input string to a byte[]
  34. byte[] DataToEncrypt = UTF8.GetBytes(Message);
  35.  
  36. //Console.WriteLine( Convert.ToBase64String(DataToEncrypt));
  37.  
  38. ICryptoTransform cTransform = crypt.CreateEncryptor();
  39.  
  40. // Step 5. Attempt to encrypt the string
  41. try
  42. {
  43. ICryptoTransform Encryptor = crypt.CreateEncryptor();
  44. Results = Encryptor.TransformFinalBlock(DataToEncrypt, 0, DataToEncrypt.Length);
  45. }
  46. finally
  47. {
  48. // Clear the crypt and Hashprovider services of any sensitive information
  49. crypt.Clear();
  50. HashProvider.Clear();
  51. }
  52.  
  53. // Step 6. Return the encrypted string as a base64 encoded string
  54. return Convert.ToBase64String(Results);
  55. }
  56. }
Success #stdin #stdout 0.04s 30872KB
stdin
hgjghj gfhj fghj fghj fghj
stdout
ylb5KsQvROs=