Search code examples
node.jscryptojsnode-java

Reading public and private key from stored files in node js


I wanted to encrypt and decrypt a message in node using public and private keys stored in my system. I was using the following java code to read the file and use the keys.

Java Code:

byte[]  keyBytes = Files.readAllBytes(new File(publicKeyFileName).toPath());
X509EncodedKeySpec spec = new X509EncodedKeySpec(keyBytes);
KeyFactory kf = KeyFactory.getInstance("RSA");
publicKey=kf.generatePublic(spec);

I am able to use the above java method without any issues to read the public key from file. However, I want to achieve similar functionality in node. I have tried using crypto for achieving the same but it gives me error while passing the key to publicEncrypt method.

Node:

var encryptStringWithRsaPublicKey = function(toEncrypt, relativeOrAbsolutePathToPublicKey) {
    var absolutePath = path.resolve(relativeOrAbsolutePathToPublicKey);
    var publicKey = fs.read(absolutepath, "utf-8");
    console.log(publicKey);
    var buffer = Buffer.from(toEncrypt);
    var encrypted = crypto.publicEncrypt(publicKey, buffer);
    return encrypted.toString("base64");
};

Error

internal/crypto/cipher.js:43
    return method(toBuf(key), buffer, padding, passphrase);
           ^

Error: error:0906D06C:PEM routines:PEM_read_bio:no start line

Please help. Thanks


Solution

  • Your problem is located in the file format you are actually using with Java. You probably save the private and the public in encoded ("byte array") to a file and rebuild the keys e.g. with X509EncodedKeySpec.

    This format is not compatible to Node.JS and you have 3 ways to solve it:

    a) you write the keys in Java with re neccessary format for usage in Node.JS

    b) you write a converter in Node.JS to get the correct format

    c) you convert the files with a tool like OPENSSL.

    Here I show you the "c-way" as you are handling just one keypair and probably don't need a programatically solution.

    Let's say you have two files with the private key ("rsa_privatekey_2048.der") and the public key ("rsa_publickey_2048.der").

    In OPENSSL you are using the command line with

    openssl rsa -inform der -in rsa_privatekey_2048.der -outform pem -out rsa_privatekey_2048.pem
    openssl rsa -inform der -pubin -in rsa_publickey_2048.der -outform pem -RSAPublicKey_out -out rsa_publickey_2048.pem
    

    to convert the files to their PEM-encoded formats.

    Below you can find the two sample files I created.

    rsa_privatekey_2048.pem:

    -----BEGIN RSA PRIVATE KEY-----
    MIIEpQIBAAKCAQEAm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VLiCBqsCgYEAgl0xtSY/EP6fZJQ2ek+L
    4DqNN6WUeCRgXxonbA1mR91AALyOVNVyIreJuYHlb7ccvJ9BZexH9dRrMQ3N4ibS
    /6cecAzD1S9XxF6oBwQHdbH6ewC9VFFcQdsxKW5gxWrwRQJUp1fbUoOVyb1gDa5/
    vZg7VvoZ0rh74Mu/cAzdgPUCgYEAiNANdwt29AKyUyefykpLGCczGL8aPP88l6z7
    R38uAX1Ygg/pdJoUrnHo+FkIbHkcXMRfHFF3j7NoMWynwSLg50OUiPX80SiLN5qm
    iytDzskZjsEL2gq6IF1NHRabTfWlmrVDjR9mQhTabq+NtIDwlPOqs9100nrlbFIy
    6uU0z18CgYEAkDxQg5UjOzbYIighp/e0LkfKlwUE/cMtISUq1A6Yio9AKZYdBV8p
    dd4osUW0gZvDflXlQaBIaNlOwl035lyEZtyuKmFh1oSmoir/TTMbIk0avSgKCLGg
    fnhabaQRHl4RdXWcEsioRv3aZUsGkb46Y8xODyAUPRHBPhBsd71gnZ8=
    -----END RSA PRIVATE KEY-----
    

    rsa_publickey_2048.pem:

    -----BEGIN RSA PUBLIC KEY-----
    MIIBCgKCAQEAmbeKgSAwVe0nZ84XlbDhMkUDjx1C0duA16MkzHTg1uh9SouOKK0e
    3gPtTJ9LssaHlXSYhjpMDMWGO6ujd85XRosI2u9eSMNRYY25AQuBriSTVdi9BHqW
    AuWuo6VuvTrkgWTL69vNWvLXTOkTiIyrgnhiavjNvm4UVy2AcO2Y3ER+dKgJpQAY
    lEP1jvuQuf6dfNdSBoN0DZbxZXYbQqoA9R/u0GZHCXY+r8A54RejG34pnnuHkoyR
    OZz5H9LbKGOiaETryornQ1TRvB/p9tgIoCJFI71WsKsqeWQPG3Ymg/FoEWXNY0yo
    pZEjpkZa3tU+hrOmAFIRg+/bedKfjYFi/QIDAQAB
    -----END RSA PUBLIC KEY-----