如何在程序中加载平台证书私钥

更新时间:2024.11.20

推荐使用微信支付提供的SDK。你也可以查看下列编程语言的示例代码。

JAVA:

1代码解释代码改写/**
2  * 获取私钥。
3  *
4  * @param filename 私钥文件路径  (required)
5  * @return 私钥对象
6  */
7public static PrivateKey getPrivateKey(String filename) throws IOException {
8  String content = new String(Files.readAllBytes(Paths.get(filename)), "utf-8");
9  try {
10    String privateKey = content.replace("-----BEGIN PRIVATE KEY-----", "")
11        .replace("-----END PRIVATE KEY-----", "")
12        .replaceAll("\\s+", "");
13    KeyFactory kf = KeyFactory.getInstance("RSA");
14    return kf.generatePrivate(
15        new PKCS8EncodedKeySpec(Base64.getDecoder().decode(privateKey)));
16  } catch (NoSuchAlgorithmException e) {
17    throw new RuntimeException("当前Java环境不支持RSA", e);
18  } catch (InvalidKeySpecException e) {
19    throw new RuntimeException("无效的密钥格式");
20  }
21}

PHP:

1代码解释代码改写/**
2* Read private key from file
3*
4* @param string    $filepath     PEM encoded private key file path
5* 
6* @return resource|bool     Private key resource identifier on success, or FALSE on error
7*/
8public static function getPrivateKey($filepath) {
9    return openssl_get_privatekey(file_get_contents($filepath));
10}

csharp:

1protected string sign(string message)
2{
3    // 需去除私钥文件中的-----BEGIN/END PRIVATE KEY-----
4    string privateKey = "MIIEvgIBADANBgkqhkiG...30HBe+GD1tntZgf6I1Y0ZpHZ";
5    byte[] keyData = Convert.FromBase64String(privateKey);
6    using (CngKey cngKey = CngKey.Import(keyData, CngKeyBlobFormat.Pkcs8PrivateBlob))
7    using (RSACng rsa = new RSACng(cngKey))
8    {
9        byte[] data = System.Text.Encoding.UTF8.GetBytes(message);
10        return Convert.ToBase64String(rsa.SignData(data, HashAlgorithmName.SHA256, RSASignaturePadding.Pkcs1));
11    }
12}

GO:

1代码解释代码改写package main
2import (
3  "crypto/x509"
4  "encoding/pem"
5  "fmt"
6  "log"
7)
8func main() {
9  var pemData = []byte(`
10-----BEGIN PRIVATE KEY-----
11MIIEvQIBADANBgkqhkiG9w0BA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Lbm
28TV/46QCBzQKBgDd7ZQDXPT+epHmT4HJD9sVvW9dZVPsWmckP/MC0xqdcE1QGEjjf
29mablPcfjLma1J1m//Ep1vniHkkBgNJkpBgDzbHoSWAN5335ccEug2d4yFIwq19rj
30sY9efUaVOirSV/kiY3KSotRWGeIDC+YNHtpTx58VNZes0gvutH2Iz9ahAoGAUcoW
31b/xEMv0dURxF8C+lfxtSlxlBhymsg3AYWV+Tn7mdJSS4Nhv592vI/A/Mn37zh+BC
32P8lpX3lq2HzPEPoKF7b4Q22ggdvlSQT6SMT8mTtfbyPSyRAQdWZQZnyVkTD3TvPD
33g7CKD1As8KFiFuXPAD2KgI9nVz6XhNBpjZ8rbyECgYEAsOrm1hbNZbvlNhnuUjw5
34DTgTuJ3B0j1aK/7C2EQWR+mIG2q5TKDC6xNdszV0gK1/TbJk4RNgQo0JLkuZ2Xk2
35Q8KhaNe+X8SYP9CFKIsXuhGrYI5ICjipov5oJqjESV4wle575eWwdPgF1ICabpIq
36dnX2MxS9tkk830uXxPrXpRA=
37-----END PRIVATE KEY-----`)
38  block, rest := pem.Decode(pemData)
39  if block == nil || block.Type != "PRIVATE KEY" {
40    log.Fatal("failed to decode PEM block containing public key")
41  }
42  pri, err := x509.ParsePKCS8PrivateKey(block.Bytes)
43  if err != nil {
44    log.Fatal(err)
45  }
46  fmt.Printf("Got a %T, with remaining data: %q", pri, rest)
47}

 

 

更多技术问题
技术咨询
反馈
咨询
目录
置顶