diff --git a/framework/Furion.Pure/DataEncryption/Encryptions/RSAEncryption.cs b/framework/Furion.Pure/DataEncryption/Encryptions/RSAEncryption.cs
new file mode 100644
index 0000000000000000000000000000000000000000..de9a413ad9ea7d7ac9a438fb985006e602065d39
--- /dev/null
+++ b/framework/Furion.Pure/DataEncryption/Encryptions/RSAEncryption.cs
@@ -0,0 +1,122 @@
+// -----------------------------------------------------------------------------
+// 让 .NET 开发更简单,更通用,更流行。
+// Copyright © 2020-2021 Furion, 百小僧, Baiqian Co.,Ltd.
+//
+// 框架名称:Furion
+// 框架作者:百小僧
+// 框架版本:2.11.1
+// 源码地址:Gitee: https://gitee.com/dotnetchina/Furion
+// Github:https://github.com/monksoul/Furion
+// 开源协议:Apache-2.0(https://gitee.com/dotnetchina/Furion/blob/master/LICENSE)
+// -----------------------------------------------------------------------------
+
+using Furion.DependencyInjection;
+using System;
+using System.Security.Cryptography;
+using System.Text;
+
+namespace Furion.DataEncryption
+{
+ ///
+ /// RSA密钥对结构体
+ ///
+ public struct RsaSecretKey
+ {
+ ///
+ /// 初始化秘钥
+ ///
+ ///
+ ///
+ public RsaSecretKey(string privateKey, string publicKey)
+ {
+ PrivateKey = privateKey;
+ PublicKey = publicKey;
+ }
+
+ ///
+ /// 公钥
+ ///
+ public string PublicKey { get; set; }
+
+ ///
+ /// 私钥
+ ///
+ public string PrivateKey { get; set; }
+ }
+
+
+ ///
+ /// RSA加密函数
+ ///
+ [SuppressSniffer]
+ public class RSAEncryption
+ {
+ ///
+ /// 生成RSA秘钥
+ ///
+ /// 大小必须为2048到16384之间,且必须能被8整除
+ ///
+ public static RsaSecretKey GenerateRsaSecretKey(int keySize)
+ {
+ if (keySize < 2048 || keySize > 16384 || keySize % 8 != 0)
+ throw new ArgumentException("keySize must be between 2048 and 16384 in size and must be divisible by 8.",
+ "keySize");
+ var rsaKey = new RsaSecretKey();
+ using (var rsa = new RSACryptoServiceProvider(keySize))
+ {
+ rsaKey.PrivateKey = rsa.ToXmlString(true);
+ rsaKey.PublicKey = rsa.ToXmlString(false);
+ }
+
+ return rsaKey;
+ }
+
+
+ ///
+ /// 加密
+ ///
+ /// 明文内容
+ /// 公钥
+ ///
+ public static string Encrypt(string content, string xmlPublicKey)
+ {
+ try
+ {
+ string encryptedContent;
+ using var rsa = new RSACryptoServiceProvider();
+ rsa.FromXmlString(xmlPublicKey);
+ var encryptedData = rsa.Encrypt(Encoding.Default.GetBytes(content), false);
+ encryptedContent = Convert.ToBase64String(encryptedData);
+
+ return encryptedContent;
+ }
+ catch (Exception)
+ {
+ throw new Exception("Encryption error.Please check that the xmlPublicKey is valid.");
+ }
+ }
+
+ ///
+ /// 解密
+ ///
+ /// 密文内容
+ /// 私钥
+ ///
+ public static string Decrypt(string content, string xmlPrivateKey)
+ {
+ try
+ {
+ string decryptedContent;
+ using var rsa = new RSACryptoServiceProvider();
+ rsa.FromXmlString(xmlPrivateKey);
+ var decryptedData = rsa.Decrypt(Convert.FromBase64String(content), false);
+ decryptedContent = Encoding.Default.GetString(decryptedData);
+ return decryptedContent;
+ }
+ catch (Exception)
+ {
+ throw new Exception("Decryption error. Please check whether the xmlPrivateKey and ciphertext match.");
+ }
+ }
+ }
+}
\ No newline at end of file
diff --git a/framework/Furion.Pure/DataEncryption/Extensions/DataEncryptionStringExtensions.cs b/framework/Furion.Pure/DataEncryption/Extensions/DataEncryptionStringExtensions.cs
index 47da8e54e9ef4a2c3e767280a5a620231017b9aa..1a945cdbfc7ede44817e7500ca5362116632b66d 100644
--- a/framework/Furion.Pure/DataEncryption/Extensions/DataEncryptionStringExtensions.cs
+++ b/framework/Furion.Pure/DataEncryption/Extensions/DataEncryptionStringExtensions.cs
@@ -109,5 +109,27 @@ namespace Furion.DataEncryption.Extensions
{
return PBKDF2Encryption.Compare(text, encryptText);
}
+
+ ///
+ /// 字符串 RSA 解密
+ ///
+ /// 需要解密的文本
+ /// 私钥
+ ///
+ public static string ToRSADecrypt(this string text, string xmlPrivateKey)
+ {
+ return RSAEncryption.Decrypt(text, xmlPrivateKey);
+ }
+
+ ///
+ /// 字符串 RSA 加密
+ ///
+ /// 需要加密的文本
+ /// 公钥
+ ///
+ public static string ToRSAEncrpyt(this string text, string xmlPublicKey)
+ {
+ return RSAEncryption.Encrypt(text, xmlPublicKey);
+ }
}
}
\ No newline at end of file
diff --git a/framework/Furion/DataEncryption/Encryptions/RSAEncryption.cs b/framework/Furion/DataEncryption/Encryptions/RSAEncryption.cs
new file mode 100644
index 0000000000000000000000000000000000000000..de9a413ad9ea7d7ac9a438fb985006e602065d39
--- /dev/null
+++ b/framework/Furion/DataEncryption/Encryptions/RSAEncryption.cs
@@ -0,0 +1,122 @@
+// -----------------------------------------------------------------------------
+// 让 .NET 开发更简单,更通用,更流行。
+// Copyright © 2020-2021 Furion, 百小僧, Baiqian Co.,Ltd.
+//
+// 框架名称:Furion
+// 框架作者:百小僧
+// 框架版本:2.11.1
+// 源码地址:Gitee: https://gitee.com/dotnetchina/Furion
+// Github:https://github.com/monksoul/Furion
+// 开源协议:Apache-2.0(https://gitee.com/dotnetchina/Furion/blob/master/LICENSE)
+// -----------------------------------------------------------------------------
+
+using Furion.DependencyInjection;
+using System;
+using System.Security.Cryptography;
+using System.Text;
+
+namespace Furion.DataEncryption
+{
+ ///
+ /// RSA密钥对结构体
+ ///
+ public struct RsaSecretKey
+ {
+ ///
+ /// 初始化秘钥
+ ///
+ ///
+ ///
+ public RsaSecretKey(string privateKey, string publicKey)
+ {
+ PrivateKey = privateKey;
+ PublicKey = publicKey;
+ }
+
+ ///
+ /// 公钥
+ ///
+ public string PublicKey { get; set; }
+
+ ///
+ /// 私钥
+ ///
+ public string PrivateKey { get; set; }
+ }
+
+
+ ///
+ /// RSA加密函数
+ ///
+ [SuppressSniffer]
+ public class RSAEncryption
+ {
+ ///
+ /// 生成RSA秘钥
+ ///
+ /// 大小必须为2048到16384之间,且必须能被8整除
+ ///
+ public static RsaSecretKey GenerateRsaSecretKey(int keySize)
+ {
+ if (keySize < 2048 || keySize > 16384 || keySize % 8 != 0)
+ throw new ArgumentException("keySize must be between 2048 and 16384 in size and must be divisible by 8.",
+ "keySize");
+ var rsaKey = new RsaSecretKey();
+ using (var rsa = new RSACryptoServiceProvider(keySize))
+ {
+ rsaKey.PrivateKey = rsa.ToXmlString(true);
+ rsaKey.PublicKey = rsa.ToXmlString(false);
+ }
+
+ return rsaKey;
+ }
+
+
+ ///
+ /// 加密
+ ///
+ /// 明文内容
+ /// 公钥
+ ///
+ public static string Encrypt(string content, string xmlPublicKey)
+ {
+ try
+ {
+ string encryptedContent;
+ using var rsa = new RSACryptoServiceProvider();
+ rsa.FromXmlString(xmlPublicKey);
+ var encryptedData = rsa.Encrypt(Encoding.Default.GetBytes(content), false);
+ encryptedContent = Convert.ToBase64String(encryptedData);
+
+ return encryptedContent;
+ }
+ catch (Exception)
+ {
+ throw new Exception("Encryption error.Please check that the xmlPublicKey is valid.");
+ }
+ }
+
+ ///
+ /// 解密
+ ///
+ /// 密文内容
+ /// 私钥
+ ///
+ public static string Decrypt(string content, string xmlPrivateKey)
+ {
+ try
+ {
+ string decryptedContent;
+ using var rsa = new RSACryptoServiceProvider();
+ rsa.FromXmlString(xmlPrivateKey);
+ var decryptedData = rsa.Decrypt(Convert.FromBase64String(content), false);
+ decryptedContent = Encoding.Default.GetString(decryptedData);
+ return decryptedContent;
+ }
+ catch (Exception)
+ {
+ throw new Exception("Decryption error. Please check whether the xmlPrivateKey and ciphertext match.");
+ }
+ }
+ }
+}
\ No newline at end of file
diff --git a/framework/Furion/DataEncryption/Extensions/DataEncryptionStringExtensions.cs b/framework/Furion/DataEncryption/Extensions/DataEncryptionStringExtensions.cs
index 47da8e54e9ef4a2c3e767280a5a620231017b9aa..e8a963b0e3d7a277b5bb7c90d1718a8a098c5db6 100644
--- a/framework/Furion/DataEncryption/Extensions/DataEncryptionStringExtensions.cs
+++ b/framework/Furion/DataEncryption/Extensions/DataEncryptionStringExtensions.cs
@@ -109,5 +109,27 @@ namespace Furion.DataEncryption.Extensions
{
return PBKDF2Encryption.Compare(text, encryptText);
}
+
+ ///
+ /// 字符串 RSA 解密
+ ///
+ /// 需要解密的文本
+ /// 私钥
+ ///
+ public static string ToRSADecrypt(this string text, string xmlPrivateKey)
+ {
+ return RSAEncryption.Decrypt(text,xmlPrivateKey);
+ }
+
+ ///
+ /// 字符串 RSA 加密
+ ///
+ /// 需要加密的文本
+ /// 公钥
+ ///
+ public static string ToRSAEncrpyt(this string text, string xmlPublicKey)
+ {
+ return RSAEncryption.Encrypt(text,xmlPublicKey);
+ }
}
}
\ No newline at end of file
diff --git a/handbook/docs/encryption.mdx b/handbook/docs/encryption.mdx
index ff1d4c27f87a295fac51579495aa72c5848cbcea..860a7435d98ce65abd9c17eb39685cc94752eea8 100644
--- a/handbook/docs/encryption.mdx
+++ b/handbook/docs/encryption.mdx
@@ -17,6 +17,7 @@ sidebar_label: 20. 数据加解密
- `AES` 加解密
- `JWT` 加解密
- `PBKDF2` 加密
+- `RSA` 加解密
## 20.3 加解密使用
@@ -106,9 +107,21 @@ services.AddPBKDF2EncryptionOptions();
:::
+### 20.3.6 `RSA` 加密
+
+```cs
+// 测试 RSA 加密
+var rsakey=RSAEncryption.GenerateRsaSecretKey(2048); //生成RSA秘钥 秘钥大小必须为2048到16384,并且是8的倍数
+var basestring = RSAEncryption.Encrypt("百小僧",rsakey.PublicKey); // 加密
+var str2 = RSAEncryption.Decrypt(basestring,rsakey.PrivateKey); // 解密
+return (basestring,str2);
+```
+
+
+
## 20.4 字符串拓展方式
-`Furion` 框架也提供了字符串拓展方式进行 `MD5加密、AES/DESC加解密`。
+`Furion` 框架也提供了字符串拓展方式进行 `MD5加密、AES/DESC加解密、RSA加解密`。
```cs
using Furion.DataEncryption.Extensions;
@@ -128,6 +141,11 @@ var str = s.ToDESCDecrypt("sfdsfdsfdsfdsfdsfdsfdsfdsfdfdsfdsfdfdfdfd");
// PBKDF2 加密
var s = "Furion".ToPBKDF2Encrypt();
var b = "Furion".ToPBKDF2Compare(s); // 比较
+
+// RSA 加解密
+var rsakey=RSAEncryption.GenerateRsaSecretKey(2048); //生成RSA秘钥 秘钥大小必须为2048到16384,并且是8的倍数
+var s= "Furion".ToRSAEncrpyt(rsakey.PublicKey); //加密
+var str=s.ToRSADecrypt(rsakey.PrivateKey); //解密
```
## 20.5 反馈与建议