⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 blockciphertest.java

📁 kmlnjlkj nlkjlkjkljl okopokipoipo oipipipo i
💻 JAVA
📖 第 1 页 / 共 3 页
字号:
                fail("failed exception test - no exception thrown");            }            catch (InvalidKeyException e)            {                // ignore okay            }            catch (Exception e)            {                fail("failed exception test.", e);            }        }        catch (Exception e)        {            fail("unexpected exception.", e);        }                try        {            byte[] rawDESKey = { (byte)128, (byte)131, (byte)133, (byte)134,                    (byte)137, (byte)138, (byte)140, (byte)143 };            SecretKeySpec cipherKey = new SecretKeySpec(rawDESKey, "DES");            Cipher cipher = Cipher.getInstance("DES/CBC/NoPadding", "BC");                        try            {                // According specification engineInit(int opmode, Key key,                // SecureRandom random) throws InvalidKeyException if this                // cipher is being                // initialized for decryption and requires algorithm parameters                // that cannot be determined from the given key                cipher.init(Cipher.DECRYPT_MODE, cipherKey, (SecureRandom)null);                                fail("failed exception test - no InvalidKeyException thrown");            }            catch (InvalidKeyException e)            {                // ignore            }        }        catch (Exception e)        {            fail("unexpected exception.", e);        }        try        {            byte[] rawDESKey = { -128, -125, -123, -122, -119, -118 };            SecretKeySpec cipherKey = new SecretKeySpec(rawDESKey, "DES");            Cipher cipher = Cipher.getInstance("DES/ECB/NoPadding", "BC");            try            {                // According specification engineInit(int opmode, Key key,                // SecureRandom random) throws InvalidKeyException if the given                // key is inappropriate for initializing this cipher                cipher.init(Cipher.ENCRYPT_MODE, cipherKey);                                fail("failed exception test - no InvalidKeyException thrown");            }            catch (InvalidKeyException e)            {                // ignore            }        }        catch (Exception e)        {            fail("unexpected exception.", e);        }        try        {            byte[] rawDESKey = { -128, -125, -123, -122, -119, -118, -117, -115, -114 };            SecretKeySpec cipherKey = new SecretKeySpec(rawDESKey, "DES");            Cipher cipher = Cipher.getInstance("DES/ECB/NoPadding", "BC");            try            {                // According specification engineInit(int opmode, Key key,                // SecureRandom random) throws InvalidKeyException if the given                // key is inappropriate for initializing this cipher                cipher.init(Cipher.ENCRYPT_MODE, cipherKey);                                fail("failed exception test - no InvalidKeyException thrown");            }            catch (InvalidKeyException e)            {                // ignore            }        }        catch (Exception e)        {            fail("unexpected exception.", e);        }                try        {            byte[] rawDESKey = { (byte)128, (byte)131, (byte)133, (byte)134,                    (byte)137, (byte)138, (byte)140, (byte)143 };            SecretKeySpec cipherKey = new SecretKeySpec(rawDESKey, "DES");            Cipher ecipher = Cipher.getInstance("DES/ECB/PKCS5Padding", "BC");            ecipher.init(Cipher.ENCRYPT_MODE, cipherKey);            byte[] cipherText = new byte[0];            try            {                // According specification Method engineUpdate(byte[] input,                // int inputOffset, int inputLen, byte[] output, int                // outputOffset)                // throws ShortBufferException - if the given output buffer is                // too                // small to hold the result                ecipher.update(new byte[20], 0, 20, cipherText);                                fail("failed exception test - no ShortBufferException thrown");            }            catch (ShortBufferException e)            {                // ignore            }        }        catch (Exception e)        {            fail("unexpected exception.", e);        }        try        {            KeyGenerator keyGen = KeyGenerator.getInstance("DES", "BC");            keyGen.init((SecureRandom)null);            // According specification engineGenerateKey() doesn't throw any exceptions.            SecretKey key = keyGen.generateKey();            if (key == null)            {                fail("key is null!");            }        }        catch (Exception e)        {            fail("unexpected exception.", e);        }        try        {            AlgorithmParameters algParams = AlgorithmParameters.getInstance("DES", "BC");                        algParams.init(new IvParameterSpec(new byte[8]));            // According specification engineGetEncoded() returns            // the parameters in their primary encoding format. The primary            // encoding            // format for parameters is ASN.1, if an ASN.1 specification for            // this type            // of parameters exists.            byte[] iv = algParams.getEncoded();                        if (iv.length != 10)            {                fail("parameters encoding wrong length - "  + iv.length);            }        }        catch (Exception e)        {            fail("unexpected exception.", e);        }        try        {            try            {                AlgorithmParameters algParams = AlgorithmParameters.getInstance("DES", "BC");                    byte[] encoding = new byte[10];                encoding[0] = 3;                encoding[1] = 8;                    // According specification engineInit(byte[] params, String format)                // throws                // IOException on decoding errors, but BC throws ClassCastException.                algParams.init(encoding, "ASN.1");                    fail("failed exception test - no IOException thrown");            }            catch (IOException e)            {                // okay            }                        try            {                Cipher c = Cipher.getInstance("DES", "BC");                    Key k = new PublicKey()                {                    public String getAlgorithm()                    {                        return "STUB";                    }                    public String getFormat()                    {                        return null;                    }                    public byte[] getEncoded()                    {                        return null;                    }                                    };                    c.init(Cipher.ENCRYPT_MODE, k);                    fail("failed exception test - no InvalidKeyException thrown for public key");            }            catch (InvalidKeyException e)            {                // okay            }                        try            {                Cipher c = Cipher.getInstance("DES", "BC");                    Key k = new PrivateKey()                {                    public String getAlgorithm()                    {                        return "STUB";                    }                    public String getFormat()                    {                        return null;                    }                    public byte[] getEncoded()                    {                        return null;                    }                                    };                    c.init(Cipher.DECRYPT_MODE, k);                    fail("failed exception test - no InvalidKeyException thrown for private key");            }            catch (InvalidKeyException e)            {                // okay            }        }        catch (Exception e)        {            fail("unexpected exception.", e);        }    }        public void performTest()    {        for (int i = 0; i != cipherTests1.length; i += 2)        {            test(cipherTests1[i], input1, Hex.decode(cipherTests1[i + 1]));        }        for (int i = 0; i != cipherTests2.length; i += 2)        {            test(cipherTests2[i], input2, Hex.decode(cipherTests2[i + 1]));        }        //        // check for less than a block        //        try        {            Cipher c = Cipher.getInstance("AES/CTS/NoPadding", "BC");                        c.init(Cipher.ENCRYPT_MODE, new SecretKeySpec(new byte[16], "AES"));                        c.doFinal(new byte[4]);                        fail("CTS failed to throw exception");        }        catch (Exception e)        {            if (!(e instanceof IllegalBlockSizeException))            {                fail("CTS exception test - " + e, e);            }        }                testExceptions();    }    public static void main(        String[]    args)    {        Security.addProvider(new BouncyCastleProvider());        runTest(new BlockCipherTest());    }}

⌨️ 快捷键说明

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