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 { /// /// 公钥解密 /// /// 明文 /// 公钥 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 ""; } } } }