在自己签发CA证书和颁发X509证书时,私钥通过下面的方法保存为PEM 相关代码可以已经提交在了 https://github.com/q2g/q2g-helper-pem-nuget/pull/13
public static void SavePem(this X509Certificate2 @this, out string cert, out string privateKey)
cert = string.Empty;
privateKey = string.Empty;
if (@this.HasPrivateKey)
#if NET452
var p = (@this.PrivateKey as RSACryptoServiceProvider).ExportParameters(true);
#else
var p = @this.GetRSAPrivateKey().ExportParameters(true);
#endif
var key = new RsaPrivateCrtKeyParameters(
new Org.BouncyCastle.Math.BigInteger(1, p.Modulus), new Org.BouncyCastle.Math.BigInteger(1, p.Exponent), new Org.BouncyCastle.Math.BigInteger(1, p.D),
new Org.BouncyCastle.Math.BigInteger(1, p.P), new Org.BouncyCastle.Math.BigInteger(1, p.Q), new Org.BouncyCastle.Math.BigInteger(1, p.DP), new Org.BouncyCastle.Math.BigInteger(1, p.DQ),
new Org.BouncyCastle.Math.BigInteger(1, p.InverseQ));
using (var stringWriter = new StringWriter())
var pemWriter = new Org.BouncyCastle.OpenSsl.PemWriter(stringWriter);
pemWriter.WriteObject(key);
privateKey = stringWriter.GetStringBuilder().ToString();
cert = PemCertificateHelper.ExportCertificateToPEM(@this);
catch (Exception ex)
throw new Exception($"Certificate could not be saved. ", ex);
public static void SavePem(this X509Certificate2 @this, string certFile, string privateKeyFile = null)
Directory.CreateDirectory(Path.GetDirectoryName(certFile));
if (!string.IsNullOrEmpty(privateKeyFile) && @this.HasPrivateKey)
Directory.CreateDirectory(Path.GetDirectoryName(privateKeyFile));
#if NET452
var p = (@this.PrivateKey as RSACryptoServiceProvider).ExportParameters(true);
#else
var p = @this.GetRSAPrivateKey().ExportParameters(true);
#endif
var key = new RsaPrivateCrtKeyParameters(
new Org.BouncyCastle.Math.BigInteger(1, p.Modulus), new Org.BouncyCastle.Math.BigInteger(1, p.Exponent), new Org.BouncyCastle.Math.BigInteger(1, p.D),
new Org.BouncyCastle.Math.BigInteger(1, p.P), new Org.BouncyCastle.Math.BigInteger(1, p.Q), new Org.BouncyCastle.Math.BigInteger(1, p.DP), new Org.BouncyCastle.Math.BigInteger(1, p.DQ),
new Org.BouncyCastle.Math.BigInteger(1, p.InverseQ));
using (var sw = new StreamWriter(privateKeyFile))
var pemWriter = new Org.BouncyCastle.OpenSsl.PemWriter(sw);
pemWriter.WriteObject(key);
File.WriteAllText(certFile, PemCertificateHelper.ExportCertificateToPEM(@this));
catch (Exception ex)
throw new Exception($"Certificate could not be saved. cert: {certFile} - key: {privateKeyFile}", ex);
配置文件中代码:
public const string API_URL = "https://api.mch.weixin.qq.com/mmpaymkttransfers/promotion/transfers"; /// <summary>
支付时回调地址作为参数传过去,支付完成后联通沃支付将页面重定向至你给他传的回调地址上,并携带参数,参数同样包括明文密文,需要在我们的回调地址方法中进行验签和业务处理后,再重定向至自己网站的支付成功页面。在验签时,我们将对方传过来的参数按照第二步同样的方式,按参数列表的键的ASKII码排序并用管道符“|”拼接为字符(注意,要将参数列表中的signMsg密文签名去除)。将要生成的签名的参数遍历成一个字符串,需根据参数名的ASKII码排序(一定要排,不然对方验签通不过),并使用管道符“|”拼接起来,例如。
这并不坏。 Java不提供任何函数来编写PEM文件。 你在做什么是正确的方法。 即使KeyTool也是这样做的,BASE64Encoder encoder = new BASE64Encoder(); out.println(X509Factory.BEGIN_CERT); encoder.encodeBuffer(cert.getEncoded(), out); out.println(X50...
首先说明 MS并不建议私钥加密,而且.net 于安全的考虑,RSACryptoServiceProvider类解密时只有同时拥有公钥和私钥才可以,原因是公钥是公开的,会被多人持有,这样的数据传输是不安全的。但是架不住有BouncyCastle这个第三方组件,也是可以实现的。只不过在.net core 2.2 下,没有了 RSACryptoServiceProvider,只好改用 Syst...