Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  package org.bouncycastle.pqc.jcajce.provider.mceliece;
  
  import java.security.Key;
 
 
     extends KeyFactorySpi
 {

    
The OID of the algorithm.
 
     public static final String OID = "1.3.6.1.4.1.8301.3.1.3.4.2";

    
Converts, if possible, a key specification into a BCMcElieceCCA2PublicKey. Currently, the following key specifications are supported: org.bouncycastle.pqc.jcajce.spec.McElieceCCA2PublicKeySpec, java.security.spec.X509EncodedKeySpec.

Parameters:
keySpec the key specification
Returns:
the McEliece CCA2 public key
Throws:
java.security.spec.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(
                    ntmatrixG));
            }
            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 BCMcElieceCCA2PrivateKey. Currently, the following key specifications are supported: org.bouncycastle.pqc.jcajce.spec.McElieceCCA2PrivateKeySpec, java.security.spec.PKCS8EncodedKeySpec.

Parameters:
keySpec the key specification
Returns:
the McEliece CCA2 private key
Throws:
java.security.spec.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(nkencFieldPoly,
                        encGoppaPolyencPencHencQInv));
            }
            catch (IOException cce)
            {
                throw new InvalidKeySpecException(
                    "Unable to decode PKCS8EncodedKeySpec.");
            }
        }
        throw new InvalidKeySpecException("Unsupported key specification: "
            + keySpec.getClass() + ".");
    }

    
Converts, if possible, a given key into a key specification. Currently, the following key specifications are supported:

Parameters:
key the key
keySpec the key specification
Returns:
the specification of the McEliece CCA2 key
Throws:
java.security.spec.InvalidKeySpecException if the key type or the key specification is not supported.
See also:
BCMcElieceCCA2PrivateKey
org.bouncycastle.pqc.jcajce.spec.McElieceCCA2PrivateKeySpec
BCMcElieceCCA2PublicKey
org.bouncycastle.pqc.jcajce.spec.McElieceCCA2PublicKeySpec
    public KeySpec getKeySpec(Key keyClass keySpec)
        throws InvalidKeySpecException
    {
        if (key instanceof BCMcElieceCCA2PrivateKey)
        {
            if (PKCS8EncodedKeySpec.class.isAssignableFrom(keySpec))
            {
                return new PKCS8EncodedKeySpec(key.getEncoded());
            }
            else if (McElieceCCA2PrivateKeySpec.class
                .isAssignableFrom(keySpec))
            {
                BCMcElieceCCA2PrivateKey privKey = (BCMcElieceCCA2PrivateKey)key;
                return new McElieceCCA2PrivateKeySpec(privKey.getN(), privKey
                    .getK(), privKey.getField(), privKey.getGoppaPoly(),
                    privKey.getP(), privKey.getH(), privKey.getQInv());
            }
        }
        else if (key instanceof BCMcElieceCCA2PublicKey)
        {
            if (X509EncodedKeySpec.class.isAssignableFrom(keySpec))
            {
                return new X509EncodedKeySpec(key.getEncoded());
            }
            else if (McElieceCCA2PublicKeySpec.class
                .isAssignableFrom(keySpec))
            {
                BCMcElieceCCA2PublicKey pubKey = (BCMcElieceCCA2PublicKey)key;
                return new McElieceCCA2PublicKeySpec(pubKey.getN(), pubKey
                    .getT(), pubKey.getG());
            }
        }
        else
        {
            throw new InvalidKeySpecException("Unsupported key type: "
                + key.getClass() + ".");
        }
        throw new InvalidKeySpecException("Unknown key specification: "
            + keySpec + ".");
    }

    
Translates a key into a form known by the FlexiProvider. Currently, only the following "source" keys are supported: BCMcElieceCCA2PrivateKey, BCMcElieceCCA2PublicKey.

Parameters:
key the key
Returns:
a key of a known key type
Throws:
java.security.InvalidKeyException if the key type is not supported.
    public Key translateKey(Key key)
        throws InvalidKeyException
    {
        if ((key instanceof BCMcElieceCCA2PrivateKey)
            || (key instanceof BCMcElieceCCA2PublicKey))
        {
            return key;
        }
        throw new InvalidKeyException("Unsupported key type.");
    }
        throws InvalidKeySpecException
    {
        // get the inner type inside the BIT STRING
        try
        {
            ASN1Primitive innerType = pki.parsePublicKey();
            McElieceCCA2PublicKey key = McElieceCCA2PublicKey.getInstance((ASN1Sequence)innerType);
            return new BCMcElieceCCA2PublicKey(key.getOID().getId(), key.getN(), key.getT(), key.getG());
        }
        catch (IOException cce)
        {
            throw new InvalidKeySpecException("Unable to decode X509EncodedKeySpec");
        }
    }
        throws InvalidKeySpecException
    {
        // get the inner type inside the BIT STRING
        try
        {
            ASN1Primitive innerType = pki.parsePrivateKey().toASN1Primitive();
            McElieceCCA2PrivateKey key = McElieceCCA2PrivateKey.getInstance(innerType);
            return new BCMcElieceCCA2PrivateKey(key.getOID().getId(), key.getN(), key.getK(), key.getField(), key.getGoppaPoly(), key.getP(), key.getH(), key.getQInv());
        }
        catch (IOException cce)
        {
            throw new InvalidKeySpecException("Unable to decode PKCS8EncodedKeySpec");
        }
    }
    protected PublicKey engineGeneratePublic(KeySpec keySpec)
        throws InvalidKeySpecException
    {
        return null;  //To change body of implemented methods use File | Settings | File Templates.
    }
    protected PrivateKey engineGeneratePrivate(KeySpec keySpec)
        throws InvalidKeySpecException
    {
        return null;  //To change body of implemented methods use File | Settings | File Templates.
    }
    protected KeySpec engineGetKeySpec(Key keyClass tClass)
        throws InvalidKeySpecException
    {
        return null;  //To change body of implemented methods use File | Settings | File Templates.
    }
    protected Key engineTranslateKey(Key key)
        throws InvalidKeyException
    {
        return null;  //To change body of implemented methods use File | Settings | File Templates.
    }
New to GrepCode? Check out our FAQ X