//using Org.BouncyCastle.Crypto; //using Org.BouncyCastle.Crypto.Parameters; //using Org.BouncyCastle.Security; //using System; //using System.Collections.Generic; //using System.Linq; //using System.Security.Cryptography; //using System.Text; //using System.Threading.Tasks; ///// ///// Rsa非对称加密工具类 ///// //public class RsaBCHelper //{ // /// // /// 签名 // /// // /// 待签名字符串 // /// 私钥 // /// 签名后字符串 // public static string Sign(string content, string privateKey) // { // AsymmetricKeyParameter priKey = RsaKeyHelper.loadPrivateKeyPk1(privateKey); // ISigner sig = SignerUtilities.GetSigner("SHA256withRSA"); //其他算法 如SHA1withRSA // sig.Init(true, priKey); // var bytes = Encoding.Default.GetBytes(content); // sig.BlockUpdate(bytes, 0, bytes.Length); // byte[] signature = sig.GenerateSignature(); // var signedString = Convert.ToBase64String(signature); // return signedString; // } // /// // /// 验签 // /// // /// 待验签字符串 // /// 签名 // /// 公钥 // /// true(通过),false(不通过) // public static bool Verify(string content, string signedString, string publicKey) // { // AsymmetricKeyParameter pubKey = RsaKeyHelper.loadPublicKey(publicKey); // ISigner signer = SignerUtilities.GetSigner("SHA256withRSA"); //其他算法 如SHA1withRSA // signer.Init(false, pubKey); // var expectedSig = Convert.FromBase64String(signedString); // var msgBytes = Encoding.Default.GetBytes(content); // signer.BlockUpdate(msgBytes, 0, msgBytes.Length); // return signer.VerifySignature(expectedSig); // } // /// // /// 公钥加密 // /// // /// 需要加密的字符串 // /// 公钥 // /// 明文 // public static string Encrypt(string resData, string publicKey) // { // try // { // AsymmetricKeyParameter pubKey = RsaKeyHelper.loadPublicKey(publicKey); // IBufferedCipher cipher = CipherUtilities.GetCipher("RSA/ECB/PKCS1Padding"); // cipher.Init(true, pubKey);//true表示加密 // var data = Encoding.Default.GetBytes(resData.Trim()); // byte[] encryptData = cipher.DoFinal(data); // return Convert.ToBase64String(encryptData); // } // catch (Exception) // { // throw new Exception("加密失败"); // } // } // /// // /// 私钥解密 // /// // /// 加密字符串 // /// 私钥 // /// 编码格式 // /// 明文 // public static string Decrypt(string resData, string privateKey) // { // try // { // AsymmetricKeyParameter priKey = RsaKeyHelper.loadPrivateKeyPk1(privateKey); // IBufferedCipher cipher = CipherUtilities.GetCipher("RSA/ECB/PKCS1Padding"); // cipher.Init(false, priKey);//false表示解密 // var encryptData = Convert.FromBase64String(resData); // var decryptData = cipher.DoFinal(encryptData); // return Encoding.Default.GetString(decryptData); // } // catch (Exception) // { // throw new Exception("解密失败"); // } // } // /// // /// 私钥加密 // /// // /// 需要加密的字符串 // /// 私钥 // /// 明文 // public static string EncryptByPrivateKey(string resData, string privateKey) // { // try // { // AsymmetricKeyParameter priKey = RsaKeyHelper.loadPrivateKeyPk1(privateKey); // IBufferedCipher cipher = CipherUtilities.GetCipher("RSA/ECB/PKCS1Padding"); // cipher.Init(true, priKey);//true表示加密 // var data = Encoding.Default.GetBytes(resData.Trim()); // byte[] encryptData = cipher.DoFinal(data); // return Convert.ToBase64String(encryptData); // } // catch (Exception) // { // throw new Exception("加密失败"); // } // } // /// // /// 公钥解密 // /// // /// 加密字符串 // /// 公钥 // /// 明文 // public static string DecryptByPublicKey(string resData, string publicKey) // { // try // { // AsymmetricKeyParameter pubKey = RsaKeyHelper.loadPublicKey(publicKey); // IBufferedCipher cipher = CipherUtilities.GetCipher("RSA/ECB/PKCS1Padding"); // cipher.Init(false, pubKey);//false表示解密 // var encryptData = Convert.FromBase64String(resData); // var decryptData = cipher.DoFinal(encryptData); // return Encoding.Default.GetString(decryptData); // } // catch (Exception) // { // throw new Exception("解密失败"); // } // } //}