From 3028cf5db55b16241ef347329599c4f7b1975c2f Mon Sep 17 00:00:00 2001 From: ruri Date: Tue, 7 Apr 2020 03:24:00 +0200 Subject: [PATCH] Remove automatic SHA256 of key and iv in AES functions --- RuriLib/Functions/Crypto/Crypto.cs | 27 +++++++++++++-------------- 1 file changed, 13 insertions(+), 14 deletions(-) diff --git a/RuriLib/Functions/Crypto/Crypto.cs b/RuriLib/Functions/Crypto/Crypto.cs index 932ad1b..5d79d03 100644 --- a/RuriLib/Functions/Crypto/Crypto.cs +++ b/RuriLib/Functions/Crypto/Crypto.cs @@ -416,7 +416,7 @@ namespace RuriLib.Functions.Crypto public static string AESEncrypt(string data, string key, string iv = "", CipherMode mode = CipherMode.CBC, PaddingMode padding = PaddingMode.None) { string encData = null; - byte[][] keys = GetHashKeys(key, iv); + byte[][] keys = ConvertKeys(key, iv); try { @@ -440,7 +440,7 @@ namespace RuriLib.Functions.Crypto public static string AESDecrypt(string data, string key, string iv = "", CipherMode mode = CipherMode.CBC, PaddingMode padding = PaddingMode.None) { string decData = null; - byte[][] keys = GetHashKeys(key, iv); + byte[][] keys = ConvertKeys(key, iv); try { @@ -452,22 +452,21 @@ namespace RuriLib.Functions.Crypto return decData; } - private static byte[][] GetHashKeys(string key, string iv) + private static byte[][] ConvertKeys(string key, string iv) { byte[][] result = new byte[2][]; - SHA256 sha2 = new SHA256CryptoServiceProvider(); + result[0] = Convert.FromBase64String(key); - byte[] rawKey = Convert.FromBase64String(key); - byte[] rawIV = iv != string.Empty ? Convert.FromBase64String(iv) : Convert.FromBase64String(key); - - byte[] hashKey = sha2.ComputeHash(rawKey); - byte[] hashIV = sha2.ComputeHash(rawIV); - - Array.Resize(ref hashIV, 16); - - result[0] = hashKey; - result[1] = hashIV; + if (string.IsNullOrEmpty(iv)) + { + result[1] = Convert.FromBase64String(key); + Array.Resize(ref result[1], 16); + } + else + { + result[1] = Convert.FromBase64String(iv); + } return result; }