Skip to content

Instantly share code, notes, and snippets.

@sureshg
Forked from markscottwright/convert to pkcs 8.md
Created June 20, 2018 18:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sureshg/562f01d0f793c3809272f824925ab50c to your computer and use it in GitHub Desktop.
Save sureshg/562f01d0f793c3809272f824925ab50c to your computer and use it in GitHub Desktop.
How to convert a java private key from PKCS#1 encoding to PKCS#8

I had some historical key material data in pkcs#1 format that needed to be in pkcs#8 for input into another system. Here's how to do it, using BouncyCastle:

import org.bouncycastle.asn1.ASN1InputStream;
import org.bouncycastle.asn1.DERObject;
import org.bouncycastle.asn1.pkcs.PKCSObjectIdentifiers;
import org.bouncycastle.asn1.pkcs.PrivateKeyInfo;
import org.bouncycastle.asn1.x509.AlgorithmIdentifier;
import java.security.PrivateKey;


    public static byte[] toPkcs8(PrivateKey k) throws IOException {
        final String keyFormat = k.getFormat();

        if (keyFormat.equals("PKCS#8")) {
            return k.getEncoded();
        }

        else if (keyFormat.equals("PKCS#1")) {
            try (ASN1InputStream asn1InputStream = new ASN1InputStream(k.getEncoded())) {
                DERObject rsaPrivateKey = asn1InputStream.readObject();
                return new PrivateKeyInfo(
                        new AlgorithmIdentifier(PKCSObjectIdentifiers.rsaEncryption), rsaPrivateKey)
                                .getDEREncoded();
            }
        }

        throw new IOException("Unexpected key format" + keyFormat);
    }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment