blockciphertest.java
来自「bouncycastle 是一个JAVA安全提供者」· Java 代码 · 共 827 行 · 第 1/2 页
JAVA
827 行
{ // ignore - this is what we want... } IvParameterSpec spec; spec = new IvParameterSpec(iv); in.init(Cipher.DECRYPT_MODE, key, spec); } else { in.init(Cipher.DECRYPT_MODE, key); } } } catch (Exception e) { fail("" + algorithm + " failed initialisation - " + e.toString()); } // // encryption pass // bOut = new ByteArrayOutputStream(); cOut = new CipherOutputStream(bOut, out); try { for (int i = 0; i != input.length / 2; i++) { cOut.write(input[i]); } cOut.write(input, input.length / 2, input.length - input.length / 2); cOut.close(); } catch (IOException e) { fail("" + algorithm + " failed encryption - " + e.toString()); } byte[] bytes; bytes = bOut.toByteArray(); if (!areEqual(bytes, output)) { fail("" + algorithm + " failed encryption - expected " + new String(Hex.encode(output)) + " got " + new String(Hex.encode(bytes))); } // // decryption pass // bIn = new ByteArrayInputStream(bytes); cIn = new CipherInputStream(bIn, in); try { DataInputStream dIn = new DataInputStream(cIn); bytes = new byte[input.length]; for (int i = 0; i != input.length / 2; i++) { bytes[i] = (byte)dIn.read(); } dIn.readFully(bytes, input.length / 2, bytes.length - input.length / 2); } catch (Exception e) { fail("" + algorithm + " failed decryption - " + e.toString()); } if (!areEqual(bytes, input)) { fail("" + algorithm + " failed decryption - expected " + new String(Hex.encode(input)) + " got " + new String(Hex.encode(bytes))); } } private void testExceptions() { SecretKeyFactory skF = null; try { skF = SecretKeyFactory.getInstance("DESede", "BC"); } catch (Exception e) { fail("unexpected exception.", e); } KeySpec ks = null; SecretKey secKey = null; byte[] bb = new byte[24]; try { skF.getKeySpec(null, null); fail("failed exception test - no exception thrown"); } catch (InvalidKeySpecException e) { // ignore okay } catch (Exception e) { fail("failed exception test.", e); } try { ks = (KeySpec)new DESedeKeySpec(bb); skF.getKeySpec(null, ks.getClass()); fail("failed exception test - no exception thrown"); } catch (InvalidKeySpecException e) { // ignore okay; } catch (Exception e) { fail("failed exception test.", e); } try { skF.getKeySpec(secKey, null); } catch (InvalidKeySpecException e) { // ignore okay } catch (Exception e) { fail("failed exception test.", e); } try { KeyGenerator kg = KeyGenerator.getInstance("DESede", "BC"); try { kg.init(Integer.MIN_VALUE, new SecureRandom()); fail("failed exception test - no exception thrown"); } catch (InvalidParameterException e) { // ignore okay } catch (Exception e) { fail("failed exception test.", e); } } catch (Exception e) { fail("unexpected exception.", e); } try { skF = SecretKeyFactory.getInstance("DESede", "BC"); try { skF.translateKey(null); 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 { 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 } 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 + =
减小字号Ctrl + -
显示快捷键?