jdkalgorithmparameters.java

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

JAVA
1,385
字号
         *  GOST3410Parameter ::= SEQUENCE {         *                   prime INTEGER, -- p         *                   subprime INTEGER, -- q         *                   base INTEGER, -- a}         * </pre>         */        protected byte[] engineGetEncoded()        {            ByteArrayOutputStream   bOut = new ByteArrayOutputStream();            DEROutputStream         dOut = new DEROutputStream(bOut);            GOST3410PublicKeyAlgParameters       gost3410P = new GOST3410PublicKeyAlgParameters(new DERObjectIdentifier(currentSpec.getPublicKeyParamSetOID()), new DERObjectIdentifier(currentSpec.getDigestParamSetOID()), new DERObjectIdentifier(currentSpec.getEncryptionParamSetOID()));                        try            {                dOut.writeObject(gost3410P);                dOut.close();            }            catch (IOException e)            {                throw new RuntimeException("Error encoding GOST3410Parameters");            }                        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 == GOST3410PublicKeyParameterSetSpec.class)            {                return currentSpec;            }                        throw new InvalidParameterSpecException("unknown parameter spec passed to GOST3410 parameters object.");        }                protected void engineInit(                AlgorithmParameterSpec paramSpec)        throws InvalidParameterSpecException        {            if (!(paramSpec instanceof GOST3410ParameterSpec))            {                throw new InvalidParameterSpecException("GOST3410ParameterSpec required to initialise a GOST3410 algorithm parameters object");            }                        this.currentSpec = (GOST3410ParameterSpec)paramSpec;        }                protected void engineInit(                byte[] params)        throws IOException        {            ByteArrayInputStream   bIn = new ByteArrayInputStream(params);            ASN1InputStream        dIn = new ASN1InputStream(bIn);                        try            {                GOST3410PublicKeyAlgParameters gost3410P = new GOST3410PublicKeyAlgParameters((ASN1Sequence)dIn.readObject());                GOST3410ParamSetParameters p = GOST3410NamedParameters.getByOID(gost3410P.getPublicKeyParamSet());                                currentSpec = new GOST3410ParameterSpec(gost3410P.getPublicKeyParamSet().getId(), gost3410P.getDigestParamSet().getId(), gost3410P.getEncryptionParamSet().getId());            }            catch (ClassCastException e)            {                throw new IOException("Not a valid GOST3410 Parameter encoding.");            }            catch (ArrayIndexOutOfBoundsException e)            {                throw new IOException("Not a valid GOST3410 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 "GOST3410 Parameters";        }    }        public static class ElGamal        extends JDKAlgorithmParameters    {        ElGamalParameterSpec     currentSpec;        /**         * Return the X.509 ASN.1 structure ElGamalParameter.         * <p>         * <pre>         *  ElGamalParameter ::= SEQUENCE {         *                   prime INTEGER, -- p         *                   base INTEGER, -- g}         * </pre>         */        protected byte[] engineGetEncoded()         {            ByteArrayOutputStream   bOut = new ByteArrayOutputStream();            DEROutputStream         dOut = new DEROutputStream(bOut);            ElGamalParameter        elP = new ElGamalParameter(currentSpec.getP(), currentSpec.getG());            try            {                dOut.writeObject(elP);                dOut.close();            }            catch (IOException e)            {                throw new RuntimeException("Error encoding ElGamalParameters");            }            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 == ElGamalParameterSpec.class)            {                return currentSpec;            }            else if (paramSpec == DHParameterSpec.class)            {                return new DHParameterSpec(currentSpec.getP(), currentSpec.getG());            }            throw new InvalidParameterSpecException("unknown parameter spec passed to ElGamal parameters object.");        }        protected void engineInit(            AlgorithmParameterSpec paramSpec)             throws InvalidParameterSpecException        {            if (!(paramSpec instanceof ElGamalParameterSpec) && !(paramSpec instanceof DHParameterSpec))            {                throw new InvalidParameterSpecException("DHParameterSpec required to initialise a ElGamal algorithm parameters object");            }            if (paramSpec instanceof ElGamalParameterSpec)            {                this.currentSpec = (ElGamalParameterSpec)paramSpec;            }            else            {                DHParameterSpec s = (DHParameterSpec)paramSpec;                                this.currentSpec = new ElGamalParameterSpec(s.getP(), s.getG());            }        }        protected void engineInit(            byte[] params)             throws IOException        {            ByteArrayInputStream   bIn = new ByteArrayInputStream(params);            ASN1InputStream        aIn = new ASN1InputStream(bIn);            try            {                ElGamalParameter elP = new ElGamalParameter((ASN1Sequence)aIn.readObject());                currentSpec = new ElGamalParameterSpec(elP.getP(), elP.getG());            }            catch (ClassCastException e)            {                throw new IOException("Not a valid ElGamal Parameter encoding.");            }            catch (ArrayIndexOutOfBoundsException e)            {                throw new IOException("Not a valid ElGamal 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 "ElGamal Parameters";        }    }    public static class IES        extends JDKAlgorithmParameters    {        IESParameterSpec     currentSpec;        /**         * in the abscence of a standard way of doing it this will do for         * now...         */        protected byte[] engineGetEncoded()         {            ByteArrayOutputStream   bOut = new ByteArrayOutputStream();            DEROutputStream         dOut = new DEROutputStream(bOut);            try            {                ASN1EncodableVector    v = new ASN1EncodableVector();                v.add(new DEROctetString(currentSpec.getDerivationV()));                v.add(new DEROctetString(currentSpec.getEncodingV()));                v.add(new DERInteger(currentSpec.getMacKeySize()));                dOut.writeObject(new DERSequence(v));                dOut.close();            }            catch (IOException e)            {                throw new RuntimeException("Error encoding IESParameters");            }            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 == IESParameterSpec.class)            {                return currentSpec;            }            throw new InvalidParameterSpecException("unknown parameter spec passed to ElGamal parameters object.");        }        protected void engineInit(            AlgorithmParameterSpec paramSpec)             throws InvalidParameterSpecException        {            if (!(paramSpec instanceof IESParameterSpec))            {                throw new InvalidParameterSpecException("IESParameterSpec required to initialise a IES algorithm parameters object");            }            this.currentSpec = (IESParameterSpec)paramSpec;        }        protected void engineInit(            byte[] params)             throws IOException        {            ByteArrayInputStream   bIn = new ByteArrayInputStream(params);            ASN1InputStream        aIn = new ASN1InputStream(bIn);            try            {                ASN1Sequence    s = (ASN1Sequence)aIn.readObject();                this.currentSpec = new IESParameterSpec(                                        ((ASN1OctetString)s.getObjectAt(0)).getOctets(),                                        ((ASN1OctetString)s.getObjectAt(0)).getOctets(),                                        ((DERInteger)s.getObjectAt(0)).getValue().intValue());            }            catch (ClassCastException e)            {                throw new IOException("Not a valid IES Parameter encoding.");            }            catch (ArrayIndexOutOfBoundsException e)            {                throw new IOException("Not a valid IES 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 "IES Parameters";        }    }        public static class PSS        extends JDKAlgorithmParameters    {          PSSParameterSpec     currentSpec;            /**         * Return the PKCS#1 ASN.1 structure RSA-ES-OAEP-params.         */        protected byte[] engineGetEncoded()         {            ByteArrayOutputStream   bOut = new ByteArrayOutputStream();            DEROutputStream         dOut = new DEROutputStream(bOut);            PSSParameterSpec    pssSpec = (PSSParameterSpec)currentSpec;            RSASSAPSSparams     pssP = new RSASSAPSSparams(RSASSAPSSparams.DEFAULT_HASH_ALGORITHM, RSASSAPSSparams.DEFAULT_MASK_GEN_FUNCTION, new DERInteger(pssSpec.getSaltLength()), RSASSAPSSparams.DEFAULT_TRAILER_FIELD);            try            {                dOut.writeObject(pssP);                dOut.close();            }            catch (IOException e)            {                throw new RuntimeException("Error encoding PSSParameters");            }                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 == PSSParameterSpec.class && currentSpec instanceof PSSParameterSpec)            {                return currentSpec;            }                throw new InvalidParameterSpecException("unknown parameter spec passed to PSS parameters object.");        }            protected void engineInit(            AlgorithmParameterSpec paramSpec)             throws InvalidParameterSpecException        {            if (!(paramSpec instanceof PSSParameterSpec))            {                throw new InvalidParameterSpecException("PSSParameterSpec required to initialise an PSS algorithm parameters object");            }                this.currentSpec = (PSSParameterSpec)paramSpec;        }            protected void engineInit(            byte[] params)             throws IOException        {            ByteArrayInputStream   bIn = new ByteArrayInputStream(params);            ASN1InputStream        aIn = new ASN1InputStream(bIn);                try            {                RSASSAPSSparams pssP = new RSASSAPSSparams((ASN1Sequence)aIn.readObject());                    currentSpec = new PSSParameterSpec(                                       pssP.getSaltLength().getValue().intValue());            }            catch (ClassCastException e)            {                throw new IOException("Not a valid PSS Parameter encoding.");            }            catch (ArrayIndexOutOfBoundsException e)            {                throw new IOException("Not a valid PSS 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 "PSS Parameters";        }    }}

⌨️ 快捷键说明

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