klboke / klblog-talk

博客评论仓库
2 stars 0 forks source link

AES,RSA公私钥加解密 | KL博客 #104

Open klboke opened 6 years ago

klboke commented 6 years ago

http://www.kailing.pub/article/index/arcid/106.html

KL博客,kl博客,java,php学习心得技术分享,一个爱折腾的程序猿

klboke commented 3 years ago

输出标准格式pem文件项目:https://github.com/bcgit/bc-java

klboke commented 3 years ago
    @Test
    void generateRsaKey() throws Exception {
        KeyPair keyPair = Keys.keyPairFor(SignatureAlgorithm.RS256); //or RS384, RS512, PS256, PS384, PS512, ES256, ES384, ES512
        String publicKey = Encoders.BASE64.encode(keyPair.getPublic().getEncoded());
        String privateKey = Encoders.BASE64.encode(keyPair.getPrivate().getEncoded());

        System.err.println("queuing.app.publicKey=" + publicKey);
        System.err.println("queuing.app.privateKey=" + privateKey);

        String filename  = "./rsa_public_key.pem";
        PemWriter pemWriter = new PemWriter(new OutputStreamWriter(new FileOutputStream(filename)));

        PemObject pemObject = new PemObject("RSA PUBLIC KEY", keyPair.getPublic().getEncoded());
        pemWriter.writeObject(pemObject);
        pemWriter.flush();
        Assert.assertNotNull(pemObject);
    }