public String encrypt(EncryptData encryptData){
  GsonBuilder gsonBuilder=new GsonBuilder();
  gsonBuilder.registerTypeAdapter(EncryptData.class,new EncryptDataJsonSerializer());
  Gson gson=gsonBuilder.create();
  String payload=gson.toJson(encryptData);
  try {
    String protectedHeader=createProtectedHeader();
    String encodededProtectedHeader=encryptUtil.base64UrlEncode(protectedHeader.getBytes());
    byte[] contentEncryptionKey=encryptUtil.generateSecureRandomBytes(CONTENT_ENCRYPTION_KEY_SIZE);
    byte[] encryptedContentEncryptionKey=encryptUtil.encryptContentEncryptionKey(contentEncryptionKey,(RSAPublicKey)publicKeyResponse.getPublicKey());
    String encodedEncryptedContentEncryptionKey=encryptUtil.base64UrlEncode(encryptedContentEncryptionKey);
    byte[] macKey=Arrays.copyOf(contentEncryptionKey,CONTENT_ENCRYPTION_KEY_SIZE / 2);
    byte[] encKey=Arrays.copyOfRange(contentEncryptionKey,CONTENT_ENCRYPTION_KEY_SIZE / 2,CONTENT_ENCRYPTION_KEY_SIZE);
    byte[] initializationVector=encryptUtil.generateSecureRandomBytes(INITIALIZATION_VECTOR);
    String encodededinitializationVector=encryptUtil.base64UrlEncode(initializationVector);
    byte[] cipherText=encryptUtil.encryptPayload(payload,encKey,initializationVector);
    String encodedCipherText=encryptUtil.base64UrlEncode(cipherText);
    byte[] additionalAuthenticatedData=encodededProtectedHeader.getBytes(Charset.forName("UTF-8"));
    byte[] al=calculateAdditionalAuthenticatedDataLength(additionalAuthenticatedData);
    byte[] calculatedHmac=calculateHMAC(macKey,additionalAuthenticatedData,initializationVector,cipherText,al);
    byte[] authenticationTag=Arrays.copyOf(calculatedHmac,calculatedHmac.length / 2);
    String encodedAuthenticationTag=encryptUtil.base64UrlEncode(authenticationTag);
    return buildCompactRespresentation(encodededProtectedHeader,encodedEncryptedContentEncryptionKey,encodededinitializationVector,encodedCipherText,encodedAuthenticationTag);
  }
 catch (  EncryptDataException e) {
    Log.i(TAG,"Error while encrypting fields" + e.getMessage());
  }
  return null;
}

public String encrypt ( EncryptData encryptData ) { GsonBuilder gsonBuilder = new GsonBuilder ( ) ; gsonBuilder . registerTypeAdapter ( EncryptData . class , new EncryptDataJsonSerializer ( ) ) ; Gson gson = gsonBuilder . create ( ) ; String payload = gson . toJson ( encryptData ) ; try { String protectedHeader = createProtectedHeader ( ) ; String encodededProtectedHeader = encryptUtil . base64UrlEncode ( protectedHeader . getBytes ( ) ) ; byte [ ] contentEncryptionKey = encryptUtil . generateSecureRandomBytes ( CONTENT_ENCRYPTION_KEY_SIZE ) ; byte [ ] encryptedContentEncryptionKey = encryptUtil . encryptContentEncryptionKey ( contentEncryptionKey , ( RSAPublicKey ) publicKeyResponse . getPublicKey ( ) ) ; String encodedEncryptedContentEncryptionKey = encryptUtil . base64UrlEncode ( encryptedContentEncryptionKey ) ; byte [ ] macKey = Arrays . copyOf ( contentEncryptionKey , CONTENT_ENCRYPTION_KEY_SIZE / 2 ) ; byte [ ] encKey = Arrays . copyOfRange ( contentEncryptionKey , CONTENT_ENCRYPTION_KEY_SIZE / 2 , CONTENT_ENCRYPTION_KEY_SIZE ) ; byte [ ] initializationVector = encryptUtil . generateSecureRandomBytes ( INITIALIZATION_VECTOR ) ; String encodededinitializationVector = encryptUtil . base64UrlEncode ( initializationVector ) ; byte [ ] cipherText = encryptUtil . encryptPayload ( payload , encKey , initializationVector ) ; String encodedCipherText = encryptUtil . base64UrlEncode ( cipherText ) ; byte [ ] additionalAuthenticatedData = encodededProtectedHeader . getBytes ( Charset . forName ( "UTF-8" ) ) ; byte [ ] al = calculateAdditionalAuthenticatedDataLength ( additionalAuthenticatedData ) ; byte [ ] calculatedHmac = calculateHMAC ( macKey , additionalAuthenticatedData , initializationVector , cipherText , al ) ; byte [ ] authenticationTag = Arrays . copyOf ( calculatedHmac , calculatedHmac . length / 2 ) ; String encodedAuthenticationTag = encryptUtil . base64UrlEncode ( authenticationTag ) ; return buildCompactRespresentation ( encodededProtectedHeader , encodedEncryptedContentEncryptionKey , encodededinitializationVector , encodedCipherText , encodedAuthenticationTag ) ; } catch ( EncryptDataException e ) { Log . i ( TAG , "Error while encrypting fields" + e . getMessage ( ) ) ; } return null ; }
encrypts all paymentproductfield values for the given paymentrequest
