@ Override public EncryptedData encrypt ( byte [ ] plainBytes , KeyParameter aesKey ) throws KeyCrypterException { checkNotNull ( plainBytes ) ; checkNotNull ( aesKey ) ; try { // Generate iv - each encryption call has a different iv. byte [ ] iv = new byte [ BLOCK_LENGTH ] ; secureRandom . nextBytes ( iv ) ; ParametersWithIV keyWithIv = new ParametersWithIV ( aesKey , iv ) ; // Encrypt using AES. BufferedBlockCipher cipher = new PaddedBufferedBlockCipher ( new CBCBlockCipher ( new AESEngine ( ) ) ) ; cipher . init ( true , keyWithIv ) ; byte [ ] encryptedBytes = new byte [ cipher . getOutputSize ( plainBytes . length ) ] ; final int length1 = cipher . processBytes ( plainBytes , 0 , plainBytes . length , encryptedBytes , 0 ) ; final int length2 = cipher . doFinal ( encryptedBytes , length1 ) ; return new EncryptedData ( iv , Arrays . copyOf ( encryptedBytes , length1 + length2 ) ) ; } catch ( Exception e ) { throw new KeyCrypterException ( "Could not encrypt bytes." , e ) ; } }
Password based encryption using AES - CBC 256 bits .