/SecurityMethods/Security.cs

http://ssta.codeplex.com · C# · 112 lines · 76 code · 7 blank · 29 comment · 0 complexity · e8995c0b169fa1472e4970adfa772d56 MD5 · raw file

  1. using System;
  2. using System.IO;
  3. using System.Security;
  4. using System.Security.Cryptography;
  5. using System.Runtime.InteropServices;
  6. using System.Text;
  7. namespace Lewis.SST.SecurityMethods
  8. {
  9. /// <summary>
  10. /// Security class handles encryption/decryption for connection strings
  11. /// </summary>
  12. public class Security
  13. {
  14. /// <summary>
  15. /// Call this function to remove the key from memory after use for security.
  16. /// </summary>
  17. /// <param name="Destination"></param>
  18. /// <param name="Length"></param>
  19. /// <returns></returns>
  20. [System.Runtime.InteropServices.DllImport("KERNEL32.DLL", EntryPoint="RtlZeroMemory")]
  21. public static extern bool ZeroMemory(ref string Destination, int Length);
  22. /// <summary>
  23. /// Function to Generate 64 bit Key.
  24. /// </summary>
  25. /// <returns></returns>
  26. public static string GenerateKey()
  27. {
  28. // Create an instance of Symetric Algorithm. Key and IV is generated automatically.
  29. DESCryptoServiceProvider desCrypto = (DESCryptoServiceProvider)DESCryptoServiceProvider.Create();
  30. // Use the Automatically generated key for Encryption.
  31. return ASCIIEncoding.ASCII.GetString(desCrypto.Key);
  32. }
  33. private Security()
  34. {
  35. // default ctor
  36. }
  37. /// <summary>
  38. /// static method to encrypt passed in string using passed in public key
  39. /// </summary>
  40. /// <param name="plainString"></param>
  41. /// <param name="sKey"></param>
  42. /// <returns></returns>
  43. public static string EncryptString(string plainString, string sKey)
  44. {
  45. DESCryptoServiceProvider DES = new DESCryptoServiceProvider();
  46. DES.Key = ASCIIEncoding.ASCII.GetBytes(sKey);
  47. DES.IV = ASCIIEncoding.ASCII.GetBytes(sKey);
  48. String outstring = null;
  49. using (MemoryStream sEncrypted = new MemoryStream())
  50. {
  51. byte[] output = null;
  52. ICryptoTransform desencrypt = DES.CreateEncryptor();
  53. using (CryptoStream cryptostream = new CryptoStream(sEncrypted, desencrypt, CryptoStreamMode.Write))
  54. {
  55. UTF8Encoding e = new UTF8Encoding();
  56. byte[] bytearrayinput = e.GetBytes(plainString.ToCharArray());
  57. cryptostream.Write(bytearrayinput, 0, bytearrayinput.Length);
  58. cryptostream.FlushFinalBlock();
  59. cryptostream.Close();
  60. }
  61. output = sEncrypted.ToArray();
  62. sEncrypted.Close();
  63. outstring = Convert.ToBase64String(output);
  64. }
  65. return outstring;
  66. }
  67. /// <summary>
  68. /// static method to decrypt passed in string using passed in public key
  69. /// </summary>
  70. /// <param name="encryptedString"></param>
  71. /// <param name="sKey"></param>
  72. /// <returns></returns>
  73. public static string DecryptString(string encryptedString, string sKey)
  74. {
  75. DESCryptoServiceProvider DES = new DESCryptoServiceProvider();
  76. DES.Key = ASCIIEncoding.ASCII.GetBytes(sKey);
  77. DES.IV = ASCIIEncoding.ASCII.GetBytes(sKey);
  78. string outstring = null;
  79. using (MemoryStream sDecrypted = new MemoryStream())
  80. {
  81. try
  82. {
  83. byte[] output = null;
  84. UTF8Encoding e = new UTF8Encoding();
  85. ICryptoTransform desdecrypt = DES.CreateDecryptor();
  86. using (CryptoStream cryptostream = new CryptoStream(sDecrypted, desdecrypt, CryptoStreamMode.Write))
  87. {
  88. byte[] bytearrayinput = Convert.FromBase64String(encryptedString);
  89. cryptostream.Write(bytearrayinput, 0, bytearrayinput.Length);
  90. cryptostream.FlushFinalBlock();
  91. cryptostream.Close();
  92. }
  93. output = sDecrypted.ToArray();
  94. sDecrypted.Close();
  95. outstring = e.GetString(output);
  96. }
  97. catch //(CryptographicException cex)
  98. {
  99. // nop
  100. }
  101. }
  102. return outstring;
  103. }
  104. }
  105. }