mirror of
https://github.com/MikuLeaks/KianaBH3.git
synced 2025-12-12 13:04:33 +01:00
56 lines
1.8 KiB
C#
56 lines
1.8 KiB
C#
using System.Security.Cryptography;
|
|
using System.Text;
|
|
using System.Text.Json;
|
|
|
|
namespace KianaBH.Util.Crypto;
|
|
|
|
public static class DispatchEncryption
|
|
{
|
|
private static readonly JsonSerializerOptions JsonSerializerOptions = new()
|
|
{
|
|
PropertyNamingPolicy = JsonNamingPolicy.SnakeCaseLower
|
|
};
|
|
|
|
public static string? EncryptDispatchContent(string version, object? data)
|
|
{
|
|
if (!ConfigManager.Hotfix.AesKeys.TryGetValue(version, out var aesKey))
|
|
return null;
|
|
|
|
var serializedData = JsonSerializer.Serialize(data, JsonSerializerOptions);
|
|
var keyBytes = aesKey.Split(' ')
|
|
.Select(b => Convert.ToByte(b, 16))
|
|
.ToArray();
|
|
|
|
using var aes = Aes.Create();
|
|
aes.Mode = CipherMode.ECB;
|
|
aes.Padding = PaddingMode.PKCS7;
|
|
aes.Key = keyBytes;
|
|
|
|
var encryptor = aes.CreateEncryptor();
|
|
var dataBytes = Encoding.UTF8.GetBytes(serializedData);
|
|
var encryptedBytes = encryptor.TransformFinalBlock(dataBytes, 0, dataBytes.Length);
|
|
|
|
return Convert.ToBase64String(encryptedBytes);
|
|
}
|
|
|
|
public static string? DecryptDispatchContent(string version, string base64Data)
|
|
{
|
|
if (!ConfigManager.Hotfix.AesKeys.TryGetValue(version, out var aesKey))
|
|
return null;
|
|
|
|
var keyBytes = aesKey.Split(' ')
|
|
.Select(b => Convert.ToByte(b, 16))
|
|
.ToArray();
|
|
|
|
using var aes = Aes.Create();
|
|
aes.Mode = CipherMode.ECB;
|
|
aes.Padding = PaddingMode.PKCS7;
|
|
aes.Key = keyBytes;
|
|
|
|
var decryptor = aes.CreateDecryptor();
|
|
var dataBytes = Convert.FromBase64String(base64Data);
|
|
var decryptedBytes = decryptor.TransformFinalBlock(dataBytes, 0, dataBytes.Length);
|
|
|
|
return Encoding.UTF8.GetString(decryptedBytes);
|
|
}
|
|
} |