123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110 |
- using System;
- using System.IO;
- using System.Security.Cryptography;
- using System.Text;
- namespace Vinno.vCloud.Common.License.Licenses
- {
- public static class AesHelper
- {
- /// <summary>
- /// "Vinno.vCloud.CurrentLicense" two MD5
- /// </summary>
- private const string Key = "2A4A2B05EFD02D5BDEBB55711213BBBD";
- /// <summary>
- /// Gets Encrypt text
- /// </summary>
- /// <param name="inputText"></param>
- /// <returns></returns>
- public static string Encrypt(string inputText)
- {
- return AesEncrypt(inputText, Key);
- }
- /// <summary>
- /// Gets Decrypt text
- /// </summary>
- /// <param name="inputText"></param>
- /// <returns></returns>
- internal static string Decrypt(string inputText)
- {
- return AesDecrypt(inputText, Key);
- }
- /// <summary>
- /// AES algorithm Encrypt string
- /// </summary>
- /// <param name="input"></param>
- /// <param name="key">AES Key </param>
- /// <returns></returns>
- private static string AesEncrypt(string input, string key)
- {
- var encryptKey = Encoding.UTF8.GetBytes(key);
- using (var aesAlg = Aes.Create())
- {
- using (var encryptor = aesAlg.CreateEncryptor(encryptKey, aesAlg.IV))
- {
- using (var msEncrypt = new MemoryStream())
- {
- using (var csEncrypt = new CryptoStream(msEncrypt, encryptor,
- CryptoStreamMode.Write))
- using (var swEncrypt = new StreamWriter(csEncrypt))
- {
- swEncrypt.Write(input);
- }
- var iv = aesAlg.IV;
- var decryptedContent = msEncrypt.ToArray();
- var result = new byte[iv.Length + decryptedContent.Length];
- Buffer.BlockCopy(iv, 0, result, 0, iv.Length);
- Buffer.BlockCopy(decryptedContent, 0, result,
- iv.Length, decryptedContent.Length);
- return Convert.ToBase64String(result);
- }
- }
- }
- }
- /// <summary>
- /// AES algorithm Decrypt string
- /// </summary>
- /// <param name="cipherText"></param>
- /// <param name="keyString">AES Key</param>
- /// <returns></returns>
- private static string AesDecrypt(string cipherText, string keyString)
- {
- var fullCipher = Convert.FromBase64String(cipherText);
- byte[] iv = new byte[16];
- var cipher = new byte[fullCipher.Length - iv.Length];
- Buffer.BlockCopy(fullCipher, 0, iv, 0, iv.Length);
- Buffer.BlockCopy(fullCipher, iv.Length, cipher, 0, fullCipher.Length - iv.Length);
- var key = Encoding.UTF8.GetBytes(keyString);
- using (var aesAlg = Aes.Create())
- {
- using (var decryptor = aesAlg.CreateDecryptor(key, iv))
- {
- string result;
- using (var msDecrypt = new MemoryStream(cipher))
- {
- using (var csDecrypt = new CryptoStream(msDecrypt, decryptor, CryptoStreamMode.Read))
- {
- using (var srDecrypt = new StreamReader(csDecrypt))
- {
- result = srDecrypt.ReadToEnd();
- }
- }
- }
- return result;
- }
- }
- }
- }
- }
|