0

I've created an org.bouncycastle.asn1.pkcs.RSAPrivateKey using its static getInstance(byte[]) from a PKCS#1 formated DER bytes, now I want to cast(or change) this to PrivateKey, how to do that??

monim
  • 3,427
  • 3
  • 23
  • 36

1 Answers1

0

You can directly create the PrivateKey from an ASN.1 encoded bytes array.

public static PrivateKey makeKey(byte[] keyBytes) {
    KeyFactory keyFactory = KeyFactory.getInstance("RSA");
    KeySpec privateKeySpec = new PKCS8EncodedKeySpec(keyBytes);
    PrivateKey privateKey = keyFactory.generatePrivate(privateKeySpec);
    return privateKey;
}
divanov
  • 6,173
  • 3
  • 32
  • 51
Buddhima Gamlath
  • 2,318
  • 1
  • 16
  • 26
  • 1
    the bytes are not PKCS8 encoded so this probably will throw invalidkeyException – monim Dec 24 '13 at 06:09
  • 1
    `RSAPrivateKey` doesn't have `getInstance(byte[])` method. http://www.bouncycastle.org/docs/docs1.5on/org/bouncycastle/asn1/pkcs/RSAPrivateKey.html#getInstance%28org.bouncycastle.asn1.ASN1TaggedObject,%20boolean%29 – divanov Dec 29 '13 at 09:47