@Override public EncryptedPrivateKey encrypt(byte[] plainBytes,KeyParameter aesKey) throws KeyCrypterException {
  checkNotNull(plainBytes);
  checkNotNull(aesKey);
  try {
    byte[] iv=new byte[BLOCK_LENGTH];
    secureRandom.nextBytes(iv);
    ParametersWithIV keyWithIv=new ParametersWithIV(aesKey,iv);
    BufferedBlockCipher cipher=new PaddedBufferedBlockCipher(new CBCBlockCipher(new AESFastEngine()));
    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 EncryptedPrivateKey(iv,Arrays.copyOf(encryptedBytes,length1 + length2));
  }
 catch (  Exception e) {
    throw new KeyCrypterException("Could not encrypt bytes.",e);
  }
}

@ Override public EncryptedPrivateKey encrypt ( byte [ ] plainBytes , KeyParameter aesKey ) throws KeyCrypterException { checkNotNull ( plainBytes ) ; checkNotNull ( aesKey ) ; try { byte [ ] iv = new byte [ BLOCK_LENGTH ] ; secureRandom . nextBytes ( iv ) ; ParametersWithIV keyWithIv = new ParametersWithIV ( aesKey , iv ) ; BufferedBlockCipher cipher = new PaddedBufferedBlockCipher ( new CBCBlockCipher ( new AESFastEngine ( ) ) ) ; 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 EncryptedPrivateKey ( 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 .
