风光大葬

This commit is contained in:
2025-11-04 11:37:27 +08:00
parent 628cfabf97
commit bdf67a0312
329 changed files with 3048 additions and 1222 deletions

View File

@@ -1,6 +1,4 @@
using Microsoft.VisualBasic;
using MongoDB.Driver;
using MongoDB.Driver.Core.Servers;
using NPOI.SS.UserModel;
using NPOI.XSSF.UserModel;
using System;
@@ -53,17 +51,57 @@ namespace Ramitta.lib
Dispatcher.CurrentDispatcher.Invoke(DispatcherPriority.Background, new Action(delegate { }));
});
}
public static SolidColorBrush GenerateRandomColor(SolidColorBrush color = null)
public static SolidColorBrush GenerateRandomColor(SolidColorBrush color = null, bool? requireLightColor = null)
{
// 如果 color 参数为 null生成一个新的随机颜色
if (color == null)
{
Random random = new Random();
byte r = (byte)random.Next(0, 256); // 红色分量 0 - 255
byte g = (byte)random.Next(0, 256); // 绿色分量 0 - 255
byte b = (byte)random.Next(0, 256); // 蓝色分量 0 - 255
color = new SolidColorBrush(Color.FromRgb(r, g, b));
// 根据需求生成亮色或暗色
if (requireLightColor.HasValue)
{
int attempts = 0;
const int maxAttempts = 100; // 防止无限循环
while (attempts < maxAttempts)
{
byte r = (byte)random.Next(0, 256);
byte g = (byte)random.Next(0, 256);
byte b = (byte)random.Next(0, 256);
// 计算亮度 (使用常见的亮度公式)
double brightness = (0.299 * r + 0.587 * g + 0.114 * b) / 255;
// 检查是否符合要求
bool isLight = brightness > 0.5;
if ((requireLightColor.Value && isLight) || (!requireLightColor.Value && !isLight))
{
color = new SolidColorBrush(Color.FromRgb(r, g, b));
break;
}
attempts++;
}
// 如果多次尝试仍未找到合适的颜色,使用默认方法
if (color == null)
{
byte r = (byte)random.Next(0, 256);
byte g = (byte)random.Next(0, 256);
byte b = (byte)random.Next(0, 256);
color = new SolidColorBrush(Color.FromRgb(r, g, b));
}
}
else
{
// 不限制亮度,使用原来的随机方法
byte r = (byte)random.Next(0, 256);
byte g = (byte)random.Next(0, 256);
byte b = (byte)random.Next(0, 256);
color = new SolidColorBrush(Color.FromRgb(r, g, b));
}
}
return color;
@@ -363,123 +401,4 @@ namespace Ramitta.lib
}
}
}
public class CryptoHelper
{
/// <summary>
/// 生成RSA密钥对
/// </summary>
/// <param name="keySize">密钥长度通常为2048、4096</param>
/// <returns>包含公钥和私钥的元组</returns>
public static (string publicKey, string privateKey) GenerateKeyPair(int keySize = 2048)
{
try
{
using (var rsa = RSA.Create(keySize))
{
string publicKey = rsa.ToXmlString(false);
string privateKey = rsa.ToXmlString(true);
return (publicKey, privateKey);
}
}
catch (Exception ex)
{
MessageBox.Show($"密钥生成失败: {ex.Message}");
return (null, null);
}
}
/// <summary>
/// 使用私钥对数据进行签名
/// </summary>
public static string? SignData(string data, string privateKey)
{
try
{
using (var rsa = RSA.Create())
{
rsa.FromXmlString(privateKey);
byte[] dataBytes = Encoding.UTF8.GetBytes(data);
byte[] signature = rsa.SignData(dataBytes, HashAlgorithmName.SHA256, RSASignaturePadding.Pkcs1);
return Convert.ToBase64String(signature);
}
}
catch (Exception ex)
{
return null;
}
}
/// <summary>
/// 使用公钥验证签名
/// </summary>
public static bool? VerifySignature(string data, string signature, string publicKey)
{
try
{
using (var rsa = RSA.Create())
{
rsa.FromXmlString(publicKey);
byte[] dataBytes = Encoding.UTF8.GetBytes(data);
byte[] signatureBytes = Convert.FromBase64String(signature);
return rsa.VerifyData(dataBytes, signatureBytes, HashAlgorithmName.SHA256, RSASignaturePadding.Pkcs1);
}
}
catch (Exception ex)
{
return false;
}
}
/// <summary>
/// 使用公钥加密数据
/// </summary>
public static string? Encrypt(string data, string publicKey)
{
try
{
using (var rsa = RSA.Create())
{
rsa.FromXmlString(publicKey);
byte[] dataBytes = Encoding.UTF8.GetBytes(data);
byte[] encryptedBytes = rsa.Encrypt(dataBytes, RSAEncryptionPadding.OaepSHA256);
return Convert.ToBase64String(encryptedBytes);
}
}
catch (Exception ex)
{
return null;
}
}
/// <summary>
/// 使用私钥解密数据
/// </summary>
public static string? Decrypt(string encryptedData, string privateKey)
{
try
{
using (var rsa = RSA.Create())
{
rsa.FromXmlString(privateKey);
byte[] encryptedBytes = Convert.FromBase64String(encryptedData);
byte[] decryptedBytes = rsa.Decrypt(encryptedBytes, RSAEncryptionPadding.OaepSHA256);
return Encoding.UTF8.GetString(decryptedBytes);
}
}
catch (Exception ex)
{
return null;
}
}
}
}