{"record":{"id":"3e9735fd91432a5a","repo":"JeffreySu/WeiXinMPSDK","slug":"rsa","errorCode":null,"errorMessage":"证书中未包含 RSA 公钥。","messagePattern":"证书中未包含 RSA 公钥。","errorType":"exception","errorClass":"CryptographicException","httpStatus":null,"severity":"error","filePath":"src/Senparc.Weixin.TenPay/Senparc.Weixin.TenPayV3/Helpers/SecurityHelper.cs","lineNumber":156,"sourceCode":"                    var buff = rsa.Encrypt(Encoding.UTF8.GetBytes(text), RSAEncryptionPadding.OaepSHA1);\n                    return Convert.ToBase64String(buff);\n                }\n            }\n            #endregion\n\n            if (encryptionType == CertType.SM)\n            {\n                ECPublicKeyParameters eCPublicKeyParameters = SMPemHelper.LoadPublicKeyToParameters(Encoding.UTF8.GetBytes(publicKey));\n                return GmHelper.Sm2Encrypt(eCPublicKeyParameters, text);\n            }\n            else\n            {\n                using (var x509 = new X509Certificate2(Encoding.UTF8.GetBytes(publicKey)))\n                using (var rsa = x509.GetRSAPublicKey())\n                {\n                    if (rsa == null)\n                    {\n                        throw new CryptographicException(\"证书中未包含 RSA 公钥。\");\n                    }\n\n                    var buff = rsa.Encrypt(Encoding.UTF8.GetBytes(text), RSAEncryptionPadding.OaepSHA1);\n                    return Convert.ToBase64String(buff);\n                }\n            }\n        }\n\n        /// <summary>\n        /// 字段加密\n        /// </summary>\n        /// <param name=\"request\"></param>\n        /// <param name=\"publicKey\"></param>\n        /// <param name=\"encryptionType\"></param>\n        /// <param name=\"isWeixinPubKey\">是否是微信支付公钥</param>\n        public static void FieldEncrypt(object request, string publicKey, CertType encryptionType, bool isWeixinPubKey = false)\n        {\n            if (request == null)","sourceCodeStart":138,"sourceCodeEnd":174,"githubUrl":"https://github.com/JeffreySu/WeiXinMPSDK/blob/be573f6f94bdbf718dd5f6cdecb137fbc7ff651e/src/Senparc.Weixin.TenPay/Senparc.Weixin.TenPayV3/Helpers/SecurityHelper.cs#L138-L174","documentation":"SecurityHelper.Encrypt encrypts text with the RSA public key taken from the supplied certificate (publicKey string). If the X509Certificate2 built from it has no RSA public key (GetRSAPublicKey() returns null, e.g. it's an EC/SM2 cert), CryptographicException \"证书中未包含 RSA 公钥。\" is thrown.","triggerScenarios":"Calling Encrypt (e.g. for WeChat Pay field encryption) with a publicKey string containing a certificate whose key algorithm is not RSA (EC/SM2), or an invalid/empty certificate blob that parses without an RSA key.","commonSituations":"WeChat Pay migrated from platform certificates (RSA) to public-key mode; developers pass an SM2 public key or public-key ID string where a base64 X509 RSA certificate is expected.","solutions":["Pass the base64 platform certificate content that contains an RSA public key (GetRSAPublicKey() != null).","If using public-key mode, ensure the helper/API variant supports the WeChat public key format instead of an X509 cert.","Verify the cert: new X509Certificate2(bytes).GetRSAPublicKey() != null before calling Encrypt."],"exampleFix":"// before\nvar encrypted = SecurityHelper.Encrypt(sm2PublicKeyPem, \"13800138000\");\n// after\nvar encrypted = SecurityHelper.Encrypt(Convert.ToBase64String(rsaCertBytes), \"13800138000\"); // RSA platform certificate","handlingStrategy":"validation","validationCode":"bool HasRsaPublicKey(string certBase64) {\n    using var x509 = new X509Certificate2(Convert.FromBase64String(certBase64));\n    return x509.GetRSAPublicKey() != null;\n}","typeGuard":null,"tryCatchPattern":"try { var s = SecurityHelper.Encrypt(publicKey, text); }\ncatch (CryptographicException ex) { logger.Error(ex, \"证书无 RSA 公钥\"); }","preventionTips":["Only pass RSA platform certificates to SecurityHelper.Encrypt","Distinguish WeChat public-key mode from platform-certificate mode in config","Check key algorithm at cert load time"],"tags":["cryptography","rsa","x509","wechat-pay"],"backgroundTag":"invalid-argument-value","analyzedSha":"be573f6f94bdbf718dd5f6cdecb137fbc7ff651e","analyzedAt":"2026-09-12T10:01:50.733Z","contentChangedAt":"2026-09-12T10:01:50.733Z","schemaVersion":2},"datasetVersion":"2026-09-16T04:17:20.429Z"}