AesHelper.cs 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. using System;
  2. using System.IO;
  3. using System.Security.Cryptography;
  4. using System.Text;
  5. namespace Vinno.vCloud.Common.License.Licenses
  6. {
  7. public static class AesHelper
  8. {
  9. /// <summary>
  10. /// "Vinno.vCloud.CurrentLicense" two MD5
  11. /// </summary>
  12. private const string Key = "2A4A2B05EFD02D5BDEBB55711213BBBD";
  13. /// <summary>
  14. /// Gets Encrypt text
  15. /// </summary>
  16. /// <param name="inputText"></param>
  17. /// <returns></returns>
  18. public static string Encrypt(string inputText)
  19. {
  20. return AesEncrypt(inputText, Key);
  21. }
  22. /// <summary>
  23. /// Gets Decrypt text
  24. /// </summary>
  25. /// <param name="inputText"></param>
  26. /// <returns></returns>
  27. internal static string Decrypt(string inputText)
  28. {
  29. return AesDecrypt(inputText, Key);
  30. }
  31. /// <summary>
  32. /// AES algorithm Encrypt string
  33. /// </summary>
  34. /// <param name="input"></param>
  35. /// <param name="key">AES Key </param>
  36. /// <returns></returns>
  37. private static string AesEncrypt(string input, string key)
  38. {
  39. var encryptKey = Encoding.UTF8.GetBytes(key);
  40. using (var aesAlg = Aes.Create())
  41. {
  42. using (var encryptor = aesAlg.CreateEncryptor(encryptKey, aesAlg.IV))
  43. {
  44. using (var msEncrypt = new MemoryStream())
  45. {
  46. using (var csEncrypt = new CryptoStream(msEncrypt, encryptor,
  47. CryptoStreamMode.Write))
  48. using (var swEncrypt = new StreamWriter(csEncrypt))
  49. {
  50. swEncrypt.Write(input);
  51. }
  52. var iv = aesAlg.IV;
  53. var decryptedContent = msEncrypt.ToArray();
  54. var result = new byte[iv.Length + decryptedContent.Length];
  55. Buffer.BlockCopy(iv, 0, result, 0, iv.Length);
  56. Buffer.BlockCopy(decryptedContent, 0, result,
  57. iv.Length, decryptedContent.Length);
  58. return Convert.ToBase64String(result);
  59. }
  60. }
  61. }
  62. }
  63. /// <summary>
  64. /// AES algorithm Decrypt string
  65. /// </summary>
  66. /// <param name="cipherText"></param>
  67. /// <param name="keyString">AES Key</param>
  68. /// <returns></returns>
  69. private static string AesDecrypt(string cipherText, string keyString)
  70. {
  71. var fullCipher = Convert.FromBase64String(cipherText);
  72. byte[] iv = new byte[16];
  73. var cipher = new byte[fullCipher.Length - iv.Length];
  74. Buffer.BlockCopy(fullCipher, 0, iv, 0, iv.Length);
  75. Buffer.BlockCopy(fullCipher, iv.Length, cipher, 0, fullCipher.Length - iv.Length);
  76. var key = Encoding.UTF8.GetBytes(keyString);
  77. using (var aesAlg = Aes.Create())
  78. {
  79. using (var decryptor = aesAlg.CreateDecryptor(key, iv))
  80. {
  81. string result;
  82. using (var msDecrypt = new MemoryStream(cipher))
  83. {
  84. using (var csDecrypt = new CryptoStream(msDecrypt, decryptor, CryptoStreamMode.Read))
  85. {
  86. using (var srDecrypt = new StreamReader(csDecrypt))
  87. {
  88. result = srDecrypt.ReadToEnd();
  89. }
  90. }
  91. }
  92. return result;
  93. }
  94. }
  95. }
  96. }
  97. }