jceblockcipher.java

来自「kmlnjlkj nlkjlkjkljl okopokipoipo oipipi」· Java 代码 · 共 1,312 行 · 第 1/3 页

JAVA
1,312
字号
        else if (params instanceof GOST28147ParameterSpec)        {            GOST28147ParameterSpec    gost28147Param = (GOST28147ParameterSpec)params;            param = new ParametersWithSBox(                       new KeyParameter(key.getEncoded()), ((GOST28147ParameterSpec)params).getSbox());            if (gost28147Param.getIV() != null && ivLength != 0)            {                param = new ParametersWithIV(param, gost28147Param.getIV());                ivParam = (ParametersWithIV)param;            }        }        else if (params instanceof RC2ParameterSpec)        {            RC2ParameterSpec    rc2Param = (RC2ParameterSpec)params;            param = new RC2Parameters(key.getEncoded(), ((RC2ParameterSpec)params).getEffectiveKeyBits());            if (rc2Param.getIV() != null && ivLength != 0)            {                param = new ParametersWithIV(param, rc2Param.getIV());                ivParam = (ParametersWithIV)param;            }        }        else if (params instanceof RC5ParameterSpec)        {            RC5ParameterSpec    rc5Param = (RC5ParameterSpec)params;            param = new RC5Parameters(key.getEncoded(), ((RC5ParameterSpec)params).getRounds());            if (baseEngine.getAlgorithmName().startsWith("RC5"))            {                if (baseEngine.getAlgorithmName().equals("RC5-32"))                {                    if (rc5Param.getWordSize() != 32)                    {                        throw new InvalidAlgorithmParameterException("RC5 already set up for a word size of 32 not " + rc5Param.getWordSize() + ".");                    }                }                else if (baseEngine.getAlgorithmName().equals("RC5-64"))                {                    if (rc5Param.getWordSize() != 64)                    {                        throw new InvalidAlgorithmParameterException("RC5 already set up for a word size of 64 not " + rc5Param.getWordSize() + ".");                    }                }            }            else            {                throw new InvalidAlgorithmParameterException("RC5 parameters passed to a cipher that is not RC5.");            }            if ((rc5Param.getIV() != null) && (ivLength != 0))            {                param = new ParametersWithIV(param, rc5Param.getIV());                ivParam = (ParametersWithIV)param;            }        }        else        {            throw new InvalidAlgorithmParameterException("unknown parameter type.");        }        if ((ivLength != 0) && !(param instanceof ParametersWithIV))        {            SecureRandom    ivRandom = random;            if (ivRandom == null)            {                ivRandom = new SecureRandom();            }            if ((opmode == Cipher.ENCRYPT_MODE) || (opmode == Cipher.WRAP_MODE))            {                byte[]  iv = new byte[ivLength];                ivRandom.nextBytes(iv);                param = new ParametersWithIV(param, iv);                ivParam = (ParametersWithIV)param;            }            else if (cipher.getUnderlyingCipher().getAlgorithmName().indexOf("PGPCFB") < 0)            {                throw new InvalidAlgorithmParameterException("no IV set when one expected");            }        }        if (random != null && padded)        {            param = new ParametersWithRandom(param, random);        }        try        {            switch (opmode)            {            case Cipher.ENCRYPT_MODE:            case Cipher.WRAP_MODE:                cipher.init(true, param);                break;            case Cipher.DECRYPT_MODE:            case Cipher.UNWRAP_MODE:                cipher.init(false, param);                break;            default:                throw new InvalidParameterException("unknown opmode " + opmode + " passed");            }        }        catch (Exception e)        {            throw new InvalidKeyException(e.getMessage());        }    }    protected void engineInit(        int                 opmode,        Key                 key,        AlgorithmParameters params,        SecureRandom        random)     throws InvalidKeyException, InvalidAlgorithmParameterException    {        AlgorithmParameterSpec  paramSpec = null;        if (params != null)        {            for (int i = 0; i != availableSpecs.length; i++)            {                try                {                    paramSpec = params.getParameterSpec(availableSpecs[i]);                    break;                }                catch (Exception e)                {                    // try again if possible                }            }            if (paramSpec == null)            {                throw new InvalidAlgorithmParameterException("can't handle parameter " + params.toString());            }        }        engineInit(opmode, key, paramSpec, random);                engineParams = params;    }    protected void engineInit(        int                 opmode,        Key                 key,        SecureRandom        random)         throws InvalidKeyException    {        try        {            engineInit(opmode, key, (AlgorithmParameterSpec)null, random);        }        catch (InvalidAlgorithmParameterException e)        {            throw new InvalidKeyException(e.getMessage());        }    }    protected byte[] engineUpdate(        byte[]  input,        int     inputOffset,        int     inputLen)     {        int     length = cipher.getUpdateOutputSize(inputLen);        if (length > 0)        {                byte[]  out = new byte[length];                int len = cipher.processBytes(input, inputOffset, inputLen, out, 0);                if (len == 0)                {                    return null;                }                else if (len != out.length)                {                    byte[]  tmp = new byte[len];                    System.arraycopy(out, 0, tmp, 0, len);                    return tmp;                }                return out;        }        cipher.processBytes(input, inputOffset, inputLen, null, 0);        return null;    }    protected int engineUpdate(        byte[]  input,        int     inputOffset,        int     inputLen,        byte[]  output,        int     outputOffset)        throws ShortBufferException    {        try        {            return cipher.processBytes(input, inputOffset, inputLen, output, outputOffset);        }        catch (DataLengthException e)        {            throw new ShortBufferException(e.getMessage());        }    }    protected byte[] engineDoFinal(        byte[]  input,        int     inputOffset,        int     inputLen)         throws IllegalBlockSizeException, BadPaddingException    {        int     len = 0;        byte[]  tmp = new byte[engineGetOutputSize(inputLen)];        if (inputLen != 0)        {            len = cipher.processBytes(input, inputOffset, inputLen, tmp, 0);        }        try        {            len += cipher.doFinal(tmp, len);        }        catch (DataLengthException e)        {            throw new IllegalBlockSizeException(e.getMessage());        }        catch (InvalidCipherTextException e)        {            throw new BadPaddingException(e.getMessage());        }        if (len == tmp.length)        {            return tmp;        }        byte[]  out = new byte[len];        System.arraycopy(tmp, 0, out, 0, len);        return out;    }    protected int engineDoFinal(        byte[]  input,        int     inputOffset,        int     inputLen,        byte[]  output,        int     outputOffset)         throws IllegalBlockSizeException, BadPaddingException    {        int     len = 0;        if (inputLen != 0)        {                len = cipher.processBytes(input, inputOffset, inputLen, output, outputOffset);        }        try        {            return (len + cipher.doFinal(output, outputOffset + len));        }        catch (DataLengthException e)        {            throw new IllegalBlockSizeException(e.getMessage());        }        catch (InvalidCipherTextException e)        {            throw new BadPaddingException(e.getMessage());        }    }    private boolean isAEADModeName(        String modeName)    {        return "CCM".equals(modeName) || "EAX".equals(modeName) || "GCM".equals(modeName);    }    /*     * The ciphers that inherit from us.     */    /**     * DES     */    static public class DES        extends JCEBlockCipher    {        public DES()        {            super(new DESEngine());        }    }    /**     * DESCBC     */    static public class DESCBC        extends JCEBlockCipher    {        public DESCBC()        {            super(new CBCBlockCipher(new DESEngine()), 64);        }    }    /**     * DESede     */    static public class DESede        extends JCEBlockCipher    {        public DESede()        {            super(new DESedeEngine());        }    }    /**     * DESedeCBC     */    static public class DESedeCBC        extends JCEBlockCipher    {        public DESedeCBC()        {            super(new CBCBlockCipher(new DESedeEngine()), 64);        }    }    /**     *  GOST28147     */    static public class GOST28147        extends JCEBlockCipher    {        public GOST28147()        {            super(new GOST28147Engine());        }    }        static public class GOST28147cbc        extends JCEBlockCipher    {        public GOST28147cbc()        {            super(new CBCBlockCipher(new GOST28147Engine()), 64);        }    }        /**     * SKIPJACK     */    static public class Skipjack        extends JCEBlockCipher    {        public Skipjack()        {            super(new SkipjackEngine());        }    }    /**     * Blowfish     */    static public class Blowfish        extends JCEBlockCipher    {        public Blowfish()        {            super(new BlowfishEngine());        }    }    /**     * Twofish     */    static public class Twofish        extends JCEBlockCipher    {        public Twofish()        {            super(new TwofishEngine());        }    }    /**     * RC2     */    static public class RC2        extends JCEBlockCipher    {        public RC2()        {            super(new RC2Engine());        }    }    /**     * RC2CBC     */    static public class RC2CBC        extends JCEBlockCipher    {        public RC2CBC()        {            super(new CBCBlockCipher(new RC2Engine()), 64);        }    }    /**     * RC5     */    static public class RC5        extends JCEBlockCipher    {        public RC5()        {            super(new RC532Engine());        }    }    /**     * RC564     */    static public class RC564

⌨️ 快捷键说明

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