byte[] out = new byte[cipher.length]; while(i < cipher.length){ ctrEngine.processBlock(cipher, i, out, i); i += engine.getBlockSize(); if (cipher.length - i < engine.getBlockSize()) byte[] tmpBlock = new byte[16]; System.arraycopy(cipher, i, tmpBlock, 0, cipher.length - i); ctrEngine.processBlock(tmpBlock, 0, tmpBlock, 0); System.arraycopy(tmpBlock, 0, out, i, cipher.length - i);
@Test // basic encryption/decryption public void test11() throws Throwable { byte[] keyBytes = sha3("...".getBytes()); log.info("key: {}", Hex.toHexString(keyBytes)); byte[] ivBytes = new byte[16]; byte[] payload = Hex.decode("22400891000000000000000000000000"); KeyParameter key = new KeyParameter(keyBytes); ParametersWithIV params = new ParametersWithIV(key, new byte[16]); AESEngine engine = new AESEngine(); SICBlockCipher ctrEngine = new SICBlockCipher(engine); ctrEngine.init(true, params); byte[] cipher = new byte[16]; ctrEngine.processBlock(payload, 0, cipher, 0); log.info("cipher: {}", Hex.toHexString(cipher)); byte[] output = new byte[cipher.length]; ctrEngine.init(false, params); ctrEngine.processBlock(cipher, 0, output, 0); assertEquals(Hex.toHexString(output), Hex.toHexString(payload)); log.info("original: {}", Hex.toHexString(payload)); }
@Test // big packet encryption public void test12() throws Throwable { AESEngine engine = new AESEngine(); SICBlockCipher ctrEngine = new SICBlockCipher(engine); byte[] keyBytes = Hex.decode("a4627abc2a3c25315bff732cb22bc128f203912dd2a840f31e66efb27a47d2b1"); byte[] ivBytes = new byte[16]; byte[] payload = Hex.decode("0109efc76519b683d543db9d0991bcde99cc9a3d14b1d0ecb8e9f1f66f31558593d746eaa112891b04ef7126e1dce17c9ac92ebf39e010f0028b8ec699f56f5d0c0d00"); byte[] cipherText = Hex.decode("f9fab4e9dd9fc3e5d0d0d16da254a2ac24df81c076e3214e2c57da80a46e6ae4752f4b547889fa692b0997d74f36bb7c047100ba71045cb72cfafcc7f9a251762cdf8f"); KeyParameter key = new KeyParameter(keyBytes); ParametersWithIV params = new ParametersWithIV(key, ivBytes); ctrEngine.init(true, params); byte[] in = payload; byte[] out = new byte[in.length]; int i = 0; while(i < in.length){ ctrEngine.processBlock(in, i, out, i); i += engine.getBlockSize(); if (in.length - i < engine.getBlockSize()) break; } // process left bytes if (in.length - i > 0){ byte[] tmpBlock = new byte[16]; System.arraycopy(in, i, tmpBlock, 0, in.length - i); ctrEngine.processBlock(tmpBlock, 0, tmpBlock, 0); System.arraycopy(tmpBlock, 0, out, i, in.length - i); } log.info("cipher: {}", Hex.toHexString(out)); assertEquals(Hex.toHexString(cipherText), Hex.toHexString(out)); }
private int process(byte b, byte[] out, int outOff) { bufBlock[bufOff++] = b; if (bufOff == bufBlock.length) { int size; if (forEncryption) { size = cipher.processBlock(bufBlock, 0, out, outOff); mac.update(out, outOff, blockSize); } else { mac.update(bufBlock, 0, blockSize); size = cipher.processBlock(bufBlock, 0, out, outOff); } bufOff = blockSize; System.arraycopy(bufBlock, blockSize, bufBlock, 0, blockSize); return size; } return 0; }
size = cipher.processBlock(bufBlock, 0, out, outOff); size = cipher.processBlock(bufBlock, 0, out, outOff);
cipher.processBlock(bufBlock, 0, tmp, 0); cipher.processBlock(bufBlock, blockSize, tmp, blockSize); cipher.processBlock(bufBlock, 0, tmp, 0); cipher.processBlock(bufBlock, blockSize, tmp, blockSize);
byte[] out = new byte[cipher.length]; while (i < cipher.length) { ctrEngine.processBlock(cipher, i, out, i); i += engine.getBlockSize(); if (cipher.length - i < engine.getBlockSize()) byte[] tmpBlock = new byte[16]; System.arraycopy(cipher, i, tmpBlock, 0, cipher.length - i); ctrEngine.processBlock(tmpBlock, 0, tmpBlock, 0); System.arraycopy(tmpBlock, 0, out, i, cipher.length - i);
byte[] out = new byte[cipher.length]; while (i < cipher.length) { ctrEngine.processBlock(cipher, i, out, i); i += engine.getBlockSize(); if (cipher.length - i < engine.getBlockSize()) { byte[] tmpBlock = new byte[16]; System.arraycopy(cipher, i, tmpBlock, 0, cipher.length - i); ctrEngine.processBlock(tmpBlock, 0, tmpBlock, 0); System.arraycopy(tmpBlock, 0, out, i, cipher.length - i);
byte[] out = new byte[cipher.length]; while(i < cipher.length){ ctrEngine.processBlock(cipher, i, out, i); i += engine.getBlockSize(); if (cipher.length - i < engine.getBlockSize()) byte[] tmpBlock = new byte[16]; System.arraycopy(cipher, i, tmpBlock, 0, cipher.length - i); ctrEngine.processBlock(tmpBlock, 0, tmpBlock, 0); System.arraycopy(tmpBlock, 0, out, i, cipher.length - i);
cipher.processBlock(bufBlock, 0, tmp, 0); cipher.processBlock(bufBlock, 0, tmp, 0);
@Test // basic encryption/decryption public void test11() { byte[] keyBytes = sha3("...".getBytes()); log.info("key: {}", Hex.toHexString(keyBytes)); byte[] payload = Hex.decode("22400891000000000000000000000000"); KeyParameter key = new KeyParameter(keyBytes); ParametersWithIV params = new ParametersWithIV(key, new byte[16]); AESEngine engine = new AESEngine(); SICBlockCipher ctrEngine = new SICBlockCipher(engine); ctrEngine.init(true, params); byte[] cipher = new byte[16]; ctrEngine.processBlock(payload, 0, cipher, 0); log.info("cipher: {}", Hex.toHexString(cipher)); byte[] output = new byte[cipher.length]; ctrEngine.init(false, params); ctrEngine.processBlock(cipher, 0, output, 0); assertEquals(Hex.toHexString(output), Hex.toHexString(payload)); log.info("original: {}", Hex.toHexString(payload)); }
@Test // big packet encryption public void test12() { AESEngine engine = new AESEngine(); SICBlockCipher ctrEngine = new SICBlockCipher(engine); byte[] keyBytes = Hex.decode("a4627abc2a3c25315bff732cb22bc128f203912dd2a840f31e66efb27a47d2b1"); byte[] ivBytes = new byte[16]; byte[] payload = Hex.decode("0109efc76519b683d543db9d0991bcde99cc9a3d14b1d0ecb8e9f1f66f31558593d746eaa112891b04ef7126e1dce17c9ac92ebf39e010f0028b8ec699f56f5d0c0d00"); byte[] cipherText = Hex.decode("f9fab4e9dd9fc3e5d0d0d16da254a2ac24df81c076e3214e2c57da80a46e6ae4752f4b547889fa692b0997d74f36bb7c047100ba71045cb72cfafcc7f9a251762cdf8f"); KeyParameter key = new KeyParameter(keyBytes); ParametersWithIV params = new ParametersWithIV(key, ivBytes); ctrEngine.init(true, params); byte[] out = new byte[payload.length]; int i = 0; while (i < payload.length) { ctrEngine.processBlock(payload, i, out, i); i += engine.getBlockSize(); if (payload.length - i < engine.getBlockSize()) { break; } } // process left bytes if (payload.length - i > 0) { byte[] tmpBlock = new byte[16]; System.arraycopy(payload, i, tmpBlock, 0, payload.length - i); ctrEngine.processBlock(tmpBlock, 0, tmpBlock, 0); System.arraycopy(tmpBlock, 0, out, i, payload.length - i); } log.info("cipher: {}", Hex.toHexString(out)); assertEquals(Hex.toHexString(cipherText), Hex.toHexString(out)); }