Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to convert JSON private key into PKCS12 format?

Tags:

json

php

I am working with Google Cloud Storage services in PHP, I have a APPLICATION_GOOGLE_CLIENT_KEY_FILE in JSON format.

My question is can we convert JSON private key into PKCS12 format? If yes then please provide me some way, as I am unable to do so.

like image 624
Amrinder Singh Avatar asked Nov 06 '15 07:11

Amrinder Singh


People also ask

What is PKCS 12 keystore?

In cryptography, PKCS #12 defines an archive file format for storing many cryptography objects as a single file. It is commonly used to bundle a private key with its X. 509 certificate or to bundle all the members of a chain of trust. PKCS #12. Filename extension.


2 Answers

Here's what I pieced together for a similar situation from the openssl docs. It isn't based on any deep understanding of keys, but it did work for me for the libraries I was using with Node. In the object from the JSON file, there is a member called 'private_key'. Copy its value into a new file, say 'jsonkey.key', and replace all the occurrences '\n' with actual newlines. Then run the command:

openssl rsa -in jsonkey.key | openssl pkcs12 -password pass:notasecret -export -nocerts -out p12key.p12

Of course, change p12key.p12 to whatever you want your pkcs12 file to be called.

like image 193
Holy Joe Avatar answered Oct 14 '22 07:10

Holy Joe


This builds on Holy Joe's answer using a quick and dirty shell script. You'll need node to be installed.

#!/bin/sh
value=`cat ./json.json`

var=`node -p -e 'JSON.parse(process.argv[1]).private_key' "$value"`

dest=jsonkey.key
echo "$var" > "$dest"

openssl rsa -in jsonkey.key | openssl pkcs12 -password pass:notasecret -export -nocerts -out key.p12
like image 2
Tony Destro Avatar answered Oct 14 '22 05:10

Tony Destro