public String encrypt(String plaintextBase64){
  Base64.Decoder decoder=getDecoder();
  final byte[] plaintext=decoder.decode(plaintextBase64);
  byte[] nonce=new byte[NONCE_BYTES];
  random.nextBytes(nonce);
  byte[] ciphertext=gcm(Mode.ENCRYPT,derivationInfo,nonce,plaintext);
  Crypted crypted=Crypted.of(derivationInfo,ciphertext,nonce);
  String encryptedJson;
  try {
    encryptedJson=MAPPER.writeValueAsString(crypted);
  }
 catch (  JsonProcessingException e) {
    throw Throwables.propagate(e);
  }
  if (!Subtles.secureCompare(decoder.decode(decrypt(encryptedJson)),plaintext)) {
    logger.warn("Decryption of (just encrypted) data does not match original! [name={}]",derivationInfo);
  }
  return encryptedJson;
}

public String encrypt ( String plaintextBase64 ) { Base64 . Decoder decoder = getDecoder ( ) ; final byte [ ] plaintext = decoder . decode ( plaintextBase64 ) ; byte [ ] nonce = new byte [ NONCE_BYTES ] ; random . nextBytes ( nonce ) ; byte [ ] ciphertext = gcm ( Mode . ENCRYPT , derivationInfo , nonce , plaintext ) ; Crypted crypted = Crypted . of ( derivationInfo , ciphertext , nonce ) ; String encryptedJson ; try { encryptedJson = MAPPER . writeValueAsString ( crypted ) ; } catch ( JsonProcessingException e ) { throw Throwables . propagate ( e ) ; } if ( ! Subtles . secureCompare ( decoder . decode ( decrypt ( encryptedJson ) ) , plaintext ) ) { logger . warn ( "Decryption of (just encrypted) data does not match original! [name={}]" , derivationInfo ) ; } return encryptedJson ; }
encrypts content under a derived key .
