简体   繁体   English

在节点中读取Java生成的公钥

[英]Read Java generated public key in node

I have the bytes of a public key generated in Java with something like this: 我有一个用Java生成的公钥的字节,如下所示:

package test;

import java.io.IOException;
import java.security.KeyPair;
import java.security.KeyPairGenerator;
import java.security.NoSuchAlgorithmException;
import java.security.PublicKey;
import java.util.Base64;

public class Test {

    public static void main(String[] args) throws IOException, NoSuchAlgorithmException {
         KeyPairGenerator generator = KeyPairGenerator.getInstance("RSA");
         //generator.initialize(dsabits, sr);
         KeyPair keyPair = generator.generateKeyPair();
         PublicKey publicKey = keyPair.getPublic();

         byte[] encodedBytes = Base64.getEncoder().encode(publicKey.getEncoded());
         System.out.println("decodedBytes " + new String(encodedBytes));
    }

}

=> decodedBytes MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCJQz9HgPKzGi+jT5uZ7CEU8mQyNZXm1IpPBIaMGZ2IW54iv+ptVAI8lG9mRNPTyZf07GZNnDSp5RcAn19lUh6eBxZ6R+Yg78rjw4UFfuGQPe8pKrAsy0ESOxLJrh2iQpa4H1DQ86Jq1kSUGFBMCpk68RyT+yQGwBCO8yomqp+7HQIDAQAB => decodedBytes MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCJQz9HgPKzGi + jT5uZ7CEU8mQyNZXm1IpPBIaMGZ2IW54iv + ptVAI8lG9mRNPTyZf07GZNnDSp5RcAn19lUh6eBxZ6R + Yg78rjw4UFfuGQPe8pKrAsy0ESOxLJrh2iQpa4H1DQ86Jq1kSUGFBMCpk68RyT + yQGwBCO8yomqp + 7HQIDAQAB

Now I need to encrypt a string in node using this publick key. 现在我需要使用此publick键加密节点中的字符串。 I tried with 我试过了

var crypto = require("crypto");
var pubKeyB64 = 'MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCJQz9HgPKzGi+jT5uZ7CEU8mQyNZXm1IpPBIaMGZ2IW54iv+ptVAI8lG9mRNPTyZf07GZNnDSp5RcAn19lUh6eBxZ6R+Yg78rjw4UFfuGQPe8pKrAsy0ESOxLJrh2iQpa4H1DQ86Jq1kSUGFBMCpk68RyT+yQGwBCO8yomqp+7HQIDAQAB';

var pubKey = Buffer.from(pubKeyB64, 'base64');

var toEncrypt = 'ciao';
var buffer = new Buffer(toEncrypt);
var encrypted = crypto.publicEncrypt(pubKey, buffer);
console.log(encrypted.toString("base64"));

but unfortunately I get 但不幸的是我得到了

Error: error:0906D06C:PEM routines:PEM_read_bio:no start line 错误:错误:0906D06C:PEM例程:PEM_read_bio:无起始行

I don't have control over the public key generation format, unfortunately I have to use what I got there. 我无法控制公钥生成格式,不幸的是我必须使用我在那里的内容。 Is there a way to use this key in node to encrypt a string somehow? 有没有办法在节点中使用此密钥以某种方式加密字符串?

Assuming you mean https://nodejs.org/api/crypto.html#crypto_crypto_publicencrypt_key_buffer it says it wants as a string 假设你的意思是https://nodejs.org/api/crypto.html#crypto_crypto_publicencrypt_key_buffer它说它想要一个字符串

A PEM encoded public or private key. PEM编码的公钥或私钥。

To create the PEM encoding, instead of converting your base64 to binary, break it into chunks of 64 chars except the last each followed by a newline, put a line -----BEGIN PUBLIC KEY----- plus newline at the beginning and a line -----END PUBLIC KEY----- plus newline at the end. 要创建PEM编码,而不是将base64转换为二进制,将其分成64个字符的块,除了最后一个后面跟一个换行符,放一行-----BEGIN PUBLIC KEY-----加上换行符开头和一行-----END PUBLIC KEY-----加上最后的换行符。

I could code this on the Java side but you say you can't change that, and I don't know enough js to code the nodejs side, so setting CW for anyone else to. 我可以在Java端对此进行编码,但是你说你不能改变它,我不知道有足够的js来编码nodejs,所以为其他人设置CW。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM