public static string RSAEncrypt(string content, string publicKeyPem, string charset, bool keyFromFile) { if (keyFromFile) { ArgumentValidator.CheckArgument(!String.IsNullOrEmpty(publicKeyPem), "公钥文件路径不可为空。"); publicKeyPem = File.ReadAllText(publicKeyPem); } return(AsymmetricManager.GetByName("RSA").Encrypt(content, charset, publicKeyPem)); }
public static string RSADecrypt(string content, string privateKeyPem, string charset, string signType, bool keyFromFile) { if (keyFromFile) { ArgumentValidator.CheckArgument(!String.IsNullOrEmpty(privateKeyPem), "私钥文件路径不可为空。"); privateKeyPem = LoadPrivateKeyFromRSACertFile(privateKeyPem); } return(AsymmetricManager.GetByName(signType).Decrypt(content, charset, privateKeyPem)); }
/// <summary> /// 验证指定内容的签名,支持RSA、RSA2、SM2三种算法,推荐替换为相同功能的方法VerifyContent /// </summary> /// <param name="content">待验签的内容</param> /// <param name="sign">签名字符串</param> /// <param name="publicKey">支付宝公钥</param> /// <param name="charset">字符集编码</param> /// <param name="signType">签名算法类型,RSA2或RSA、SM2</param> /// <param name="keyFromFile">是否从文件加载支付宝公钥内容。 /// 如果该参数为true,则publicKey为公钥文件路径; /// 如果该参数为false,则publicKey为公钥内容 /// </param> /// <returns>true:验证成功;false:验证失败</returns> public static bool RSACheckContent(string content, string sign, string publicKey, string charset, string signType, bool keyFromFile) { if (keyFromFile) { ArgumentValidator.CheckArgument(!String.IsNullOrEmpty(publicKey), "公钥文件路径不可为空。"); publicKey = File.ReadAllText(publicKey); } return(AsymmetricManager.GetByName(signType).Verify(content, charset, publicKey, sign)); }