37 lines
702 B
C#
37 lines
702 B
C#
using System;
|
|
using System.Security.Cryptography;
|
|
using System.Text;
|
|
|
|
namespace Hua.DotNet.Code.Helper;
|
|
|
|
public class RsaHelper
|
|
{
|
|
private static RSA? _Rsa;
|
|
|
|
private static void InitRsa()
|
|
{
|
|
if (_Rsa == null)
|
|
{
|
|
_Rsa = RSA.Create();
|
|
}
|
|
}
|
|
|
|
public static string Encrypt(string str)
|
|
{
|
|
InitRsa();
|
|
return Convert.ToBase64String(_Rsa.Encrypt(Encoding.UTF8.GetBytes(str), RSAEncryptionPadding.Pkcs1));
|
|
}
|
|
|
|
public static string Decrypt(string enStr)
|
|
{
|
|
InitRsa();
|
|
return Encoding.UTF8.GetString(_Rsa.Decrypt(Convert.FromBase64String(enStr), RSAEncryptionPadding.Pkcs1));
|
|
}
|
|
|
|
public static string PublicKey()
|
|
{
|
|
InitRsa();
|
|
return _Rsa.ToXmlString(includePrivateParameters: false);
|
|
}
|
|
}
|