From cb28c31519b7881415a5735b72bb0c63b253fc01 Mon Sep 17 00:00:00 2001 From: 2dust <31833384+2dust@users.noreply.github.com> Date: Tue, 1 Jul 2025 19:19:15 +0800 Subject: [PATCH] Remove unused --- v2rayN/ServiceLib/Common/AesUtils.cs | 100 --------------------------- v2rayN/ServiceLib/Common/DesUtils.cs | 74 -------------------- 2 files changed, 174 deletions(-) delete mode 100644 v2rayN/ServiceLib/Common/AesUtils.cs delete mode 100644 v2rayN/ServiceLib/Common/DesUtils.cs diff --git a/v2rayN/ServiceLib/Common/AesUtils.cs b/v2rayN/ServiceLib/Common/AesUtils.cs deleted file mode 100644 index 05f1cb38..00000000 --- a/v2rayN/ServiceLib/Common/AesUtils.cs +++ /dev/null @@ -1,100 +0,0 @@ -using System.Security.Cryptography; -using System.Text; - -namespace ServiceLib.Common; - -public class AesUtils -{ - 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, ' ')); - private static readonly string DefaultPassword = Utils.GetMd5(Utils.GetHomePath() + "AesUtils"); - - /// - /// Encrypt - /// - /// 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) - { - if (string.IsNullOrEmpty(text)) - return string.Empty; - - var plaintext = Encoding.UTF8.GetBytes(text); - var key = GetKey(password); - var iv = GenerateIv(); - - 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)) - { - cs.Write(plaintext, 0, plaintext.Length); - cs.FlushFinalBlock(); - } - - var cipherTextWithIv = ms.ToArray(); - return Convert.ToBase64String(cipherTextWithIv); - } - - /// - /// Decrypt - /// - /// 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) - { - if (string.IsNullOrEmpty(cipherTextWithIv)) - return string.Empty; - - var cipherTextWithIvBytes = Convert.FromBase64String(cipherTextWithIv); - var key = GetKey(password); - - var iv = new byte[IvSize]; - Buffer.BlockCopy(cipherTextWithIvBytes, 0, iv, 0, IvSize); - - var cipherText = new byte[cipherTextWithIvBytes.Length - IvSize]; - Buffer.BlockCopy(cipherTextWithIvBytes, IvSize, cipherText, 0, cipherText.Length); - - 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)) - { - cs.Write(cipherText, 0, cipherText.Length); - cs.FlushFinalBlock(); - } - - var plainText = ms.ToArray(); - return Encoding.UTF8.GetString(plainText); - } - - private static byte[] GetKey(string? password) - { - if (password.IsNullOrEmpty()) - { - password = DefaultPassword; - } - - using var pbkdf2 = new Rfc2898DeriveBytes(password, Salt, Iterations, HashAlgorithmName.SHA256); - return pbkdf2.GetBytes(KeySize / 8); - } - - private static byte[] GenerateIv() - { - var randomNumber = new byte[IvSize]; - - using var rng = RandomNumberGenerator.Create(); - rng.GetBytes(randomNumber); - return randomNumber; - } -} diff --git a/v2rayN/ServiceLib/Common/DesUtils.cs b/v2rayN/ServiceLib/Common/DesUtils.cs deleted file mode 100644 index aae02206..00000000 --- a/v2rayN/ServiceLib/Common/DesUtils.cs +++ /dev/null @@ -1,74 +0,0 @@ -using System.Security.Cryptography; -using System.Text; - -namespace ServiceLib.Common; - -public class DesUtils -{ - /// - /// Encrypt - /// - /// - /// /// - /// - public static string Encrypt(string? text, string? key = null) - { - if (text.IsNullOrEmpty()) - { - return string.Empty; - } - GetKeyIv(key ?? GetDefaultKey(), out var rgbKey, out var rgbIv); - var dsp = DES.Create(); - using var memStream = new MemoryStream(); - using var cryStream = new CryptoStream(memStream, dsp.CreateEncryptor(rgbKey, rgbIv), CryptoStreamMode.Write); - using var sWriter = new StreamWriter(cryStream); - sWriter.Write(text); - sWriter.Flush(); - cryStream.FlushFinalBlock(); - memStream.Flush(); - return Convert.ToBase64String(memStream.GetBuffer(), 0, (int)memStream.Length); - } - - /// - /// Decrypt - /// - /// - /// - /// - public static string Decrypt(string? encryptText, string? key = null) - { - if (encryptText.IsNullOrEmpty()) - { - return string.Empty; - } - GetKeyIv(key ?? GetDefaultKey(), out var rgbKey, out var rgbIv); - var dsp = DES.Create(); - var buffer = Convert.FromBase64String(encryptText); - - using var memStream = new MemoryStream(); - using var cryStream = new CryptoStream(memStream, dsp.CreateDecryptor(rgbKey, rgbIv), CryptoStreamMode.Write); - cryStream.Write(buffer, 0, buffer.Length); - cryStream.FlushFinalBlock(); - return Encoding.UTF8.GetString(memStream.ToArray()); - } - - private static void GetKeyIv(string key, out byte[] rgbKey, out byte[] rgbIv) - { - if (key.IsNullOrEmpty()) - { - throw new ArgumentNullException("The key cannot be null"); - } - if (key.Length <= 8) - { - throw new ArgumentNullException("The key length cannot be less than 8 characters."); - } - - rgbKey = Encoding.ASCII.GetBytes(key.Substring(0, 8)); - rgbIv = Encoding.ASCII.GetBytes(key.Insert(0, "w").Substring(0, 8)); - } - - private static string GetDefaultKey() - { - return Utils.GetMd5(Utils.GetHomePath() + "DesUtils"); - } -}