TechBuzzPro

Convert PK8 and PEM to Android Keystore

· 5 min read · Phones & Tablets

In this article
  1. What You'll Need
  2. Step-by-Step
  3. Common Mistakes
  4. Tips
  5. Troubleshooting
  6. Frequently Asked Questions

You can generate an Android keystore from a PK8 private key and a PEM certificate using the Java keytool command, but only if the PK8 file is in unencrypted PKCS#8 format and the PEM contains the matching X.509 certificate. The standard approach is to convert the PK8 into a PKCS#12 bundle first, then import that bundle into a JKS or PKCS#12 keystore with the correct alias. This is the same process used to restore a signing key for an app that was originally signed elsewhere, for example when migrating from a different build system or recovering a lost keystore. Note that menu labels and command flags can differ slightly by JDK version, so check your installed Java version if a flag is rejected.

What You'll Need

  • The private key file in PK8 format (binary or base64 DER, unencrypted PKCS#8).
  • The certificate file in PEM format (usually named cert.pem or certificate.pem).
  • A Java Development Kit (JDK) installed, which provides keytool and openssl is available separately. OpenSSL is not part of the JDK, so install it if it is missing.
  • A terminal or command prompt with both tools on your PATH.
  • The password you want to use for the new keystore and key.

Step-by-Step

  1. Confirm the PK8 is unencrypted PKCS#8. Run openssl pkcs8 -inform DER -in key.pk8 -nocrypt -out key.pem. If it succeeds, you now have a PEM private key. If it fails with a decryption or format error, the PK8 is encrypted or not PKCS#8, and you must obtain the correct unencrypted file or the passphrase before continuing.

  2. Check that the key and certificate match. Run openssl x509 -in cert.pem -noout -modulus and openssl rsa -in key.pem -noout -modulus. The two modulus strings must be identical. If they differ, the files belong to different key pairs and the keystore will not sign correctly.

  3. Bundle the key and certificate into PKCS#12. Run openssl pkcs12 -export -in cert.pem -inkey key.pem -name mykey -out bundle.p12. When prompted, set an export password. The -name value becomes the alias, so choose the alias you want in the final keystore.

  4. Create the Android keystore from the bundle. Run keytool -importkeystore -srckeystore bundle.p12 -srcstoretype PKCS12 -destkeystore my.keystore -deststoretype JKS. Enter the source password (the export password from step 3) and set a new destination password. For modern Android tooling, PKCS12 is also accepted as the destination type; JKS remains widely supported.

  5. Verify the result. Run keytool -list -v -keystore my.keystore and confirm the alias, key algorithm, key size and certificate fingerprint are present. The SHA-1 and SHA-256 fingerprints should match the original certificate.

  6. Test a signature. Optionally sign a small APK or run a Gradle build with the new keystore configured. A successful signing step confirms the key and certificate chain are usable.

Common Mistakes

  • Using an encrypted PK8 without the passphrase. The -nocrypt flag only works on unencrypted files.
  • Mixing a key and certificate from different pairs. Always compare the modulus before importing.
  • Forgetting the alias. If you omit -name, OpenSSL uses a default alias that may not match what your build expects.
  • Choosing the wrong destination store type. Some older tools expect JKS, while newer ones prefer PKCS12; check what your build system accepts.
  • Losing the keystore password. There is no recovery; keep it in a password manager and back up the file.

Tips

  • Keep the original PK8 and PEM files in a secure backup. The keystore is a conversion, not a replacement for the source material.
  • Use a strong, unique password for both the store and the key.
  • Store the keystore outside your project repository and reference it through environment variables or a local properties file.
  • If you need to support both store types, generate PKCS12 and convert to JKS only when a tool requires it.

Troubleshooting

If keytool reports that the alias already exists, choose a different -name value during the PKCS#12 export or delete the existing entry with keytool -delete -alias mykey -keystore my.keystore. If the import fails with a password error, confirm you are entering the PKCS#12 export password as the source password, not the destination password. If OpenSSL rejects the PK8 with a format error, the file may be base64-encoded DER rather than binary; decode it first or use the -inform PEM variant. If the certificate chain is incomplete, concatenate the intermediate certificates into the PEM before exporting the PKCS#12 bundle. If signing still fails after a successful import, verify the key algorithm and size match what your build tools expect, and confirm the alias in your signing configuration matches the one in the keystore.

Frequently Asked Questions

Can I import a PK8 directly into keytool?

No. keytool does not read raw PK8 files. Convert the PK8 to a PKCS#12 bundle with OpenSSL first, then import that bundle.

Do I need the original keystore to update an app?

Yes. Android requires the same signing key for updates. If the original keystore is lost, the PK8 and PEM can reconstruct an equivalent keystore as long as the key pair is unchanged.

Is JKS or PKCS12 better for Android?

PKCS12 is the modern default and is accepted by current Android tooling. JKS remains supported for older projects, so use whichever your build system documents.

What if my PK8 is encrypted?

You need the passphrase. Run openssl pkcs8 -inform DER -in key.pk8 -out key.pem and enter the passphrase when prompted. Without it, the key cannot be recovered.

Can I change the alias later?

Yes. Use keytool -changealias -keystore my.keystore -alias oldname -destalias newname and update your signing configuration to match.

Why does the fingerprint not match?

The certificate in the PEM is probably not the one paired with the PK8, or an intermediate certificate was included in the wrong order. Recheck the modulus comparison from step 2.