/Queuing System/ActiveQLibrary/Encryption.cs

# · C# · 100 lines · 60 code · 11 blank · 29 comment · 1 complexity · e0690da3c76b847fef4d721568399fc1 MD5 · raw file

  1. // Copyright 2001-2010 - Active Up SPRLU (http://www.agilecomponents.com)
  2. //
  3. // This file is part of MailSystem.NET.
  4. // MailSystem.NET is free software; you can redistribute it and/or modify
  5. // it under the terms of the GNU Lesser General Public License as published by
  6. // the Free Software Foundation; either version 2 of the License, or
  7. // (at your option) any later version.
  8. //
  9. // MailSystem.NET is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. // GNU Lesser General Public License for more details.
  13. // You should have received a copy of the GNU Lesser General Public License
  14. // along with SharpMap; if not, write to the Free Software
  15. // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  16. using System;
  17. using System.IO;
  18. using System.Security;
  19. using System.Security.Cryptography;
  20. using System.Text;
  21. namespace ActiveQLibrary
  22. {
  23. /// <summary>
  24. /// Description résumée de Encryption.
  25. /// </summary>
  26. public class Encryption
  27. {
  28. // NB that the keys sit in the code, so this is NOT bullet-proof!!!
  29. public static string Encrypt(string pToEncrypt)
  30. {
  31. return Encrypt(pToEncrypt, "4Jkw9N3f");
  32. }
  33. public static string Encrypt(string pToEncrypt, string sKey)
  34. {
  35. DESCryptoServiceProvider des = new DESCryptoServiceProvider();
  36. //Put the string into a byte array
  37. byte[] inputByteArray = Encoding.UTF8.GetBytes(pToEncrypt);
  38. //Create the crypto objects, with the key, as passed in
  39. des.Key = ASCIIEncoding.ASCII.GetBytes(sKey);
  40. des.IV = ASCIIEncoding.ASCII.GetBytes(sKey);
  41. MemoryStream ms = new MemoryStream();
  42. CryptoStream cs = new CryptoStream(ms, des.CreateEncryptor(),
  43. CryptoStreamMode.Write);
  44. //Write the byte array into the crypto stream
  45. //(It will end up in the memory stream)
  46. cs.Write(inputByteArray, 0, inputByteArray.Length);
  47. cs.FlushFinalBlock();
  48. //Get the data back from the memory stream, and into a string
  49. StringBuilder ret = new StringBuilder();
  50. foreach(byte b in ms.ToArray())
  51. {
  52. //Format as hex
  53. ret.AppendFormat("{0:X2}", b);
  54. }
  55. return ret.ToString();
  56. }
  57. public static string Decrypt(string pToDecrypt)
  58. {
  59. return Decrypt(pToDecrypt, "4Jkw9N3f");
  60. }
  61. public static string Decrypt(string pToDecrypt, string sKey)
  62. {
  63. DESCryptoServiceProvider des = new DESCryptoServiceProvider();
  64. //Put the input string into the byte array
  65. byte[] inputByteArray = new byte[pToDecrypt.Length / 2];
  66. for(int x = 0; x < pToDecrypt.Length / 2; x++)
  67. {
  68. int i = (Convert.ToInt32(pToDecrypt.Substring(x * 2, 2), 16));
  69. inputByteArray[x] = (byte)i;
  70. }
  71. //Create the crypto objects
  72. des.Key = ASCIIEncoding.ASCII.GetBytes(sKey);
  73. des.IV = ASCIIEncoding.ASCII.GetBytes(sKey);
  74. MemoryStream ms = new MemoryStream();
  75. CryptoStream cs = new CryptoStream(ms, des.CreateDecryptor(),
  76. CryptoStreamMode.Write);
  77. //Flush the data through the crypto stream into the memory stream
  78. cs.Write(inputByteArray, 0, inputByteArray.Length);
  79. cs.FlushFinalBlock();
  80. //Get the decrypted data back from the memory stream
  81. StringBuilder ret = new StringBuilder();
  82. foreach(byte b in ms.ToArray())
  83. {
  84. ret.Append((char)b);
  85. }
  86. return ret.ToString();
  87. }
  88. }
  89. }