mirror of
https://github.com/2dust/v2rayN.git
synced 2025-08-24 03:46:55 +00:00
Compare commits
4 commits
89ce7c23c9
...
01d35456bd
Author | SHA1 | Date | |
---|---|---|---|
![]() |
01d35456bd | ||
![]() |
672b8c48ac | ||
![]() |
ac1a357740 | ||
![]() |
504f8d09a6 |
2 changed files with 50 additions and 65 deletions
|
@ -1,5 +1,3 @@
|
||||||
using System;
|
|
||||||
using System.IO;
|
|
||||||
using System.Security.Cryptography;
|
using System.Security.Cryptography;
|
||||||
using System.Text;
|
using System.Text;
|
||||||
|
|
||||||
|
@ -10,106 +8,94 @@ namespace ServiceLib.Common
|
||||||
private const int KeySize = 256; // AES-256
|
private const int KeySize = 256; // AES-256
|
||||||
private const int IvSize = 16; // AES block size
|
private const int IvSize = 16; // AES block size
|
||||||
private const int Iterations = 10000;
|
private const int Iterations = 10000;
|
||||||
|
private static readonly byte[] Salt = Encoding.ASCII.GetBytes("saltysalt".PadRight(16, ' ')); // google浏览器默认盐值
|
||||||
private static readonly byte[] Salt = Encoding.ASCII.GetBytes("saltysalt".PadRight(16, ' '));//google浏览器默认盐值
|
private static readonly string DefaultPassword = Utils.GetMd5(Utils.GetHomePath() + "AesUtils");
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Encrypt
|
/// Encrypt
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="text">Plain text</param>
|
/// <param name="text">Plain text</param>
|
||||||
/// <param name="password">Password for key derivation</param>
|
/// <param name="password">Password for key derivation or direct key in ASCII bytes</param>
|
||||||
/// <returns>Base64 encoded cipher text with IV</returns>
|
/// <returns>Base64 encoded cipher text with IV</returns>
|
||||||
public static string Encrypt(string text, string password)
|
public static string Encrypt(string text, string? password = null)
|
||||||
{
|
{
|
||||||
if (string.IsNullOrEmpty(text))
|
if (string.IsNullOrEmpty(text))
|
||||||
return string.Empty;
|
return string.Empty;
|
||||||
|
|
||||||
if (string.IsNullOrEmpty(password))
|
var plaintext = Encoding.UTF8.GetBytes(text);
|
||||||
throw new ArgumentNullException("Password cannot be null.");
|
var key = GetKey(password);
|
||||||
|
var iv = GenerateIv();
|
||||||
|
|
||||||
byte[] plaintext = Encoding.UTF8.GetBytes(text);
|
using var aes = Aes.Create();
|
||||||
byte[] key = GetDefaultKey(password);
|
aes.Key = key;
|
||||||
byte[] iv = GenerateIv();
|
aes.IV = iv;
|
||||||
|
|
||||||
using (Aes aes = Aes.Create())
|
using var ms = new MemoryStream();
|
||||||
|
ms.Write(iv, 0, iv.Length);
|
||||||
|
|
||||||
|
using (var cs = new CryptoStream(ms, aes.CreateEncryptor(), CryptoStreamMode.Write))
|
||||||
{
|
{
|
||||||
aes.Key = key;
|
cs.Write(plaintext, 0, plaintext.Length);
|
||||||
aes.IV = iv;
|
cs.FlushFinalBlock();
|
||||||
|
|
||||||
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);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
var cipherTextWithIv = ms.ToArray();
|
||||||
|
return Convert.ToBase64String(cipherTextWithIv);
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Decrypt
|
/// Decrypt
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="cipherTextWithIv">Base64 encoded cipher text with IV</param>
|
/// <param name="cipherTextWithIv">Base64 encoded cipher text with IV</param>
|
||||||
/// <param name="password">Password for key derivation</param>
|
/// <param name="password">Password for key derivation or direct key in ASCII bytes</param>
|
||||||
/// <returns>Plain text</returns>
|
/// <returns>Plain text</returns>
|
||||||
public static string Decrypt(string cipherTextWithIv, string password)
|
public static string Decrypt(string cipherTextWithIv, string? password = null)
|
||||||
{
|
{
|
||||||
if (string.IsNullOrEmpty(cipherTextWithIv))
|
if (string.IsNullOrEmpty(cipherTextWithIv))
|
||||||
return string.Empty;
|
return string.Empty;
|
||||||
|
|
||||||
if (string.IsNullOrEmpty(password))
|
var cipherTextWithIvBytes = Convert.FromBase64String(cipherTextWithIv);
|
||||||
throw new ArgumentNullException("Password cannot be null.");
|
var key = GetKey(password);
|
||||||
|
|
||||||
byte[] cipherTextWithIvBytes = Convert.FromBase64String(cipherTextWithIv);
|
var iv = new byte[IvSize];
|
||||||
byte[] key = GetDefaultKey(password);
|
|
||||||
|
|
||||||
byte[] iv = new byte[IvSize];
|
|
||||||
Buffer.BlockCopy(cipherTextWithIvBytes, 0, iv, 0, 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 - 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;
|
cs.Write(cipherText, 0, cipherText.Length);
|
||||||
aes.IV = iv;
|
cs.FlushFinalBlock();
|
||||||
|
|
||||||
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);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
var plainText = ms.ToArray();
|
||||||
|
return Encoding.UTF8.GetString(plainText);
|
||||||
}
|
}
|
||||||
|
|
||||||
private static byte[] GetDefaultKey(string password)
|
private static byte[] GetKey(string? password)
|
||||||
{
|
{
|
||||||
using (Rfc2898DeriveBytes pbkdf2 = new Rfc2898DeriveBytes(password, Salt, Iterations, HashAlgorithmName.SHA256))
|
if (password.IsNullOrEmpty())
|
||||||
{
|
{
|
||||||
return pbkdf2.GetBytes(KeySize / 8);
|
password = DefaultPassword;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
using var pbkdf2 = new Rfc2898DeriveBytes(password, Salt, Iterations, HashAlgorithmName.SHA256);
|
||||||
|
return pbkdf2.GetBytes(KeySize / 8);
|
||||||
}
|
}
|
||||||
|
|
||||||
private static byte[] GenerateIv()
|
private static byte[] GenerateIv()
|
||||||
{
|
{
|
||||||
using (RNGCryptoServiceProvider rng = new RNGCryptoServiceProvider())
|
var randomNumber = new byte[IvSize];
|
||||||
{
|
|
||||||
byte[] iv = new byte[IvSize];
|
using var rng = RandomNumberGenerator.Create();
|
||||||
rng.GetBytes(iv);
|
rng.GetBytes(randomNumber);
|
||||||
return iv;
|
return randomNumber;
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
|
@ -439,8 +439,7 @@ namespace ServiceLib.ViewModels
|
||||||
}
|
}
|
||||||
else if (Utils.IsOSX())
|
else if (Utils.IsOSX())
|
||||||
{
|
{
|
||||||
//TODO
|
return _config.TunModeItem.LinuxSudoPwd.IsNotEmpty();
|
||||||
return false;
|
|
||||||
}
|
}
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue