private void generator_init(String cipher) { cipher_ctx = Util.getCipherByName(cipher); output_buffer = new byte[cipher_ctx.getBlockSize() / 8]; counter = new byte[cipher_ctx.getBlockSize() / 8]; allZeroString = new byte[cipher_ctx.getBlockSize() / 8]; tmp = new byte[cipher_ctx.getKeySize() / 8]; fetch_counter = output_buffer.length; }
private final void generateOutput() { counterInc(); output_buffer = new byte[counter.length]; cipher_ctx.encipher(counter, output_buffer); if (output_count++ > Pg) { output_count = 0; nextBytes(tmp); rekey(tmp); } }
// @SuppressWarnings("rawtypes") public static BlockCipher createCipher(String type, boolean encrypt, byte[] key, byte[] iv) { try { CipherEntry ce = getEntry(type); Class<?> cc = Class.forName(ce.cipherClass); BlockCipher bc = (BlockCipher) cc.newInstance(); if (type.endsWith("-cbc")) { bc.init(encrypt, key); return new CBCMode(bc, iv, encrypt); } else if (type.endsWith("-ctr")) { bc.init(true, key); return new CTRMode(bc, iv, encrypt); } throw new IllegalArgumentException("Cannot instantiate " + type); } catch (ClassNotFoundException e) { throw new IllegalArgumentException("Cannot instantiate " + type, e); } catch (InstantiationException e) { throw new IllegalArgumentException("Cannot instantiate " + type, e); } catch (IllegalAccessException e) { throw new IllegalArgumentException("Cannot instantiate " + type, e); } }
private void rekey(byte[] key) { cipher_ctx.initialize(key); counter = new byte[allZeroString.length]; cipher_ctx.encipher(allZeroString, counter); Arrays.fill(key, (byte) 0); }