📄 blockciphertest.java
字号:
System.arraycopy(seed, 0, bytes, offset, seed.length); offset += seed.length; } System.arraycopy(seed, 0, bytes, offset, bytes.length - offset); } } public String getName() { return "BlockCipher"; } public void test( String algorithm, byte[] input, byte[] output) { Key key = null; KeyGenerator keyGen; SecureRandom rand; Cipher in = null; Cipher out = null; CipherInputStream cIn; CipherOutputStream cOut; ByteArrayInputStream bIn; ByteArrayOutputStream bOut; rand = new FixedSecureRandom(); try { String baseAlgorithm; int index = algorithm.indexOf('/'); if (index > 0) { baseAlgorithm = algorithm.substring(0, index); } else { baseAlgorithm = algorithm; } if (baseAlgorithm.equals("IDEA") & noIDEA()) { return; } keyGen = KeyGenerator.getInstance(baseAlgorithm, "BC"); if (!keyGen.getAlgorithm().equals(baseAlgorithm)) { fail("wrong key generator returned!"); } keyGen.init(rand); key = keyGen.generateKey(); in = Cipher.getInstance(algorithm, "BC"); out = Cipher.getInstance(algorithm, "BC"); if (!in.getAlgorithm().startsWith(baseAlgorithm)) { fail("wrong cipher returned!"); } if (algorithm.startsWith("RC2")) { out.init(Cipher.ENCRYPT_MODE, key, rc2Spec, rand); } else if (algorithm.startsWith("RC5")) { if (algorithm.startsWith("RC5-64")) { out.init(Cipher.ENCRYPT_MODE, key, rc564Spec, rand); } else { out.init(Cipher.ENCRYPT_MODE, key, rc5Spec, rand); } } else { out.init(Cipher.ENCRYPT_MODE, key, rand); } } catch (Exception e) { fail("" + algorithm + " failed initialisation - " + e.toString(), e); } // // grab the iv if there is one // try { if (algorithm.startsWith("RC2")) { in.init(Cipher.DECRYPT_MODE, key, rc2Spec); } else if (algorithm.startsWith("RC5")) { if (algorithm.startsWith("RC5-64")) { in.init(Cipher.DECRYPT_MODE, key, rc564Spec, rand); } else { in.init(Cipher.DECRYPT_MODE, key, rc5Spec, rand); } } else { byte[] iv; iv = out.getIV(); if (iv != null) { try { byte[] nIv = new byte[iv.length - 1]; in.init(Cipher.DECRYPT_MODE, key, new IvParameterSpec(nIv)); fail("failed to pick up short IV"); } catch (InvalidAlgorithmParameterException e) { // 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 boolean noIDEA() { try { Cipher.getInstance("IDEA", "BC"); return false; } catch (Exception e) { return true; } } 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);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -