AESEncryption.cs 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text;
  4. using System.Security;
  5. using System.Security.Cryptography;
  6. using System.IO;
  7. public static class AESEncryption
  8. {
  9. private static byte[] _salt = Encoding.ASCII.GetBytes("o6806642kbM7c5");
  10. /// <summary>
  11. /// Encrypt the given string using AES. The string can be decrypted using
  12. /// DecryptStringAES(). The sharedSecret parameters must match.
  13. /// </summary>
  14. /// <param name="plainText">The text to encrypt.</param>
  15. /// <param name="sharedSecret">A password used to generate a key for encryption.</param>
  16. public static string Encrypt(string plainText, string sharedSecret)
  17. {
  18. if(string.IsNullOrEmpty(plainText))
  19. throw new ArgumentNullException("plainText");
  20. if(string.IsNullOrEmpty(sharedSecret))
  21. throw new ArgumentNullException("sharedSecret");
  22. string outStr = null; // Encrypted string to return
  23. RijndaelManaged aesAlg = null; // RijndaelManaged object used to encrypt the data.
  24. try
  25. {
  26. // generate the key from the shared secret and the salt
  27. Rfc2898DeriveBytes key = new Rfc2898DeriveBytes(sharedSecret, _salt);
  28. // Create a RijndaelManaged object
  29. // with the specified key and IV.
  30. aesAlg = new RijndaelManaged();
  31. aesAlg.Key = key.GetBytes(aesAlg.KeySize / 8);
  32. aesAlg.IV = key.GetBytes(aesAlg.BlockSize / 8);
  33. // Create a decrytor to perform the stream transform.
  34. ICryptoTransform encryptor = aesAlg.CreateEncryptor(aesAlg.Key, aesAlg.IV);
  35. // Create the streams used for encryption.
  36. using (MemoryStream msEncrypt = new MemoryStream())
  37. {
  38. using (CryptoStream csEncrypt = new CryptoStream(msEncrypt, encryptor, CryptoStreamMode.Write))
  39. {
  40. using (StreamWriter swEncrypt = new StreamWriter(csEncrypt))
  41. {
  42. //Write all data to the stream.
  43. swEncrypt.Write(plainText);
  44. }
  45. }
  46. outStr = Convert.ToBase64String(msEncrypt.ToArray());
  47. }
  48. }
  49. finally
  50. {
  51. // Clear the RijndaelManaged object.
  52. if (aesAlg != null)
  53. aesAlg.Clear();
  54. }
  55. // Return the encrypted bytes from the memory stream.
  56. return outStr;
  57. }
  58. /// <summary>
  59. /// Decrypt the given string. Assumes the string was encrypted using
  60. /// EncryptStringAES(), using an identical sharedSecret.
  61. /// </summary>
  62. /// <param name="cipherText">The text to decrypt.</param>
  63. /// <param name="sharedSecret">A password used to generate a key for decryption.</param>
  64. public static string Decrypt(string cipherText, string sharedSecret)
  65. {
  66. if (string.IsNullOrEmpty(cipherText))
  67. throw new ArgumentNullException("cipherText");
  68. if (string.IsNullOrEmpty(sharedSecret))
  69. throw new ArgumentNullException("sharedSecret");
  70. // Declare the RijndaelManaged object
  71. // used to decrypt the data.
  72. RijndaelManaged aesAlg = null;
  73. // Declare the string used to hold
  74. // the decrypted text.
  75. string plaintext = null;
  76. try
  77. {
  78. // generate the key from the shared secret and the salt
  79. Rfc2898DeriveBytes key = new Rfc2898DeriveBytes(sharedSecret, _salt);
  80. // Create a RijndaelManaged object
  81. // with the specified key and IV.
  82. aesAlg = new RijndaelManaged();
  83. aesAlg.Key = key.GetBytes(aesAlg.KeySize / 8);
  84. aesAlg.IV = key.GetBytes(aesAlg.BlockSize / 8);
  85. // Create a decrytor to perform the stream transform.
  86. ICryptoTransform decryptor = aesAlg.CreateDecryptor(aesAlg.Key, aesAlg.IV);
  87. // Create the streams used for decryption.
  88. byte[] bytes = Convert.FromBase64String(cipherText);
  89. using (MemoryStream msDecrypt = new MemoryStream(bytes))
  90. {
  91. using (CryptoStream csDecrypt = new CryptoStream(msDecrypt, decryptor, CryptoStreamMode.Read))
  92. {
  93. using (StreamReader srDecrypt = new StreamReader(csDecrypt))
  94. // Read the decrypted bytes from the decrypting stream
  95. // and place them in a string.
  96. plaintext = srDecrypt.ReadToEnd();
  97. }
  98. }
  99. }
  100. finally
  101. {
  102. // Clear the RijndaelManaged object.
  103. if (aesAlg != null)
  104. aesAlg.Clear();
  105. }
  106. return plaintext;
  107. }
  108. }