public void init(byte[] iv) throws IllegalArgumentException { init(iv, 0, iv.length); }
public byte processByte(byte in) { if(blockOffset == counterOut.length) { processBlock(); } return (byte) (in ^ counterOut[blockOffset++]); }
CTRBlockCipher cipher = new CTRBlockCipher(aes); cipher.init(hash, 0, 16); byte[] plaintext = new byte[data.length]; cipher.processBytes(data, 0, data.length, plaintext, 0); byte[] lengthBytes = new byte[2]; cipher.processBytes(headers, hash.length+2, 2, lengthBytes, 0); int size = ((lengthBytes[0] & 0xff) << 8) + (lengthBytes[1] & 0xff); if((size > 32768) || (size < 0)) {
CTRBlockCipher ctr = new CTRBlockCipher(aes); ctr.init(hash, 0, 16); System.arraycopy(hash, 0, header, 2, hash.length); byte[] cdata = new byte[data.length]; ctr.processBytes(data, 0, data.length, cdata, 0); ctr.processBytes(tmpLen, 0, 2, header, hash.length+2);
private void checkNIST(int bits, byte[] key, byte[] iv, byte[] plaintext, byte[] ciphertext) throws UnsupportedCipherException, NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException, InvalidAlgorithmParameterException, IllegalBlockSizeException, BadPaddingException { // First test it with JCA. if (TEST_JCA) { SecretKeySpec k = new SecretKeySpec(key, "AES"); Cipher c = Cipher.getInstance("AES/CTR/NOPADDING", Rijndael.AesCtrProvider); c.init(Cipher.ENCRYPT_MODE, k, new IvParameterSpec(iv)); byte[] output = c.doFinal(plaintext); assertTrue(Arrays.equals(output, ciphertext)); } Rijndael cipher = new Rijndael(bits, 128); cipher.initialize(key); CTRBlockCipher ctr = new CTRBlockCipher(cipher); ctr.init(iv); byte[] output = new byte[plaintext.length]; ctr.processBytes(plaintext, 0, plaintext.length, output, 0); assertTrue(Arrays.equals(output, ciphertext)); }
/** Initialize the cipher with an IV. Must only be called once for any * given IV! * @param iv The initialization vector. This is the initial value of * the plaintext counter. The plaintext is XORed with a sequence of * bytes consisting of the encryption of successive values of the counter. * @throws IllegalArgumentException If the IV length is wrong. */ public void init(byte[] iv, int offset, int length) throws IllegalArgumentException { if(length != IV.length) throw new IllegalArgumentException(); System.arraycopy(iv, offset, IV, 0, IV.length); System.arraycopy(IV, 0, counter, 0, counter.length); processBlock(); }
CTRBlockCipher ctr = new CTRBlockCipher(cipher); ctr.init(iv); byte[] ciphertext = new byte[plaintext.length]; ctr.processBytes(plaintext, 0, plaintext.length, ciphertext, 0); ctr = new CTRBlockCipher(cipher); ctr.init(iv); byte[] finalPlaintext = new byte[plaintext.length]; ctr.processBytes(ciphertext, 0, ciphertext.length, finalPlaintext, 0); assertTrue(Arrays.equals(finalPlaintext, plaintext)); if(TEST_JCA) { ctr = new CTRBlockCipher(cipher); ctr.init(iv); byte[] output = new byte[plaintext.length]; MersenneTwister random = new MersenneTwister(mt.nextLong()); int max = plaintext.length - ptr; int count = (max == 1) ? 1 : (random.nextInt(max - 1) + 1); ctr.processBytes(plaintext, ptr, count, output, ptr); ptr += count;
output[offsetOut++] = (byte) (input[offsetIn++] ^ counterOut[blockOffset++]); if(length == 0) return; processBlock(); while (blockOffset < blockSize) output[offsetOut++] = (byte) (input[offsetIn++] ^ counterOut[blockOffset++]); processBlock();
CTRBlockCipher ctr = new CTRBlockCipher(cipher); ctr.init(iv); byte[] output = new byte[plaintext.length]; MersenneTwister random = new MersenneTwister(seed); int max = plaintext.length - ptr; int count = (max == 1) ? 1 : (random.nextInt(max - 1) + 1); ctr.processBytes(plaintext, ptr, count, output, ptr); ptr += count;