From a85a44e49f935ec79b7abd14c8844ffec2eead4a Mon Sep 17 00:00:00 2001 From: YaKun Date: Thu, 27 Jul 2023 13:27:39 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0RSA=E5=B7=A5=E5=85=B7?= =?UTF-8?q?=E7=B1=BB?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- SageTools.Console/Program.cs | 4 +-- SageTools/Extension/DateTime.cs | 2 +- SageTools/Extension/String.cs | 2 +- SageTools/Utils/RsaDecryptUtils.cs | 54 ++++++++++++++++++++++++++++++ 4 files changed, 58 insertions(+), 4 deletions(-) create mode 100644 SageTools/Utils/RsaDecryptUtils.cs diff --git a/SageTools.Console/Program.cs b/SageTools.Console/Program.cs index 71f51b2..9936bf5 100644 --- a/SageTools.Console/Program.cs +++ b/SageTools.Console/Program.cs @@ -1,8 +1,8 @@ -using System; +using SageTools.Extension; +using System; using System.IO; using System.Linq; using System.Xml; -using SageTools.Extension; namespace SageTools.Console { diff --git a/SageTools/Extension/DateTime.cs b/SageTools/Extension/DateTime.cs index dcd7f58..310b204 100644 --- a/SageTools/Extension/DateTime.cs +++ b/SageTools/Extension/DateTime.cs @@ -166,7 +166,7 @@ private static string ToRelativeTimeSimple(TimeSpan ts, string sign) public static string ToRFC1123String(this DateTime @this, CultureInfo culture) => @this.ToString("r", (IFormatProvider)culture); /// - /// 获取时间戳 + /// 获取时间戳(秒) /// /// /// diff --git a/SageTools/Extension/String.cs b/SageTools/Extension/String.cs index 3844743..7710133 100644 --- a/SageTools/Extension/String.cs +++ b/SageTools/Extension/String.cs @@ -51,7 +51,7 @@ public static bool ToBool(this string str) /// /// string.Format()拓展 /// - public static string Format(this string str, params object[] args) => string.Format(str, args); + public static string FormatWith(this string str, params object[] args) => string.Format(str, args); /// /// string.Join()拓展 diff --git a/SageTools/Utils/RsaDecryptUtils.cs b/SageTools/Utils/RsaDecryptUtils.cs new file mode 100644 index 0000000..49828ea --- /dev/null +++ b/SageTools/Utils/RsaDecryptUtils.cs @@ -0,0 +1,54 @@ +using System; +using System.Security.Cryptography; +using System.Text; + +namespace SageTools.Utils +{ + /// + /// RSA非对称算法加/解密工具类 + /// + public static class RsaDecryptUtils + { + /// + /// 生成一组公钥和私钥 + /// + /// 公钥 + /// 私钥 + public static void GenerateKeys(out string publicKey, out string privateKey) + { + using var rsa = new RSACryptoServiceProvider(); + publicKey = Convert.ToBase64String(rsa.ExportCspBlob(false)); + privateKey = Convert.ToBase64String(rsa.ExportCspBlob(true)); + } + + /// + /// 加密 + /// + /// 原文 + /// 公钥 + /// + public static string Encrypt(string originalText, string publicKey) + { + var dataBytes = Encoding.UTF8.GetBytes(originalText); + using var rsa = new RSACryptoServiceProvider(); + rsa.ImportCspBlob(Convert.FromBase64String(publicKey)); + var encryptedData = rsa.Encrypt(dataBytes, false); + return Convert.ToBase64String(encryptedData); + } + + /// + /// 解密 + /// + /// 密文 + /// 私钥 + /// + public static string Decrypt(string encryptedData, string privateKey) + { + var encryptedDataBytes = Convert.FromBase64String(encryptedData); + using var rsa = new RSACryptoServiceProvider(); + rsa.ImportCspBlob(Convert.FromBase64String(privateKey)); + var decryptedDataBytes = rsa.Decrypt(encryptedDataBytes, false); + return Encoding.UTF8.GetString(decryptedDataBytes); + } + } +} \ No newline at end of file