/// <summary> /// 加密请求的信息进行RSA解密 /// </summary> /// <param name="context"></param> /// <returns></returns> private ResponseModel GetRequestRsaData(AppLyInfo applyInfo, string sign) { //请求头信息验证未通过 var resultobj = new ResponseModel(ResponseCode.BadRequest, "RSA解密失败"); string decrypt_timestamp = ""; try { //校验签名,(RSA加密请求时间戳生成) //签名运算,ANS密钥对 RSAUtilBase rSAUtil = new RsaPkcs8Util(Encoding.UTF8, applyInfo.公钥, applyInfo.私钥); //lock (lockobj) //{ // RSAEncryptionPaddingMode.Pkcs1 decrypt_timestamp = rSAUtil.Decrypt(sign, RSAEncryptionPadding.Pkcs1); //} //请求验签信息解密成功 resultobj.code = ResponseCode.Success.ToInt32(); resultobj.data = decrypt_timestamp; } catch (Exception ex) { Logger.Instance.Error("RSA解密发生异常", ex); resultobj.msg = "请求参数签名不合法"; } return(resultobj); }
public static string Decrypt(string str, string publicKey, string privateKey, int keySize) { using (var rsa = new RsaPkcs8Util(Encoding.UTF8, publicKey, privateKey, keySize)) { return(rsa.Decrypt(str, RSAEncryptionPadding.Pkcs1)); } }
/// <summary> /// JavaRsa私钥解密 /// </summary> /// <param name="text"></param> /// <param name="privateKey"></param> /// <param name="signFormat"></param> /// <returns></returns> public static string JavaRsaDecrypt(string text, string privateKey, RsaSignFormat signFormat = RsaSignFormat.Base64) { var rsaProvider = new RsaPkcs8Util(Encoding.UTF8, string.Empty, privateKey); if (signFormat == RsaSignFormat.Hex) // => byte => base64 { text = Convert.ToBase64String(FromHexString(text)); } return(rsaProvider.Decrypt(text, RSAEncryptionPadding.Pkcs1)); }
/// <summary> /// RSA 加密,解密,签名和验签 /// </summary> public static void RSASignFunc(string privateKey, string publicKey, EnumUserType enumUserType) { Encoding.RegisterProvider(CodePagesEncodingProvider.Instance); switch (enumUserType) { case EnumUserType.xml: RsaXmlUtil rsaXmlUtil = new RsaXmlUtil(Encoding.UTF8, publicKey, privateKey); var encrypt = rsaXmlUtil.Encrypt("123456789", RSAEncryptionPadding.Pkcs1); var encryptInput = rsaXmlUtil.Decrypt(encrypt, RSAEncryptionPadding.Pkcs1); Console.Write("XML加解密是否成功:"); Console.WriteLine(encryptInput is "123456789"); var sign = rsaXmlUtil.SignData("987654321", HashAlgorithmName.SHA256, RSASignaturePadding.Pkcs1); var verifyData = rsaXmlUtil.VerifyData("987654321", sign, HashAlgorithmName.SHA256, RSASignaturePadding.Pkcs1); Console.Write("XML签名是否成功"); Console.WriteLine(verifyData); break; case EnumUserType.pkcs1: RsaPkcs1Util rsaPkcs1Util = new RsaPkcs1Util(Encoding.UTF8, publicKey, privateKey); var encrypt2 = rsaPkcs1Util.Encrypt("123456789", RSAEncryptionPadding.Pkcs1); var encryptInput2 = rsaPkcs1Util.Decrypt(encrypt2, RSAEncryptionPadding.Pkcs1); Console.Write("PKCS1加解密是否成功:"); Console.WriteLine(encryptInput2 is "123456789"); var sign2 = rsaPkcs1Util.SignData("987654321", HashAlgorithmName.SHA256, RSASignaturePadding.Pkcs1); var verifyData2 = rsaPkcs1Util.VerifyData("987654321", sign2, HashAlgorithmName.SHA256, RSASignaturePadding.Pkcs1); Console.Write("PKCS1签名是否成功"); Console.WriteLine(verifyData2); break; default: RsaPkcs8Util rsaPkcs8Util = new RsaPkcs8Util(Encoding.UTF8, publicKey, privateKey); var encrypt3 = rsaPkcs8Util.Encrypt("123456789", RSAEncryptionPadding.Pkcs1); var encryptInput3 = rsaPkcs8Util.Decrypt(encrypt3, RSAEncryptionPadding.Pkcs1); Console.Write("PKCS8加解密是否成功:"); Console.WriteLine(encryptInput3 is "123456789"); var sign3 = rsaPkcs8Util.SignData("987654321", HashAlgorithmName.SHA256, RSASignaturePadding.Pkcs1); var verifyData3 = rsaPkcs8Util.VerifyData("987654321", sign3, HashAlgorithmName.SHA256, RSASignaturePadding.Pkcs1); Console.Write("PKCS8签名是否成功"); Console.WriteLine(verifyData3); break; } }
private string DecryptCredentials(SignInRequest sign) => _rsa.Decrypt(sign.credentials, RSAEncryptionPadding.Pkcs1);