Here’s how you can generate a keystore, and a pem and pk8 file from it.
# Generate a keystore with validity set to 50 years.
keytool \
-genkeypair \
-keyalg RSA \
-keysize 2048 \
-keystore my_keystore.keystore \
-validity 18250 \
-alias my_alias \
-dname "CN=, OU=, O=My Company or Organization, L=London, S=, C=UK"
# Export the keystore to PKCS12.
keytool \
-importkeystore \
-srckeystore my_keystore.keystore \
-srcstoretype JKS \
-destkeystore intermediate.p12 \
-deststoretype PKCS12
# Export from PKCS12 to PEM (Base64-encoding).
openssl pkcs12 \
-in intermediate.p12 \
-out intermediate.rsa.pem \
-nodes
# Copy the private key to a separate file.
sed -n '1,/^-----END PRIVATE KEY/ p' intermediate.rsa.pem > my_private_key.rsa.pem
# Copy the certificate to a separate file.
sed -n '1,/^-----END PRIVATE KEY/ !p' intermediate.rsa.pem > my_certificate.x509.pem
# Convert the private key from PEM to PKCS8.
openssl pkcs8 \
-topk8 \
-in my_private_key.rsa.pem \
-inform PEM \
-nocrypt \
-out my_private_key.pk8 \
-outform DER