46 lines
1.5 KiB
C#
46 lines
1.5 KiB
C#
using Org.BouncyCastle.Crypto;
|
|
using Org.BouncyCastle.Crypto.Encodings;
|
|
using Org.BouncyCastle.Crypto.Engines;
|
|
using Org.BouncyCastle.Crypto.Parameters;
|
|
using Org.BouncyCastle.Security;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.IO;
|
|
using System.Linq;
|
|
using System.Security.Cryptography;
|
|
using System.Text;
|
|
using System.Text.RegularExpressions;
|
|
|
|
namespace WpfApp1.Util
|
|
{
|
|
public static class RSAHelper
|
|
{
|
|
/// <summary>
|
|
/// 公钥解密
|
|
/// </summary>
|
|
/// <param name="content">明文</param>
|
|
/// <param name="key">公钥</param>
|
|
public static string DecryptByPublicKey(string content, string key)
|
|
{
|
|
content = content.Replace("\r", "").Replace("\n", "").Replace(" ", "");
|
|
//非对称加密算法,加解密用
|
|
IAsymmetricBlockCipher engine = new Pkcs1Encoding(new RsaEngine());
|
|
|
|
//解密
|
|
try
|
|
{
|
|
RsaKeyParameters publicKeyParam = (RsaKeyParameters)PublicKeyFactory.CreateKey(Convert.FromBase64String(key));
|
|
engine.Init(false, publicKeyParam);
|
|
byte[] byteData = Convert.FromBase64String(content);
|
|
var ResultData = engine.ProcessBlock(byteData, 0, byteData.Length);
|
|
return System.Text.Encoding.UTF8.GetString(ResultData);
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
UnityEngine.Debug.LogError(ex.Message);
|
|
return "";
|
|
}
|
|
}
|
|
}
|
|
}
|