From 01d35456bde3be7ecfd8b1ea7d0916fe1b764312 Mon Sep 17 00:00:00 2001 From: 2dust <31833384+2dust@users.noreply.github.com> Date: Tue, 26 Nov 2024 20:07:41 +0800 Subject: [PATCH] Update AesUtils.cs --- v2rayN/ServiceLib/Common/AesUtils.cs | 117 +++++++++++---------------- 1 file changed, 45 insertions(+), 72 deletions(-) diff --git a/v2rayN/ServiceLib/Common/AesUtils.cs b/v2rayN/ServiceLib/Common/AesUtils.cs index f6ccf761..dcd4ac2a 100644 --- a/v2rayN/ServiceLib/Common/AesUtils.cs +++ b/v2rayN/ServiceLib/Common/AesUtils.cs @@ -1,5 +1,3 @@ -using System; -using System.IO; using System.Security.Cryptography; using System.Text; @@ -10,10 +8,8 @@ namespace ServiceLib.Common private const int KeySize = 256; // AES-256 private const int IvSize = 16; // AES block size private const int Iterations = 10000; - private static readonly byte[] Salt = Encoding.ASCII.GetBytes("saltysalt".PadRight(16, ' ')); // google浏览器默认盐值 - - private static readonly string DefaultPassword =Utils.GetHomePath() + "AesUtils"; + private static readonly string DefaultPassword = Utils.GetMd5(Utils.GetHomePath() + "AesUtils"); /// /// Encrypt @@ -21,34 +17,30 @@ namespace ServiceLib.Common /// Plain text /// Password for key derivation or direct key in ASCII bytes /// Base64 encoded cipher text with IV - public static string Encrypt(string text, string password = null) + public static string Encrypt(string text, string? password = null) { if (string.IsNullOrEmpty(text)) return string.Empty; - byte[] plaintext = Encoding.UTF8.GetBytes(text); - byte[] key = GetKey(password); - byte[] iv = GenerateIv(); + var plaintext = Encoding.UTF8.GetBytes(text); + var key = GetKey(password); + var iv = GenerateIv(); - using (Aes aes = Aes.Create()) + using var aes = Aes.Create(); + aes.Key = key; + aes.IV = iv; + + using var ms = new MemoryStream(); + ms.Write(iv, 0, iv.Length); + + using (var cs = new CryptoStream(ms, aes.CreateEncryptor(), CryptoStreamMode.Write)) { - aes.Key = key; - aes.IV = iv; - - using (MemoryStream ms = new MemoryStream()) - { - ms.Write(iv, 0, iv.Length); - - using (CryptoStream cs = new CryptoStream(ms, aes.CreateEncryptor(), CryptoStreamMode.Write)) - { - cs.Write(plaintext, 0, plaintext.Length); - cs.FlushFinalBlock(); - } - - byte[] cipherTextWithIv = ms.ToArray(); - return Convert.ToBase64String(cipherTextWithIv); - } + cs.Write(plaintext, 0, plaintext.Length); + cs.FlushFinalBlock(); } + + var cipherTextWithIv = ms.ToArray(); + return Convert.ToBase64String(cipherTextWithIv); } /// @@ -57,72 +49,53 @@ namespace ServiceLib.Common /// Base64 encoded cipher text with IV /// Password for key derivation or direct key in ASCII bytes /// Plain text - public static string Decrypt(string cipherTextWithIv, string password = null) + public static string Decrypt(string cipherTextWithIv, string? password = null) { if (string.IsNullOrEmpty(cipherTextWithIv)) return string.Empty; - byte[] cipherTextWithIvBytes = Convert.FromBase64String(cipherTextWithIv); - byte[] key = GetKey(password); + var cipherTextWithIvBytes = Convert.FromBase64String(cipherTextWithIv); + var key = GetKey(password); - byte[] iv = new byte[IvSize]; + var iv = new byte[IvSize]; Buffer.BlockCopy(cipherTextWithIvBytes, 0, iv, 0, IvSize); - byte[] cipherText = new byte[cipherTextWithIvBytes.Length - IvSize]; + var cipherText = new byte[cipherTextWithIvBytes.Length - IvSize]; Buffer.BlockCopy(cipherTextWithIvBytes, IvSize, cipherText, 0, cipherText.Length); - using (Aes aes = Aes.Create()) + using var aes = Aes.Create(); + aes.Key = key; + aes.IV = iv; + + using var ms = new MemoryStream(); + using (var cs = new CryptoStream(ms, aes.CreateDecryptor(), CryptoStreamMode.Write)) { - aes.Key = key; - aes.IV = iv; - - using (MemoryStream ms = new MemoryStream()) - { - using (CryptoStream cs = new CryptoStream(ms, aes.CreateDecryptor(), CryptoStreamMode.Write)) - { - cs.Write(cipherText, 0, cipherText.Length); - cs.FlushFinalBlock(); - } - - byte[] plainText = ms.ToArray(); - return Encoding.UTF8.GetString(plainText); - } + cs.Write(cipherText, 0, cipherText.Length); + cs.FlushFinalBlock(); } + + var plainText = ms.ToArray(); + return Encoding.UTF8.GetString(plainText); } - private static byte[] GetKey(string password) + private static byte[] GetKey(string? password) { - if (string.IsNullOrEmpty(password)) + if (password.IsNullOrEmpty()) { - return GetDefaultKey(); + password = DefaultPassword; } - else - { - byte[] key = Encoding.ASCII.GetBytes(password); - if (key.Length != KeySize / 8) - { - throw new ArgumentException($"Password bytes length must be {KeySize / 8} bytes."); - } - return key; - } - } - private static byte[] GetDefaultKey() - { - using (Rfc2898DeriveBytes pbkdf2 = new Rfc2898DeriveBytes(DefaultPassword, Salt, Iterations, HashAlgorithmName.SHA256)) - { - return pbkdf2.GetBytes(KeySize / 8); - } + using var pbkdf2 = new Rfc2898DeriveBytes(password, Salt, Iterations, HashAlgorithmName.SHA256); + return pbkdf2.GetBytes(KeySize / 8); } private static byte[] GenerateIv() { - using (RNGCryptoServiceProvider rng = new RNGCryptoServiceProvider()) - { - byte[] iv = new byte[IvSize]; - rng.GetBytes(iv); - return iv; - } + var randomNumber = new byte[IvSize]; + + using var rng = RandomNumberGenerator.Create(); + rng.GetBytes(randomNumber); + return randomNumber; } } -} +} \ No newline at end of file