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.
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.
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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With