且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

用Java生成的RSA公钥在php中无效

更新时间:2022-03-25 23:12:49

密钥显然需要采用SubjectPublicKeyInfo格式,有时也称为"X.509"格式-但与X.509证书不同-只是增加了混乱的海洋.我不是从文档获取此信息,而是从用户在下面的评论.

The key evidently needs to be in SubjectPublicKeyInfo format, sometimes referred to as "X.509" format -- but not the same thing as an X.509 certificate -- just to add to the general sea of confusion. I got this info not from the documentation but from the user comments below.

幸运的是,生成此代码所需的行数甚至更少,因为从您的代码改编而成的这段代码片段说明了这一点:

Fortunately this takes even fewer lines of Java code to produce, as this little code fragment adapted from your code illustrates:

    PublicKey pub = keyPair.getPublic();
    byte[] pubBytes = pub.getEncoded();
    PemObject pemObject = new PemObject("PUBLIC KEY", pubBytes);
    StringWriter stringWriter = new StringWriter();
    PemWriter pemWriter = new PemWriter(stringWriter);
    pemWriter.writeObject(pemObject);
    pemWriter.close();
    System.out.println(stringWriter.toString());