Extract .key and .crt files from JKS file

JKS file is a Java keystore. Using the Java keytool program, run the following commands

Export the .der file

keytool -export -alias sample -file sample.der -keystore my.jks

Convert the .der file to unencrypted PEM (crt file)

openssl x509 -inform der -in sample.der -out sample.crt

Export the .p12 file

keytool -importkeystore -srckeystore my.jks -destkeystore keystore.p12 -deststoretype PKCS12

Convert the .p12 file to unencrypted PEM (key file)

openssl pkcs12 -in keystore.p12 -nodes -nocerts -out server.key

 

More info here: http://www.gtopia.org/blog/2010/02/der-vs-crt-vs-cer-vs-pem-certificates/

Linux Tip: Removing blank lines from a text file

Method 1: Using grep

$ grep -v '^$' infile.txt > outfile.txt

Method 2: Using sed

$ sed '/^$/d' infile.txt > outfile.txt

To remove blank lines from multiple files a script like the one below can be used

#!/bin/sh
files="/somefolder/*.txt"
for f in $files
do
  sed '/^$/d' $f > $f.tmp
  mv  $f.tmp $f
done