From bd74b82fd6866b2e1e5c62d3bdef89877d3cc2e2 Mon Sep 17 00:00:00 2001 From: uye <99072975+ABA2396@users.noreply.github.com> Date: Mon, 17 Feb 2025 12:57:10 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20SimpleEncryptionHelper=20=E6=94=AF?= =?UTF-8?q?=E6=8C=81=E9=BB=98=E8=AE=A4=E5=AD=97=E7=AC=A6=E4=B8=B2?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Helper/SimpleEncryptionHelper.cs | 30 +++++++++++++++++-- ...nalNotificationSettingsUserControlModel.cs | 2 +- 2 files changed, 29 insertions(+), 3 deletions(-) diff --git a/src/MaaWpfGui/Helper/SimpleEncryptionHelper.cs b/src/MaaWpfGui/Helper/SimpleEncryptionHelper.cs index c166df55d6..0218a79ce9 100644 --- a/src/MaaWpfGui/Helper/SimpleEncryptionHelper.cs +++ b/src/MaaWpfGui/Helper/SimpleEncryptionHelper.cs @@ -10,13 +10,26 @@ public static class SimpleEncryptionHelper { private static readonly ILogger _logger = Log.ForContext("SourceContext", "SimpleEncryptionHelper"); - public static string Encrypt(string plainText, DataProtectionScope dataProtectionScope = DataProtectionScope.CurrentUser, [CallerMemberName] string caller = "") + /// + /// 使用数据保护API对明文进行加密。 + /// + /// 需要加密的文本。如果为null或空字符串,则返回空字符串。 + /// 用于比较的默认文本。如果plainText与defaultText相同,则直接返回plainText而不加密。 + /// 数据保护的范围,默认为CurrentUser(当前用户)。 + /// 调用方法的名称,由编译器自动填充。 + /// 返回加密后的Base64编码字符串。如果加密失败,则返回原始plainText。 + public static string Encrypt(string plainText, string defaultText = "", DataProtectionScope dataProtectionScope = DataProtectionScope.CurrentUser, [CallerMemberName] string caller = "") { if (string.IsNullOrEmpty(plainText)) { return string.Empty; } + if (plainText == defaultText) + { + return plainText; + } + try { var data = Encoding.UTF8.GetBytes(plainText); @@ -31,13 +44,26 @@ public static class SimpleEncryptionHelper } } - public static string Decrypt(string encryptedText, DataProtectionScope dataProtectionScope = DataProtectionScope.CurrentUser, [CallerMemberName] string caller = "") + /// + /// 使用数据保护API对加密文本进行解密。 + /// + /// 需要解密的Base64编码加密文本。如果为null或空字符串,则返回空字符串。 + /// 用于比较的默认文本。如果encryptedText与defaultText相同,则直接返回defaultText而不解密。 + /// 数据保护的范围,默认为CurrentUser(当前用户)。 + /// 调用方法的名称,由编译器自动填充。 + /// 返回解密后的文本。如果解密失败,则返回原始encryptedText。 + public static string Decrypt(string encryptedText, string defaultText = "", DataProtectionScope dataProtectionScope = DataProtectionScope.CurrentUser, [CallerMemberName] string caller = "") { if (string.IsNullOrEmpty(encryptedText)) { return string.Empty; } + if (encryptedText == defaultText) + { + return defaultText; + } + try { var data = Convert.FromBase64String(encryptedText); diff --git a/src/MaaWpfGui/ViewModels/UserControl/Settings/ExternalNotificationSettingsUserControlModel.cs b/src/MaaWpfGui/ViewModels/UserControl/Settings/ExternalNotificationSettingsUserControlModel.cs index 1da41ca809..595c90a000 100644 --- a/src/MaaWpfGui/ViewModels/UserControl/Settings/ExternalNotificationSettingsUserControlModel.cs +++ b/src/MaaWpfGui/ViewModels/UserControl/Settings/ExternalNotificationSettingsUserControlModel.cs @@ -187,7 +187,7 @@ public class ExternalNotificationSettingsUserControlModel : PropertyChangedBase } } - private string _barkServer = SimpleEncryptionHelper.Decrypt(ConfigurationHelper.GetValue(ConfigurationKeys.ExternalNotificationBarkServer, "https://api.day.app")); + private string _barkServer = SimpleEncryptionHelper.Decrypt(ConfigurationHelper.GetValue(ConfigurationKeys.ExternalNotificationBarkServer, "https://api.day.app"), "https://api.day.app"); public string BarkServer {