Posted By


falconzy on 11/11/10

Tagged


Statistics


Viewed 124 times
Favorited by 0 user(s)

System.Security.Cryptography


/ Published in: C#
Save to your folder(s)



Copy this code and paste it in your HTML
  1. private TripleDESCryptoServiceProvider cryptdes = new TripleDESCryptoServiceProvider();
  2. private UTF8Encoding utf8 = new UTF8Encoding();
  3.  
  4. public string EncrypToText(string textToEncrypt)
  5. {
  6. try
  7. {
  8. return Convert.ToBase64String(this.EncryptToBinary(textToEncrypt));
  9. }
  10. catch (Exception ex)
  11. {
  12. throw new EncryptionException(ex.Message);
  13. }
  14. }
  15. public byte[] EncryptToBinary(string textToEncrypt)
  16. {
  17. try
  18. {
  19. byte[] input = this.utf8.GetBytes(textToEncrypt);
  20. return this.Transform(input, this.cryptdes.CreateEncryptor(this.key, this.iv));
  21. }
  22. catch (Exception ex)
  23. {
  24. throw new EncryptionException(ex.Message);
  25. }
  26. }
  27.  
  28. private byte[] Transform(byte[] input, ICryptoTransform CryptoTransform)
  29. {
  30. // create the necessary streams
  31. MemoryStream memStream = new MemoryStream();
  32. CryptoStream cryptStream = new CryptoStream(memStream, CryptoTransform, CryptoStreamMode.Write);
  33. // transform the bytes as requested
  34. cryptStream.Write(input, 0, input.Length);
  35. cryptStream.FlushFinalBlock();
  36. // Read the memory stream and convert it back into byte array
  37. memStream.Position = 0;
  38. byte[] result = memStream.ToArray();
  39. // Clean up
  40. memStream.Close();
  41. cryptStream.Close();
  42. return result;
  43. }

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.