blockciphertest.java
来自「内容:基于jdk1.4的加密算法的具体实现」· Java 代码 · 共 898 行 · 第 1/3 页
JAVA
898 行
int offset = 0; while ((offset + seed.length) < bytes.length) { 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; } keyGen = KeyGenerator.getInstance(baseAlgorithm, "BC"); keyGen.init(rand); key = keyGen.generateKey(); in = Cipher.getInstance(algorithm, "BC"); out = Cipher.getInstance(algorithm, "BC"); 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()); } // // 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 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) {
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?