Compare commits

..
2 Commits
2 changed files with 93 additions and 2 deletions

No files matched your search

+79
View File
@@ -0,0 +1,79 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.IO.Compression;
using System.Runtime.Intrinsics.X86;
using System.Security.Cryptography;
using System.Text;
namespace ForwardingServerUI.Class
{
public class AES
{
public static string AesEncipher(string Key, string dat) //加密
{
//压缩
byte[] plainBytes = Compress( Encoding.UTF8.GetBytes(dat));
using var sha256 = System.Security.Cryptography.SHA256.Create();
using var aes = System.Security.Cryptography.Aes.Create();
aes.Key = sha256.ComputeHash(Encoding.UTF8.GetBytes(Key));
aes.Mode = CipherMode.CBC;
aes.Padding = PaddingMode.PKCS7;
aes.GenerateIV(); // 生成随机 IV
using var enc = aes.CreateEncryptor();
byte[] cipherBytes = enc.TransformFinalBlock(plainBytes, 0, plainBytes.Length);
//将 IV (16字节) 和 密文 拼接成一个完整的字节数组
byte[] combinedBytes = new byte[aes.IV.Length + cipherBytes.Length];
Buffer.BlockCopy(aes.IV, 0, combinedBytes, 0, aes.IV.Length);
Buffer.BlockCopy(cipherBytes, 0, combinedBytes, aes.IV.Length, cipherBytes.Length);
return Convert.ToBase64String(combinedBytes);
}
public static string AesDecrypt(string Key, string dat)//解密
{
// 解码Base64
byte[] cipherBytes = Convert.FromBase64String(dat);
using var sha256 = System.Security.Cryptography.SHA256.Create();
using var aes = System.Security.Cryptography.Aes.Create();
aes.Key = sha256.ComputeHash(Encoding.UTF8.GetBytes(Key));
aes.Mode = CipherMode.CBC;
aes.Padding = PaddingMode.PKCS7;
// 从字节数组中提取前 16 字节作为 IV
aes.IV = cipherBytes.AsSpan(0, 16).ToArray();
using var dec = aes.CreateDecryptor();
// 解密
byte[] bytes = dec.TransformFinalBlock(cipherBytes, 16, cipherBytes.Length - 16);
//解压缩并返回
return Encoding.UTF8.GetString(Decompress(bytes));
}
// 3. Brotli 压缩辅助方法
private static byte[] Compress(byte[] data)
{
using var output = new MemoryStream();
using (var brotli = new BrotliStream(output, CompressionLevel.Optimal))
{
brotli.Write(data, 0, data.Length);
}
return output.ToArray();
}
// 4. Brotli 解压辅助方法
private static byte[] Decompress(byte[] data)
{
using var input = new MemoryStream(data);
using var brotli = new BrotliStream(input, CompressionMode.Decompress);
using var output = new MemoryStream();
brotli.CopyTo(output);
return output.ToArray();
}
}
}
+14 -2
View File
@@ -10,6 +10,7 @@ using System.Windows.Markup;
using System.Xml.Linq; using System.Xml.Linq;
using TouchSocket.Core; using TouchSocket.Core;
using TouchSocket.Sockets; using TouchSocket.Sockets;
using static ForwardingServerUI.MainWindowViewModel;
using static System.Runtime.InteropServices.JavaScript.JSType; using static System.Runtime.InteropServices.JavaScript.JSType;
namespace ForwardingServerUI.Class namespace ForwardingServerUI.Class
@@ -25,6 +26,14 @@ namespace ForwardingServerUI.Class
public required string SERRVERPASWORD { get; set; } public required string SERRVERPASWORD { get; set; }
public required string MachineVer { get; set; } public required string MachineVer { get; set; }
} }
public class PersonUser
{
public required string User { get; set; }
public required string Password { get; set; }
public required string IP { get; set; }
public required string Terminal { get; set; }
public required string Dat { get; set; }
}
private static Timer? _timer; private static Timer? _timer;
public static TcpService service = new TcpService(); public static TcpService service = new TcpService();
@@ -80,9 +89,12 @@ namespace ForwardingServerUI.Class
drEmployee["RDAT"] = trimmedDat; drEmployee["RDAT"] = trimmedDat;
drEmployee.EndEdit(); drEmployee.EndEdit();
drEmployee.AcceptChanges(); drEmployee.AcceptChanges();
}
}
PersonUser deserializedPersonuser = JsonSerializer.Deserialize<PersonUser>(trimmedDat)!;
var data = JsonSerializer.Deserialize<Dictionary<string, object>>
(AES.AesDecrypt(deserializedPersonuser.Terminal, deserializedPersonuser.Dat));
}
}
} }
else if (s_ == 90) else if (s_ == 90)
{ } { }