protected BlockCipher getCipher() { if (cipher == null) { cipher = new AES256(); cipher.initialize(BlockCipher.Direction.ENCRYPT, this); } return cipher; }
/** * Whether the engine has been initialized properly. */ protected boolean initialized() { return this.key != null && this.key.length == getKeySize(); }
/** * Test {@link AES256} wrapper for decrypting. * */ @org.junit.Test public void decrypt() throws EncryptionError { AES256 engine = new AES256(); engine.initialize(BlockCipher.Direction.DECRYPT, new SymmetricKey(key1)); byte[] result = engine.transformBlock(ciphertext1); assertArrayEquals(plaintext1, result); } }
/** * Encrypt/decrypt source block and return processed block * * @param block source block * @return transformed source block * @throws EncryptionError if key or block has wrong size */ @Override public byte[] transformBlock(byte[] block) throws EncryptionError { if (!initialized()) { throw new EncryptionError("Not initialized with proper key"); } else { byte[] buf = new byte[getBlockSize()]; aesEngine.processBlock(block, 0, buf, 0); return buf; } } }
@Override public void initialize(Direction direction, SymmetricKey key) { this.key = key.getKey(); if (initialized()) { CipherParameters params = new KeyParameter(this.key); aesEngine.init(direction == Direction.ENCRYPT, params); } }
/** * Test {@link AES256} wrapper for decrypting. * */ @org.junit.Test public void encrypt() throws EncryptionError { AES256 engine = new AES256(); engine.initialize(BlockCipher.Direction.ENCRYPT, new SymmetricKey(key1)); byte[] result = engine.transformBlock(plaintext1); assertArrayEquals(ciphertext1, result); } /**