我使用Telegram Bot API webhook支持与bouncycastle生成的自签名证书有问题

时间:2016-06-17 20:35:20

标签: java x509certificate bouncycastle telegram telegram-bot

这是我生成证书的代码。

KeyPairGenerator gen = KeyPairGenerator.getInstance("RSA");
gen.initialize(2048);
pair = gen.generateKeyPair();
privateKey = pair.getPrivate();
publicKey = pair.getPublic();
SubjectPublicKeyInfo publicKeyInfo = SubjectPublicKeyInfo.getInstance(publicKey.getEncoded());
X509v3CertificateBuilder builder = new X509v3CertificateBuilder(subjectDN, new BigInteger(serialNumber + ""),
                                                                        startDate, endDate, subjectDN,
                                                                        publicKeyInfo);
ContentSigner signer = new JcaContentSignerBuilder("SHA256withRSA").setProvider("BC").build(privateKey);
cert = new JcaX509CertificateConverter().getCertificate(builder.build(signer));

当我上传并尝试通过setWebhook API方法设置webhook时,它会失败。

{"ok":false,"error_code":400,"description":"Bad webhook: Failed to set custom cert file"}

有人能告诉我我错过了什么吗?
编辑。这是PEM格式的公钥:

-----BEGIN PUBLIC KEY-----
MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAqA2BQC0cOGVns9USxRwk
2PQHtk3lfDqEdhmQjiW6U0RA102IbLx2ALizkegO9TwjFszynjRuq6KlQT4ctvEy
XyKpb9tMF5tRg2haDDEfyCfpKxuwQfjzYLLp+RqxLMsAngMaE3UwM6lyo9jYUHxD
sfQgUWkg6vCJ9b52/IAFYsuq14//J1ZrHRlYBnGImOroMWwLBmMZVmTxeB/QyTDc
gbj/uBbOKTckk7jchAxtO/PRVZ5nW2PWxAeE0FAtwhHHXTfwINqkcEmk21/jlpvT
GTHkkoxEl+BptvIKqrgSdvoTbHSVpn9U6ZJTV8ZVC46xcjiD/eFxr+dl3oZAjG6N
5wIDAQAB
-----END PUBLIC KEY-----

我使用多部分将其发送到服务器。

2 个答案:

答案 0 :(得分:1)

我遇到了同样的问题。公钥对于Telegram来说已经足够了。确保将证书附加为二进制文件并提供文件名。

Public Sub TestMe()

    Dim sourceRange As Range
    Dim resultArray As Variant

    Set sourceRange = Worksheets(1).Range("A1")
    resultArray = CharacterArray(sourceRange.Value2)

    Dim cnt         As Long
    Dim result      As String
    Dim spaces      As String


    For cnt = (LBound(resultArray) + 1) To UBound(resultArray)
        If resultArray(cnt) = resultArray(cnt - 1) Then
            spaces = spaces & " "
            result = result & spaces & resultArray(cnt) & vbCrLf
        Else
            spaces = vbNullString
            result = result & resultArray(cnt) & vbCrLf
        End If
    Next cnt

    sourceRange.Offset(0, 1) = result

    Debug.Print result

End Sub

答案 1 :(得分:0)

webhook的API说:

  

... pem文件应该只包含公钥(包括BEGIN和END部分)

但是,他们提供的所有(!)教程都会生成证书,而不是空白公钥。

尝试将整个证书解析为PEM并将其提供给webhook:

    PrintWriter writer = new PrintWriter("cert.pem", "UTF-8");
    JcaMiscPEMGenerator pemGen = new JcaMiscPEMGenerator(cert);

    JcaPEMWriter pemWriter = new JcaPEMWriter(writer);
    pemWriter.writeObject(pemGen);
    pemWriter.close();