Skip to content

Commit

Permalink
增加RSA工具类
Browse files Browse the repository at this point in the history
  • Loading branch information
YaKun committed Jul 27, 2023
1 parent cc585b1 commit a85a44e
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 4 deletions.
4 changes: 2 additions & 2 deletions SageTools.Console/Program.cs
Original file line number Diff line number Diff line change
@@ -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
{
Expand Down
2 changes: 1 addition & 1 deletion SageTools/Extension/DateTime.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);

/// <summary>
/// 获取时间戳
/// 获取时间戳(秒)
/// </summary>
/// <param name="this"></param>
/// <returns></returns>
Expand Down
2 changes: 1 addition & 1 deletion SageTools/Extension/String.cs
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ public static bool ToBool(this string str)
/// <summary>
/// string.Format()拓展
/// </summary>
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);

/// <summary>
/// string.Join()拓展
Expand Down
54 changes: 54 additions & 0 deletions SageTools/Utils/RsaDecryptUtils.cs
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);
}
}
}

0 comments on commit a85a44e

Please sign in to comment.