java.security.spec.KeySpec#getClass()源码实例Demo

下面列出了java.security.spec.KeySpec#getClass() 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。

源代码1 项目: j2objc   文件: KeyFactoryTest.java
@Override
protected PrivateKey engineGeneratePrivate(KeySpec keySpec)
        throws InvalidKeySpecException {
    if (TestPrivateKeySpec.class == keySpec.getClass()) {
        return new TestPrivateKey(((TestPrivateKeySpec)keySpec).encoded);
    }

    throw new InvalidKeySpecException();
}
 
源代码2 项目: j2objc   文件: KeyFactoryTest.java
@Override
protected PublicKey engineGeneratePublic(KeySpec keySpec)
        throws InvalidKeySpecException {
    if (TestPublicKeySpec.class == keySpec.getClass()) {
        return new TestPublicKey(((TestPublicKeySpec)keySpec).encoded);
    }
    throw new InvalidKeySpecException();
}
 
源代码3 项目: RipplePower   文件: KeyFactory.java
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());
}
 
源代码4 项目: RipplePower   文件: KeyFactory.java
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());
}
 
源代码5 项目: RipplePower   文件: RainbowKeyFactorySpi.java
/**
 * 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() + ".");
}
 
源代码6 项目: RipplePower   文件: McElieceCCA2KeyFactorySpi.java
/**
 * 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() + ".");
}
 
源代码7 项目: RipplePower   文件: McElieceCCA2KeyFactorySpi.java
/**
 * 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() + ".");
}
 
源代码8 项目: RipplePower   文件: McElieceKeyFactorySpi.java
/**
 * 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() + ".");
}
 
源代码9 项目: RipplePower   文件: McElieceKeyFactorySpi.java
/**
 * 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() + ".");
}
 
源代码10 项目: ripple-lib-java   文件: RainbowKeyFactorySpi.java
/**
 * 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() + ".");
}
 
/**
 * 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() + ".");
}
 
/**
 * 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() + ".");
}
 
源代码13 项目: ripple-lib-java   文件: McElieceKeyFactorySpi.java
/**
 * 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() + ".");
}
 
源代码14 项目: ripple-lib-java   文件: McElieceKeyFactorySpi.java
/**
 * 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() + ".");
}
 
 方法所在类
 同类方法