|
|
@@ -38,68 +38,95 @@ namespace Winsoft.GOV.XF.WebApi.WXCore.Helpers
|
|
|
public static string AESEncrypt(this string input, string key)
|
|
|
{
|
|
|
|
|
|
- var encryptKey = Encoding.UTF8.GetBytes(key);
|
|
|
+ byte[] bytesToBeEncrypted = Encoding.UTF8.GetBytes(input);
|
|
|
+ byte[] passwordBytes = 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))
|
|
|
+ passwordBytes = SHA256.Create().ComputeHash(passwordBytes);
|
|
|
+
|
|
|
+ byte[] bytesEncrypted = AESEncryptBytes(bytesToBeEncrypted, passwordBytes);
|
|
|
+
|
|
|
+ string result = Convert.ToBase64String(bytesEncrypted);
|
|
|
+
|
|
|
+ return result;
|
|
|
+ }
|
|
|
|
|
|
- using (var swEncrypt = new StreamWriter(csEncrypt))
|
|
|
- {
|
|
|
- swEncrypt.Write(input);
|
|
|
- }
|
|
|
+ private static byte[] AESEncryptBytes(byte[] bytesToBeEncrypted, byte[] passwordBytes)
|
|
|
+ {
|
|
|
+ byte[] encryptedBytes = null;
|
|
|
|
|
|
- var iv = aesAlg.IV;
|
|
|
+ var saltBytes = new byte[9] { 13, 34, 27, 67, 189, 255, 104, 219, 122 };
|
|
|
|
|
|
- var decryptedContent = msEncrypt.ToArray();
|
|
|
+ using (var ms = new MemoryStream())
|
|
|
+ {
|
|
|
+ using (var AES = new RijndaelManaged())
|
|
|
+ {
|
|
|
+ AES.KeySize = 256;
|
|
|
+ AES.BlockSize = 128;
|
|
|
|
|
|
- var result = new byte[iv.Length + decryptedContent.Length];
|
|
|
+ var key = new Rfc2898DeriveBytes(passwordBytes, saltBytes, 1000);
|
|
|
+ AES.Key = key.GetBytes(32);
|
|
|
+ AES.IV = key.GetBytes(16);
|
|
|
|
|
|
- Buffer.BlockCopy(iv, 0, result, 0, iv.Length);
|
|
|
- Buffer.BlockCopy(decryptedContent, 0, result,
|
|
|
- iv.Length, decryptedContent.Length);
|
|
|
+ AES.Mode = CipherMode.CBC;
|
|
|
|
|
|
- return Convert.ToBase64String(result);
|
|
|
+ using (var cs = new CryptoStream(ms, AES.CreateEncryptor(),
|
|
|
+ CryptoStreamMode.Write))
|
|
|
+ {
|
|
|
+ cs.Write(bytesToBeEncrypted, 0, bytesToBeEncrypted.Length);
|
|
|
+ cs.Close();
|
|
|
}
|
|
|
+
|
|
|
+ encryptedBytes = ms.ToArray();
|
|
|
}
|
|
|
}
|
|
|
+
|
|
|
+ return encryptedBytes;
|
|
|
}
|
|
|
public static string AESDecrypt(this string input, string key)
|
|
|
{
|
|
|
- var fullCipher = Convert.FromBase64String(input);
|
|
|
+ byte[] bytesToBeDecrypted = Convert.FromBase64String(input);
|
|
|
+
|
|
|
+ byte[] passwordBytes = Encoding.UTF8.GetBytes(key);
|
|
|
+
|
|
|
+ passwordBytes = SHA256.Create().ComputeHash(passwordBytes);
|
|
|
+
|
|
|
+ byte[] bytesDecrypted = AESDecryptBytes(bytesToBeDecrypted, passwordBytes);
|
|
|
+
|
|
|
+ string result = Encoding.UTF8.GetString(bytesDecrypted);
|
|
|
+
|
|
|
+ return result;
|
|
|
+ }
|
|
|
|
|
|
- var iv = new byte[16];
|
|
|
- var cipher = new byte[16];
|
|
|
+ public static byte[] AESDecryptBytes(byte[] bytesToBeDecrypted, byte[] passwordBytes)
|
|
|
+ {
|
|
|
+ byte[] decryptedBytes = null;
|
|
|
|
|
|
- Buffer.BlockCopy(fullCipher, 0, iv, 0, iv.Length);
|
|
|
- Buffer.BlockCopy(fullCipher, iv.Length, cipher, 0, iv.Length);
|
|
|
- var decryptKey = Encoding.UTF8.GetBytes(key);
|
|
|
+ var saltBytes = new byte[9] { 13, 34, 27, 67, 189, 255, 104, 219, 122 };
|
|
|
|
|
|
- using (var aesAlg = Aes.Create())
|
|
|
+ using (var ms = new MemoryStream())
|
|
|
{
|
|
|
- using (var decryptor = aesAlg.CreateDecryptor(decryptKey, iv))
|
|
|
+ using (var AES = new RijndaelManaged())
|
|
|
{
|
|
|
- string result;
|
|
|
- using (var msDecrypt = new MemoryStream(cipher))
|
|
|
+ AES.KeySize = 256;
|
|
|
+ AES.BlockSize = 128;
|
|
|
+
|
|
|
+ var key = new Rfc2898DeriveBytes(passwordBytes, saltBytes, 1000);
|
|
|
+ AES.Key = key.GetBytes(32);
|
|
|
+ AES.IV = key.GetBytes(16);
|
|
|
+
|
|
|
+ AES.Mode = CipherMode.CBC;
|
|
|
+
|
|
|
+ using (var cs = new CryptoStream(ms, AES.CreateDecryptor(), CryptoStreamMode.Write))
|
|
|
{
|
|
|
- using (var csDecrypt = new CryptoStream(msDecrypt,
|
|
|
- decryptor, CryptoStreamMode.Read))
|
|
|
- {
|
|
|
- using (var srDecrypt = new StreamReader(csDecrypt))
|
|
|
- {
|
|
|
- result = srDecrypt.ReadToEnd();
|
|
|
- }
|
|
|
- }
|
|
|
+ cs.Write(bytesToBeDecrypted, 0, bytesToBeDecrypted.Length);
|
|
|
+ cs.Close();
|
|
|
}
|
|
|
|
|
|
- return result;
|
|
|
+ decryptedBytes = ms.ToArray();
|
|
|
}
|
|
|
}
|
|
|
+
|
|
|
+ return decryptedBytes;
|
|
|
}
|
|
|
|
|
|
/// <summary>
|