jdkalgorithmparameters.java

来自「bouncycastle 是一个JAVA安全提供者」· Java 代码 · 共 1,385 行 · 第 1/3 页

JAVA
1,385
字号
                    parameterVersion = p.getRC2ParameterVersion().intValue();                }                iv = p.getIV();                return;            }            throw new IOException("Unknown parameters format in IV parameters object");        }        protected String engineToString()         {            return "RC2 Parameters";        }    }    public static class CAST5AlgorithmParameters        extends JDKAlgorithmParameters    {        private byte[]  iv;        private int     keyLength = 128;        protected byte[] engineGetEncoded()         {            byte[]  tmp = new byte[iv.length];            System.arraycopy(iv, 0, tmp, 0, iv.length);            return tmp;        }        protected byte[] engineGetEncoded(            String format)         {            if (format.equals("RAW"))            {                return engineGetEncoded();            }            else if (format.equals("ASN.1"))            {                ByteArrayOutputStream   bOut = new ByteArrayOutputStream();                ASN1OutputStream        dOut = new ASN1OutputStream(bOut);                try                {                    dOut.writeObject(new CAST5CBCParameters(engineGetEncoded(), keyLength));                }                catch (IOException e)                {                    return null;                }                return bOut.toByteArray();            }            return null;        }        protected AlgorithmParameterSpec engineGetParameterSpec(            Class paramSpec)             throws InvalidParameterSpecException        {            if (paramSpec == IvParameterSpec.class)            {                return new IvParameterSpec(iv);            }            throw new InvalidParameterSpecException("unknown parameter spec passed to CAST5 parameters object.");        }        protected void engineInit(            AlgorithmParameterSpec paramSpec)             throws InvalidParameterSpecException        {            if (paramSpec instanceof IvParameterSpec)            {                this.iv = ((IvParameterSpec)paramSpec).getIV();            }            else            {                throw new InvalidParameterSpecException("IvParameterSpec required to initialise a CAST5 parameters algorithm parameters object");            }        }        protected void engineInit(            byte[] params)             throws IOException        {            this.iv = new byte[params.length];            System.arraycopy(params, 0, iv, 0, iv.length);        }        protected void engineInit(            byte[] params,            String format)             throws IOException        {            if (format.equals("RAW"))            {                engineInit(params);                return;            }            else if (format.equals("ASN.1"))            {                ByteArrayInputStream    bIn = new ByteArrayInputStream(params);                ASN1InputStream         aIn = new ASN1InputStream(bIn);                CAST5CBCParameters      p = CAST5CBCParameters.getInstance(aIn.readObject());                keyLength = p.getKeyLength();                iv = p.getIV();                return;            }            throw new IOException("Unknown parameters format in IV parameters object");        }        protected String engineToString()         {            return "CAST5 Parameters";        }    }    public static class PKCS12PBE        extends JDKAlgorithmParameters    {        PKCS12PBEParams params;        protected byte[] engineGetEncoded()         {            ByteArrayOutputStream   bOut = new ByteArrayOutputStream();            DEROutputStream         dOut = new DEROutputStream(bOut);            try            {                dOut.writeObject(params);            }            catch (IOException e)            {                throw new RuntimeException("Oooops! " + e.toString());            }            return bOut.toByteArray();        }        protected byte[] engineGetEncoded(            String format)         {            if (format.equals("ASN.1"))            {                return engineGetEncoded();            }            return null;        }        protected AlgorithmParameterSpec engineGetParameterSpec(            Class paramSpec)             throws InvalidParameterSpecException        {            if (paramSpec == PBEParameterSpec.class)            {                return new PBEParameterSpec(params.getIV(),                                params.getIterations().intValue());            }            throw new InvalidParameterSpecException("unknown parameter spec passed to PKCS12 PBE parameters object.");        }        protected void engineInit(            AlgorithmParameterSpec paramSpec)             throws InvalidParameterSpecException        {            if (!(paramSpec instanceof PBEParameterSpec))            {                throw new InvalidParameterSpecException("PBEParameterSpec required to initialise a PKCS12 PBE parameters algorithm parameters object");            }            PBEParameterSpec    pbeSpec = (PBEParameterSpec)paramSpec;            this.params = new PKCS12PBEParams(pbeSpec.getSalt(),                                pbeSpec.getIterationCount());        }        protected void engineInit(            byte[] params)             throws IOException        {            ByteArrayInputStream   bIn = new ByteArrayInputStream(params);            ASN1InputStream        aIn = new ASN1InputStream(bIn);            this.params = PKCS12PBEParams.getInstance(aIn.readObject());        }        protected void engineInit(            byte[] params,            String format)             throws IOException        {            if (format.equals("ASN.1"))            {                engineInit(params);                return;            }            throw new IOException("Unknown parameters format in PKCS12 PBE parameters object");        }        protected String engineToString()         {            return "PKCS12 PBE Parameters";        }    }    public static class DH        extends JDKAlgorithmParameters    {        DHParameterSpec     currentSpec;        /**         * Return the PKCS#3 ASN.1 structure DHParameter.         * <p>         * <pre>         *  DHParameter ::= SEQUENCE {         *                   prime INTEGER, -- p         *                   base INTEGER, -- g         *                   privateValueLength INTEGER OPTIONAL}         * </pre>         */        protected byte[] engineGetEncoded()         {            ByteArrayOutputStream   bOut = new ByteArrayOutputStream();            DEROutputStream         dOut = new DEROutputStream(bOut);            DHParameter             dhP = new DHParameter(currentSpec.getP(), currentSpec.getG(), currentSpec.getL());            try            {                dOut.writeObject(dhP);                dOut.close();            }            catch (IOException e)            {                throw new RuntimeException("Error encoding DHParameters");            }            return bOut.toByteArray();        }        protected byte[] engineGetEncoded(            String format)         {            if (format.equalsIgnoreCase("X.509")                    || format.equalsIgnoreCase("ASN.1"))            {                return engineGetEncoded();            }            return null;        }        protected AlgorithmParameterSpec engineGetParameterSpec(            Class paramSpec)             throws InvalidParameterSpecException        {            if (paramSpec == DHParameterSpec.class)            {                return currentSpec;            }            throw new InvalidParameterSpecException("unknown parameter spec passed to DH parameters object.");        }        protected void engineInit(            AlgorithmParameterSpec paramSpec)             throws InvalidParameterSpecException        {            if (!(paramSpec instanceof DHParameterSpec))            {                throw new InvalidParameterSpecException("DHParameterSpec required to initialise a Diffie-Hellman algorithm parameters object");            }            this.currentSpec = (DHParameterSpec)paramSpec;        }        protected void engineInit(            byte[] params)             throws IOException        {            ByteArrayInputStream   bIn = new ByteArrayInputStream(params);            ASN1InputStream        aIn = new ASN1InputStream(bIn);            try            {                DHParameter dhP = new DHParameter((ASN1Sequence)aIn.readObject());                if (dhP.getL() != null)                {                    currentSpec = new DHParameterSpec(dhP.getP(), dhP.getG(), dhP.getL().intValue());                }                else                {                    currentSpec = new DHParameterSpec(dhP.getP(), dhP.getG());                }            }            catch (ClassCastException e)            {                throw new IOException("Not a valid DH Parameter encoding.");            }            catch (ArrayIndexOutOfBoundsException e)            {                throw new IOException("Not a valid DH Parameter encoding.");            }        }        protected void engineInit(            byte[] params,            String format)             throws IOException        {            if (format.equalsIgnoreCase("X.509")                    || format.equalsIgnoreCase("ASN.1"))            {                engineInit(params);            }            else            {                throw new IOException("Unknown parameter format " + format);            }        }        protected String engineToString()         {            return "Diffie-Hellman Parameters";        }    }    public static class DSA        extends JDKAlgorithmParameters    {        DSAParameterSpec     currentSpec;        /**         * Return the X.509 ASN.1 structure DSAParameter.         * <p>         * <pre>         *  DSAParameter ::= SEQUENCE {         *                   prime INTEGER, -- p         *                   subprime INTEGER, -- q         *                   base INTEGER, -- g}         * </pre>         */        protected byte[] engineGetEncoded()         {            ByteArrayOutputStream   bOut = new ByteArrayOutputStream();            DEROutputStream         dOut = new DEROutputStream(bOut);            DSAParameter            dsaP = new DSAParameter(currentSpec.getP(), currentSpec.getQ(), currentSpec.getG());            try            {                dOut.writeObject(dsaP);                dOut.close();            }            catch (IOException e)            {                throw new RuntimeException("Error encoding DSAParameters");            }            return bOut.toByteArray();        }        protected byte[] engineGetEncoded(            String format)         {            if (format.equalsIgnoreCase("X.509")                    || format.equalsIgnoreCase("ASN.1"))            {                return engineGetEncoded();            }            return null;        }        protected AlgorithmParameterSpec engineGetParameterSpec(            Class paramSpec)             throws InvalidParameterSpecException        {            if (paramSpec == DSAParameterSpec.class)            {                return currentSpec;            }            throw new InvalidParameterSpecException("unknown parameter spec passed to DSA parameters object.");        }        protected void engineInit(            AlgorithmParameterSpec paramSpec)             throws InvalidParameterSpecException        {            if (!(paramSpec instanceof DSAParameterSpec))            {                throw new InvalidParameterSpecException("DSAParameterSpec required to initialise a DSA algorithm parameters object");            }            this.currentSpec = (DSAParameterSpec)paramSpec;        }        protected void engineInit(            byte[] params)             throws IOException        {            ByteArrayInputStream   bIn = new ByteArrayInputStream(params);            ASN1InputStream        aIn = new ASN1InputStream(bIn);            try            {                DSAParameter dsaP = new DSAParameter((ASN1Sequence)aIn.readObject());                currentSpec = new DSAParameterSpec(dsaP.getP(), dsaP.getQ(), dsaP.getG());            }            catch (ClassCastException e)            {                throw new IOException("Not a valid DSA Parameter encoding.");            }            catch (ArrayIndexOutOfBoundsException e)            {                throw new IOException("Not a valid DSA Parameter encoding.");            }        }        protected void engineInit(            byte[] params,            String format)             throws IOException        {            if (format.equalsIgnoreCase("X.509")                    || format.equalsIgnoreCase("ASN.1"))            {                engineInit(params);            }            else            {                throw new IOException("Unknown parameter format " + format);            }        }        protected String engineToString()         {            return "DSA Parameters";        }    }    public static class GOST3410        extends JDKAlgorithmParameters    {        GOST3410ParameterSpec     currentSpec;                /**         * Return the X.509 ASN.1 structure GOST3410Parameter.         * <p>         * <pre>

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?