-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'master' into production
- Loading branch information
Showing
4 changed files
with
58 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
using System; | ||
using System.Security.Cryptography; | ||
using System.Text; | ||
|
||
namespace SageTools.Utils | ||
{ | ||
/// <summary> | ||
/// RSA非对称算法加/解密工具类 | ||
/// </summary> | ||
public static class RsaDecryptUtils | ||
{ | ||
/// <summary> | ||
/// 生成一组公钥和私钥 | ||
/// </summary> | ||
/// <param name="publicKey">公钥</param> | ||
/// <param name="privateKey">私钥</param> | ||
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)); | ||
} | ||
|
||
/// <summary> | ||
/// 加密 | ||
/// </summary> | ||
/// <param name="originalText">原文</param> | ||
/// <param name="publicKey">公钥</param> | ||
/// <returns></returns> | ||
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); | ||
} | ||
|
||
/// <summary> | ||
/// 解密 | ||
/// </summary> | ||
/// <param name="encryptedData">密文</param> | ||
/// <param name="privateKey">私钥</param> | ||
/// <returns></returns> | ||
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); | ||
} | ||
} | ||
} |