fork(3) 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. MD5CryptoServiceProvider HashProvider = new MD5CryptoServiceProvider();
  21. byte[] key = HashProvider.ComputeHash(UTF8.GetBytes(Passphrase));
  22.  
  23. // Step 2. Create a new RijndaelManaged object
  24. RijndaelManaged crypt = new RijndaelManaged();
  25.  
  26. // Step 3. Setup the encoder
  27. crypt.Key = key;
  28. crypt.Mode = CipherMode.ECB;
  29. crypt.Padding = PaddingMode.PKCS7;
  30.  
  31. // Step 4. Convert the input string to a byte[]
  32. byte[] DataToEncrypt = UTF8.GetBytes(Message);
  33.  
  34. //Console.WriteLine( Convert.ToBase64String(DataToEncrypt));
  35.  
  36. ICryptoTransform cTransform = crypt.CreateEncryptor();
  37.  
  38. // Step 5. Attempt to encrypt the string
  39. try
  40. {
  41. ICryptoTransform Encryptor = crypt.CreateEncryptor();
  42. Results = Encryptor.TransformFinalBlock(DataToEncrypt, 0, DataToEncrypt.Length);
  43. }
  44. finally
  45. {
  46. // Clear the crypt and Hashprovider services of any sensitive information
  47. crypt.Clear();
  48. HashProvider.Clear();
  49. }
  50.  
  51. // Step 6. Return the encrypted string as a base64 encoded string
  52. return Convert.ToBase64String(Results);
  53. }
  54. }
Success #stdin #stdout 0.04s 37128KB
stdin
Standard input is empty
stdout
XDngx/P9tFHrd4DP3D1I/g==