1

I am trying to remove password from a private key file using openssl with node js.

The node js code is:

cmd = exec('/usr/bin/openssl', [ 'rsa', '-in', `${process.cwd()}/privkey.pem`, '-out', `/home/pratik/newPrivateKey.pem` ]); cmd.stdin.write("password", 'utf8'); 

I referred to this SO question. But in my case, on console I just see true as output. No file is created. What I am missing?

1 Answer 1

1

You need to use child_process.spawn, and then forward its stdin and stdout through your Node script using { stdio: "inherit" }.

const child_process = require('child_process'); const openssl = child_process.spawn('openssl', [ 'rsa', '-in', "/Users/my_user/.ssh/my_key", '-out', "/Users/my_user/.ssh/unlocked_key" ], { stdio: "inherit" }); 

Or, for a non-interactive version, where you don't want it to prompt for the password:

const child_process = require('child_process'); const password = "somepassword"; const openssl = child_process.exec('openssl', [ 'rsa', '-in', "/Users/my_user/.ssh/my_key", '-out', "/Users/my_user/.ssh/output_key", '-passin', `pass:${password}` ]); 
Sign up to request clarification or add additional context in comments.

7 Comments

Can you give me a working example of my code? Because when I modified code as cmd = spawn('/usr/bin/openssl', ['rsa', '-in', `${process.cwd()}/privkey.pem`, '-out', `/home/pratik/newPrivateKey.pem`]); cmd.stdout.on('data', (data) => { cmd.stdin.write("password".toString(), 'utf8'); cmd.stdin.end(); }); parent node process freezes. Any suggestions?
Here is something to get you started: see stackoverflow.com/questions/27458502/… It involves setting up a buffer variable, listening to "data" and possibly "end" events, and also forwarding STDIN between Node and the openssl command.
I tried that too. First -i is not option with openssl so interactive can not be interpreted. Second I am getting cmd.stdout as null. Any thoughts?
I was able to get this working in macOS by using { stdio: "inherit" } which forwards stdin/stdout without doing the usual stream API song and dance; edited my answer to include the code, which hopefully will shed some light on your solution.
Was your my_key password protected? If the privatekey isn't password protected, then it works fine. With password protected, you need to use stdin. If it worked for you with password protected private key, can you please provide the entire code?
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.