Java Code Examples for java.security.spec.KeySpec#getClass()
The following examples show how to use
java.security.spec.KeySpec#getClass() .
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: KeyFactoryTest.java From j2objc with Apache License 2.0 | 5 votes |
@Override protected PrivateKey engineGeneratePrivate(KeySpec keySpec) throws InvalidKeySpecException { if (TestPrivateKeySpec.class == keySpec.getClass()) { return new TestPrivateKey(((TestPrivateKeySpec)keySpec).encoded); } throw new InvalidKeySpecException(); }
Example 2
Source File: KeyFactoryTest.java From j2objc with Apache License 2.0 | 5 votes |
@Override protected PublicKey engineGeneratePublic(KeySpec keySpec) throws InvalidKeySpecException { if (TestPublicKeySpec.class == keySpec.getClass()) { return new TestPublicKey(((TestPublicKeySpec)keySpec).encoded); } throw new InvalidKeySpecException(); }
Example 3
Source File: KeyFactory.java From RipplePower with Apache License 2.0 | 5 votes |
protected PrivateKey engineGeneratePrivate(KeySpec keySpec) throws InvalidKeySpecException { if (keySpec instanceof EdDSAPrivateKeySpec) { return new EdDSAPrivateKey((EdDSAPrivateKeySpec) keySpec); } if (keySpec instanceof PKCS8EncodedKeySpec) { return new EdDSAPrivateKey((PKCS8EncodedKeySpec) keySpec); } throw new InvalidKeySpecException("key spec not recognised: " + keySpec.getClass()); }
Example 4
Source File: KeyFactory.java From RipplePower with Apache License 2.0 | 5 votes |
protected PublicKey engineGeneratePublic(KeySpec keySpec) throws InvalidKeySpecException { if (keySpec instanceof EdDSAPublicKeySpec) { return new EdDSAPublicKey((EdDSAPublicKeySpec) keySpec); } if (keySpec instanceof X509EncodedKeySpec) { return new EdDSAPublicKey((X509EncodedKeySpec) keySpec); } throw new InvalidKeySpecException("key spec not recognised: " + keySpec.getClass()); }
Example 5
Source File: RainbowKeyFactorySpi.java From RipplePower with Apache License 2.0 | 4 votes |
/** * Converts, if possible, a key specification into a * {@link BCRainbowPrivateKey}. Currently, the following key specifications * are supported: {@link RainbowPrivateKeySpec}, {@link PKCS8EncodedKeySpec}. * <p> * The ASN.1 definition of the key structure is * <pre> * RainbowPrivateKey ::= SEQUENCE { * oid OBJECT IDENTIFIER -- OID identifying the algorithm * A1inv SEQUENCE OF OCTET STRING -- inversed matrix of L1 * b1 OCTET STRING -- translation vector of L1 * A2inv SEQUENCE OF OCTET STRING -- inversed matrix of L2 * b2 OCTET STRING -- translation vector of L2 * vi OCTET STRING -- num of elmts in each Set S * layers SEQUENCE OF Layer -- layers of F * } * * Layer ::= SEQUENCE OF Poly * Poly ::= SEQUENCE { * alpha SEQUENCE OF OCTET STRING * beta SEQUENCE OF OCTET STRING * gamma OCTET STRING * eta OCTET * } * </pre> * </p> * * @param keySpec the key specification * @return the Rainbow private key * @throws InvalidKeySpecException if the KeySpec is not supported. */ public PrivateKey engineGeneratePrivate(KeySpec keySpec) throws InvalidKeySpecException { if (keySpec instanceof RainbowPrivateKeySpec) { return new BCRainbowPrivateKey((RainbowPrivateKeySpec)keySpec); } else if (keySpec instanceof PKCS8EncodedKeySpec) { // get the DER-encoded Key according to PKCS#8 from the spec byte[] encKey = ((PKCS8EncodedKeySpec)keySpec).getEncoded(); try { return generatePrivate(PrivateKeyInfo.getInstance(ASN1Primitive.fromByteArray(encKey))); } catch (Exception e) { throw new InvalidKeySpecException(e.toString()); } } throw new InvalidKeySpecException("Unsupported key specification: " + keySpec.getClass() + "."); }
Example 6
Source File: McElieceCCA2KeyFactorySpi.java From RipplePower with Apache License 2.0 | 4 votes |
/** * Converts, if possible, a key specification into a * {@link BCMcElieceCCA2PublicKey}. Currently, the following key * specifications are supported: {@link McElieceCCA2PublicKeySpec}, * {@link X509EncodedKeySpec}. * * @param keySpec the key specification * @return the McEliece CCA2 public key * @throws InvalidKeySpecException if the key specification is not supported. */ public PublicKey generatePublic(KeySpec keySpec) throws InvalidKeySpecException { if (keySpec instanceof McElieceCCA2PublicKeySpec) { return new BCMcElieceCCA2PublicKey( (McElieceCCA2PublicKeySpec)keySpec); } else if (keySpec instanceof X509EncodedKeySpec) { // get the DER-encoded Key according to X.509 from the spec byte[] encKey = ((X509EncodedKeySpec)keySpec).getEncoded(); // decode the SubjectPublicKeyInfo data structure to the pki object SubjectPublicKeyInfo pki; try { pki = SubjectPublicKeyInfo.getInstance(ASN1Primitive.fromByteArray(encKey)); } catch (IOException e) { throw new InvalidKeySpecException(e.toString()); } try { // --- Build and return the actual key. ASN1Primitive innerType = pki.parsePublicKey(); ASN1Sequence publicKey = (ASN1Sequence)innerType; // decode oidString (but we don't need it right now) String oidString = ((ASN1ObjectIdentifier)publicKey.getObjectAt(0)) .toString(); // decode <n> BigInteger bigN = ((ASN1Integer)publicKey.getObjectAt(1)).getValue(); int n = bigN.intValue(); // decode <t> BigInteger bigT = ((ASN1Integer)publicKey.getObjectAt(2)).getValue(); int t = bigT.intValue(); // decode <matrixG> byte[] matrixG = ((ASN1OctetString)publicKey.getObjectAt(3)).getOctets(); return new BCMcElieceCCA2PublicKey(new McElieceCCA2PublicKeySpec( OID, n, t, matrixG)); } catch (IOException cce) { throw new InvalidKeySpecException( "Unable to decode X509EncodedKeySpec: " + cce.getMessage()); } } throw new InvalidKeySpecException("Unsupported key specification: " + keySpec.getClass() + "."); }
Example 7
Source File: McElieceCCA2KeyFactorySpi.java From RipplePower with Apache License 2.0 | 4 votes |
/** * Converts, if possible, a key specification into a * {@link BCMcElieceCCA2PrivateKey}. Currently, the following key * specifications are supported: {@link McElieceCCA2PrivateKeySpec}, * {@link PKCS8EncodedKeySpec}. * * @param keySpec the key specification * @return the McEliece CCA2 private key * @throws InvalidKeySpecException if the KeySpec is not supported. */ public PrivateKey generatePrivate(KeySpec keySpec) throws InvalidKeySpecException { if (keySpec instanceof McElieceCCA2PrivateKeySpec) { return new BCMcElieceCCA2PrivateKey( (McElieceCCA2PrivateKeySpec)keySpec); } else if (keySpec instanceof PKCS8EncodedKeySpec) { // get the DER-encoded Key according to PKCS#8 from the spec byte[] encKey = ((PKCS8EncodedKeySpec)keySpec).getEncoded(); // decode the PKCS#8 data structure to the pki object PrivateKeyInfo pki; try { pki = PrivateKeyInfo.getInstance(ASN1Primitive.fromByteArray(encKey)); } catch (IOException e) { throw new InvalidKeySpecException("Unable to decode PKCS8EncodedKeySpec: " + e); } try { // get the inner type inside the BIT STRING ASN1Primitive innerType = pki.parsePrivateKey().toASN1Primitive(); // build and return the actual key ASN1Sequence privKey = (ASN1Sequence)innerType; // decode oidString (but we don't need it right now) String oidString = ((ASN1ObjectIdentifier)privKey.getObjectAt(0)) .toString(); // decode <n> BigInteger bigN = ((ASN1Integer)privKey.getObjectAt(1)).getValue(); int n = bigN.intValue(); // decode <k> BigInteger bigK = ((ASN1Integer)privKey.getObjectAt(2)).getValue(); int k = bigK.intValue(); // decode <fieldPoly> byte[] encFieldPoly = ((ASN1OctetString)privKey.getObjectAt(3)) .getOctets(); // decode <goppaPoly> byte[] encGoppaPoly = ((ASN1OctetString)privKey.getObjectAt(4)) .getOctets(); // decode <p> byte[] encP = ((ASN1OctetString)privKey.getObjectAt(5)).getOctets(); // decode <h> byte[] encH = ((ASN1OctetString)privKey.getObjectAt(6)).getOctets(); // decode <qInv> ASN1Sequence qSeq = (ASN1Sequence)privKey.getObjectAt(7); byte[][] encQInv = new byte[qSeq.size()][]; for (int i = 0; i < qSeq.size(); i++) { encQInv[i] = ((ASN1OctetString)qSeq.getObjectAt(i)).getOctets(); } return new BCMcElieceCCA2PrivateKey( new McElieceCCA2PrivateKeySpec(OID, n, k, encFieldPoly, encGoppaPoly, encP, encH, encQInv)); } catch (IOException cce) { throw new InvalidKeySpecException( "Unable to decode PKCS8EncodedKeySpec."); } } throw new InvalidKeySpecException("Unsupported key specification: " + keySpec.getClass() + "."); }
Example 8
Source File: McElieceKeyFactorySpi.java From RipplePower with Apache License 2.0 | 4 votes |
/** * Converts, if possible, a key specification into a * {@link BCMcEliecePublicKey}. Currently, the following key specifications * are supported: {@link McEliecePublicKeySpec}, {@link X509EncodedKeySpec}. * * @param keySpec the key specification * @return the McEliece public key * @throws InvalidKeySpecException if the key specification is not supported. */ public PublicKey generatePublic(KeySpec keySpec) throws InvalidKeySpecException { if (keySpec instanceof McEliecePublicKeySpec) { return new BCMcEliecePublicKey((McEliecePublicKeySpec)keySpec); } else if (keySpec instanceof X509EncodedKeySpec) { // get the DER-encoded Key according to X.509 from the spec byte[] encKey = ((X509EncodedKeySpec)keySpec).getEncoded(); // decode the SubjectPublicKeyInfo data structure to the pki object SubjectPublicKeyInfo pki; try { pki = SubjectPublicKeyInfo.getInstance(ASN1Primitive.fromByteArray(encKey)); } catch (IOException e) { throw new InvalidKeySpecException(e.toString()); } try { // --- Build and return the actual key. ASN1Primitive innerType = pki.parsePublicKey(); ASN1Sequence publicKey = (ASN1Sequence)innerType; // decode oidString (but we don't need it right now) String oidString = ((ASN1ObjectIdentifier)publicKey.getObjectAt(0)) .toString(); // decode <n> BigInteger bigN = ((ASN1Integer)publicKey.getObjectAt(1)).getValue(); int n = bigN.intValue(); // decode <t> BigInteger bigT = ((ASN1Integer)publicKey.getObjectAt(2)).getValue(); int t = bigT.intValue(); // decode <matrixG> byte[] matrixG = ((ASN1OctetString)publicKey.getObjectAt(3)).getOctets(); return new BCMcEliecePublicKey(new McEliecePublicKeySpec(OID, t, n, matrixG)); } catch (IOException cce) { throw new InvalidKeySpecException( "Unable to decode X509EncodedKeySpec: " + cce.getMessage()); } } throw new InvalidKeySpecException("Unsupported key specification: " + keySpec.getClass() + "."); }
Example 9
Source File: McElieceKeyFactorySpi.java From RipplePower with Apache License 2.0 | 4 votes |
/** * Converts, if possible, a key specification into a * {@link BCMcEliecePrivateKey}. Currently, the following key specifications * are supported: {@link McEliecePrivateKeySpec}, * {@link PKCS8EncodedKeySpec}. * * @param keySpec the key specification * @return the McEliece private key * @throws InvalidKeySpecException if the KeySpec is not supported. */ public PrivateKey generatePrivate(KeySpec keySpec) throws InvalidKeySpecException { if (keySpec instanceof McEliecePrivateKeySpec) { return new BCMcEliecePrivateKey((McEliecePrivateKeySpec)keySpec); } else if (keySpec instanceof PKCS8EncodedKeySpec) { // get the DER-encoded Key according to PKCS#8 from the spec byte[] encKey = ((PKCS8EncodedKeySpec)keySpec).getEncoded(); // decode the PKCS#8 data structure to the pki object PrivateKeyInfo pki; try { pki = PrivateKeyInfo.getInstance(ASN1Primitive.fromByteArray(encKey)); } catch (IOException e) { throw new InvalidKeySpecException("Unable to decode PKCS8EncodedKeySpec: " + e); } try { ASN1Primitive innerType = pki.parsePrivateKey().toASN1Primitive(); // build and return the actual key ASN1Sequence privKey = (ASN1Sequence)innerType; // decode oidString (but we don't need it right now) String oidString = ((ASN1ObjectIdentifier)privKey.getObjectAt(0)) .toString(); // decode <n> BigInteger bigN = ((ASN1Integer)privKey.getObjectAt(1)).getValue(); int n = bigN.intValue(); // decode <k> BigInteger bigK = ((ASN1Integer)privKey.getObjectAt(2)).getValue(); int k = bigK.intValue(); // decode <fieldPoly> byte[] encFieldPoly = ((ASN1OctetString)privKey.getObjectAt(3)) .getOctets(); // decode <goppaPoly> byte[] encGoppaPoly = ((ASN1OctetString)privKey.getObjectAt(4)) .getOctets(); // decode <sInv> byte[] encSInv = ((ASN1OctetString)privKey.getObjectAt(5)).getOctets(); // decode <p1> byte[] encP1 = ((ASN1OctetString)privKey.getObjectAt(6)).getOctets(); // decode <p2> byte[] encP2 = ((ASN1OctetString)privKey.getObjectAt(7)).getOctets(); //decode <h> byte[] encH = ((ASN1OctetString)privKey.getObjectAt(8)).getOctets(); // decode <qInv> ASN1Sequence qSeq = (ASN1Sequence)privKey.getObjectAt(9); byte[][] encQInv = new byte[qSeq.size()][]; for (int i = 0; i < qSeq.size(); i++) { encQInv[i] = ((ASN1OctetString)qSeq.getObjectAt(i)).getOctets(); } return new BCMcEliecePrivateKey(new McEliecePrivateKeySpec(OID, n, k, encFieldPoly, encGoppaPoly, encSInv, encP1, encP2, encH, encQInv)); } catch (IOException cce) { throw new InvalidKeySpecException( "Unable to decode PKCS8EncodedKeySpec."); } } throw new InvalidKeySpecException("Unsupported key specification: " + keySpec.getClass() + "."); }
Example 10
Source File: RainbowKeyFactorySpi.java From ripple-lib-java with ISC License | 4 votes |
/** * Converts, if possible, a key specification into a * {@link BCRainbowPrivateKey}. Currently, the following key specifications * are supported: {@link RainbowPrivateKeySpec}, {@link PKCS8EncodedKeySpec}. * <p> * The ASN.1 definition of the key structure is * <pre> * RainbowPrivateKey ::= SEQUENCE { * oid OBJECT IDENTIFIER -- OID identifying the algorithm * A1inv SEQUENCE OF OCTET STRING -- inversed matrix of L1 * b1 OCTET STRING -- translation vector of L1 * A2inv SEQUENCE OF OCTET STRING -- inversed matrix of L2 * b2 OCTET STRING -- translation vector of L2 * vi OCTET STRING -- num of elmts in each Set S * layers SEQUENCE OF Layer -- layers of F * } * * Layer ::= SEQUENCE OF Poly * Poly ::= SEQUENCE { * alpha SEQUENCE OF OCTET STRING * beta SEQUENCE OF OCTET STRING * gamma OCTET STRING * eta OCTET * } * </pre> * </p> * * @param keySpec the key specification * @return the Rainbow private key * @throws InvalidKeySpecException if the KeySpec is not supported. */ public PrivateKey engineGeneratePrivate(KeySpec keySpec) throws InvalidKeySpecException { if (keySpec instanceof RainbowPrivateKeySpec) { return new BCRainbowPrivateKey((RainbowPrivateKeySpec)keySpec); } else if (keySpec instanceof PKCS8EncodedKeySpec) { // get the DER-encoded Key according to PKCS#8 from the spec byte[] encKey = ((PKCS8EncodedKeySpec)keySpec).getEncoded(); try { return generatePrivate(PrivateKeyInfo.getInstance(ASN1Primitive.fromByteArray(encKey))); } catch (Exception e) { throw new InvalidKeySpecException(e.toString()); } } throw new InvalidKeySpecException("Unsupported key specification: " + keySpec.getClass() + "."); }
Example 11
Source File: McElieceCCA2KeyFactorySpi.java From ripple-lib-java with ISC License | 4 votes |
/** * Converts, if possible, a key specification into a * {@link BCMcElieceCCA2PublicKey}. Currently, the following key * specifications are supported: {@link McElieceCCA2PublicKeySpec}, * {@link X509EncodedKeySpec}. * * @param keySpec the key specification * @return the McEliece CCA2 public key * @throws InvalidKeySpecException if the key specification is not supported. */ public PublicKey generatePublic(KeySpec keySpec) throws InvalidKeySpecException { if (keySpec instanceof McElieceCCA2PublicKeySpec) { return new BCMcElieceCCA2PublicKey( (McElieceCCA2PublicKeySpec)keySpec); } else if (keySpec instanceof X509EncodedKeySpec) { // get the DER-encoded Key according to X.509 from the spec byte[] encKey = ((X509EncodedKeySpec)keySpec).getEncoded(); // decode the SubjectPublicKeyInfo data structure to the pki object SubjectPublicKeyInfo pki; try { pki = SubjectPublicKeyInfo.getInstance(ASN1Primitive.fromByteArray(encKey)); } catch (IOException e) { throw new InvalidKeySpecException(e.toString()); } try { // --- Build and return the actual key. ASN1Primitive innerType = pki.parsePublicKey(); ASN1Sequence publicKey = (ASN1Sequence)innerType; // decode oidString (but we don't need it right now) String oidString = ((ASN1ObjectIdentifier)publicKey.getObjectAt(0)) .toString(); // decode <n> BigInteger bigN = ((ASN1Integer)publicKey.getObjectAt(1)).getValue(); int n = bigN.intValue(); // decode <t> BigInteger bigT = ((ASN1Integer)publicKey.getObjectAt(2)).getValue(); int t = bigT.intValue(); // decode <matrixG> byte[] matrixG = ((ASN1OctetString)publicKey.getObjectAt(3)).getOctets(); return new BCMcElieceCCA2PublicKey(new McElieceCCA2PublicKeySpec( OID, n, t, matrixG)); } catch (IOException cce) { throw new InvalidKeySpecException( "Unable to decode X509EncodedKeySpec: " + cce.getMessage()); } } throw new InvalidKeySpecException("Unsupported key specification: " + keySpec.getClass() + "."); }
Example 12
Source File: McElieceCCA2KeyFactorySpi.java From ripple-lib-java with ISC License | 4 votes |
/** * Converts, if possible, a key specification into a * {@link BCMcElieceCCA2PrivateKey}. Currently, the following key * specifications are supported: {@link McElieceCCA2PrivateKeySpec}, * {@link PKCS8EncodedKeySpec}. * * @param keySpec the key specification * @return the McEliece CCA2 private key * @throws InvalidKeySpecException if the KeySpec is not supported. */ public PrivateKey generatePrivate(KeySpec keySpec) throws InvalidKeySpecException { if (keySpec instanceof McElieceCCA2PrivateKeySpec) { return new BCMcElieceCCA2PrivateKey( (McElieceCCA2PrivateKeySpec)keySpec); } else if (keySpec instanceof PKCS8EncodedKeySpec) { // get the DER-encoded Key according to PKCS#8 from the spec byte[] encKey = ((PKCS8EncodedKeySpec)keySpec).getEncoded(); // decode the PKCS#8 data structure to the pki object PrivateKeyInfo pki; try { pki = PrivateKeyInfo.getInstance(ASN1Primitive.fromByteArray(encKey)); } catch (IOException e) { throw new InvalidKeySpecException("Unable to decode PKCS8EncodedKeySpec: " + e); } try { // get the inner type inside the BIT STRING ASN1Primitive innerType = pki.parsePrivateKey().toASN1Primitive(); // build and return the actual key ASN1Sequence privKey = (ASN1Sequence)innerType; // decode oidString (but we don't need it right now) String oidString = ((ASN1ObjectIdentifier)privKey.getObjectAt(0)) .toString(); // decode <n> BigInteger bigN = ((ASN1Integer)privKey.getObjectAt(1)).getValue(); int n = bigN.intValue(); // decode <k> BigInteger bigK = ((ASN1Integer)privKey.getObjectAt(2)).getValue(); int k = bigK.intValue(); // decode <fieldPoly> byte[] encFieldPoly = ((ASN1OctetString)privKey.getObjectAt(3)) .getOctets(); // decode <goppaPoly> byte[] encGoppaPoly = ((ASN1OctetString)privKey.getObjectAt(4)) .getOctets(); // decode <p> byte[] encP = ((ASN1OctetString)privKey.getObjectAt(5)).getOctets(); // decode <h> byte[] encH = ((ASN1OctetString)privKey.getObjectAt(6)).getOctets(); // decode <qInv> ASN1Sequence qSeq = (ASN1Sequence)privKey.getObjectAt(7); byte[][] encQInv = new byte[qSeq.size()][]; for (int i = 0; i < qSeq.size(); i++) { encQInv[i] = ((ASN1OctetString)qSeq.getObjectAt(i)).getOctets(); } return new BCMcElieceCCA2PrivateKey( new McElieceCCA2PrivateKeySpec(OID, n, k, encFieldPoly, encGoppaPoly, encP, encH, encQInv)); } catch (IOException cce) { throw new InvalidKeySpecException( "Unable to decode PKCS8EncodedKeySpec."); } } throw new InvalidKeySpecException("Unsupported key specification: " + keySpec.getClass() + "."); }
Example 13
Source File: McElieceKeyFactorySpi.java From ripple-lib-java with ISC License | 4 votes |
/** * Converts, if possible, a key specification into a * {@link BCMcEliecePublicKey}. Currently, the following key specifications * are supported: {@link McEliecePublicKeySpec}, {@link X509EncodedKeySpec}. * * @param keySpec the key specification * @return the McEliece public key * @throws InvalidKeySpecException if the key specification is not supported. */ public PublicKey generatePublic(KeySpec keySpec) throws InvalidKeySpecException { if (keySpec instanceof McEliecePublicKeySpec) { return new BCMcEliecePublicKey((McEliecePublicKeySpec)keySpec); } else if (keySpec instanceof X509EncodedKeySpec) { // get the DER-encoded Key according to X.509 from the spec byte[] encKey = ((X509EncodedKeySpec)keySpec).getEncoded(); // decode the SubjectPublicKeyInfo data structure to the pki object SubjectPublicKeyInfo pki; try { pki = SubjectPublicKeyInfo.getInstance(ASN1Primitive.fromByteArray(encKey)); } catch (IOException e) { throw new InvalidKeySpecException(e.toString()); } try { // --- Build and return the actual key. ASN1Primitive innerType = pki.parsePublicKey(); ASN1Sequence publicKey = (ASN1Sequence)innerType; // decode oidString (but we don't need it right now) String oidString = ((ASN1ObjectIdentifier)publicKey.getObjectAt(0)) .toString(); // decode <n> BigInteger bigN = ((ASN1Integer)publicKey.getObjectAt(1)).getValue(); int n = bigN.intValue(); // decode <t> BigInteger bigT = ((ASN1Integer)publicKey.getObjectAt(2)).getValue(); int t = bigT.intValue(); // decode <matrixG> byte[] matrixG = ((ASN1OctetString)publicKey.getObjectAt(3)).getOctets(); return new BCMcEliecePublicKey(new McEliecePublicKeySpec(OID, t, n, matrixG)); } catch (IOException cce) { throw new InvalidKeySpecException( "Unable to decode X509EncodedKeySpec: " + cce.getMessage()); } } throw new InvalidKeySpecException("Unsupported key specification: " + keySpec.getClass() + "."); }
Example 14
Source File: McElieceKeyFactorySpi.java From ripple-lib-java with ISC License | 4 votes |
/** * Converts, if possible, a key specification into a * {@link BCMcEliecePrivateKey}. Currently, the following key specifications * are supported: {@link McEliecePrivateKeySpec}, * {@link PKCS8EncodedKeySpec}. * * @param keySpec the key specification * @return the McEliece private key * @throws InvalidKeySpecException if the KeySpec is not supported. */ public PrivateKey generatePrivate(KeySpec keySpec) throws InvalidKeySpecException { if (keySpec instanceof McEliecePrivateKeySpec) { return new BCMcEliecePrivateKey((McEliecePrivateKeySpec)keySpec); } else if (keySpec instanceof PKCS8EncodedKeySpec) { // get the DER-encoded Key according to PKCS#8 from the spec byte[] encKey = ((PKCS8EncodedKeySpec)keySpec).getEncoded(); // decode the PKCS#8 data structure to the pki object PrivateKeyInfo pki; try { pki = PrivateKeyInfo.getInstance(ASN1Primitive.fromByteArray(encKey)); } catch (IOException e) { throw new InvalidKeySpecException("Unable to decode PKCS8EncodedKeySpec: " + e); } try { ASN1Primitive innerType = pki.parsePrivateKey().toASN1Primitive(); // build and return the actual key ASN1Sequence privKey = (ASN1Sequence)innerType; // decode oidString (but we don't need it right now) String oidString = ((ASN1ObjectIdentifier)privKey.getObjectAt(0)) .toString(); // decode <n> BigInteger bigN = ((ASN1Integer)privKey.getObjectAt(1)).getValue(); int n = bigN.intValue(); // decode <k> BigInteger bigK = ((ASN1Integer)privKey.getObjectAt(2)).getValue(); int k = bigK.intValue(); // decode <fieldPoly> byte[] encFieldPoly = ((ASN1OctetString)privKey.getObjectAt(3)) .getOctets(); // decode <goppaPoly> byte[] encGoppaPoly = ((ASN1OctetString)privKey.getObjectAt(4)) .getOctets(); // decode <sInv> byte[] encSInv = ((ASN1OctetString)privKey.getObjectAt(5)).getOctets(); // decode <p1> byte[] encP1 = ((ASN1OctetString)privKey.getObjectAt(6)).getOctets(); // decode <p2> byte[] encP2 = ((ASN1OctetString)privKey.getObjectAt(7)).getOctets(); //decode <h> byte[] encH = ((ASN1OctetString)privKey.getObjectAt(8)).getOctets(); // decode <qInv> ASN1Sequence qSeq = (ASN1Sequence)privKey.getObjectAt(9); byte[][] encQInv = new byte[qSeq.size()][]; for (int i = 0; i < qSeq.size(); i++) { encQInv[i] = ((ASN1OctetString)qSeq.getObjectAt(i)).getOctets(); } return new BCMcEliecePrivateKey(new McEliecePrivateKeySpec(OID, n, k, encFieldPoly, encGoppaPoly, encSInv, encP1, encP2, encH, encQInv)); } catch (IOException cce) { throw new InvalidKeySpecException( "Unable to decode PKCS8EncodedKeySpec."); } } throw new InvalidKeySpecException("Unsupported key specification: " + keySpec.getClass() + "."); }