88import com .mastercard .developer .encryption .rsa .RSA ;
99import com .mastercard .developer .json .JsonEngine ;
1010import com .mastercard .developer .utils .ByteUtils ;
11+ import com .mastercard .developer .utils .EncodingUtils ;
1112
1213import javax .crypto .Cipher ;
1314import javax .crypto .spec .GCMParameterSpec ;
@@ -57,14 +58,14 @@ public String decrypt(JweConfig config) throws EncryptionException, GeneralSecur
5758 public static String encrypt (JweConfig config , String payload , JweHeader header ) throws EncryptionException , GeneralSecurityException {
5859 SecretKeySpec cek = AESEncryption .generateCek (256 );
5960 byte [] encryptedSecretKeyBytes = RSA .wrapSecretKey (config .getEncryptionCertificate ().getPublicKey (), cek , "SHA-256" );
60- String encryptedKey = base64UrlEncode (encryptedSecretKeyBytes );
61+ String encryptedKey = EncodingUtils . base64UrlEncode (encryptedSecretKeyBytes );
6162
6263 byte [] iv = AESEncryption .generateIv ().getIV ();
6364 byte [] payloadBytes = payload .getBytes ();
6465 GCMParameterSpec gcmSpec = new GCMParameterSpec (128 , iv );
6566
6667 String headerString = header .toJson ();
67- String encodedHeader = base64UrlEncode (headerString .getBytes ());
68+ String encodedHeader = EncodingUtils . base64UrlEncode (headerString .getBytes ());
6869
6970 byte [] aad = encodedHeader .getBytes (StandardCharsets .US_ASCII );
7071
@@ -76,7 +77,7 @@ public static String encrypt(JweConfig config, String payload, JweHeader header)
7677 byte [] cipherText = ByteUtils .subArray (cipherOutput , 0 , tagPos );
7778 byte [] authTag = ByteUtils .subArray (cipherOutput , tagPos , ByteUtils .byteLength (128 ));
7879
79- return serialize (encodedHeader , encryptedKey , base64UrlEncode (iv ), base64UrlEncode (cipherText ), base64UrlEncode (authTag ));
80+ return serialize (encodedHeader , encryptedKey , EncodingUtils . base64UrlEncode (iv ), EncodingUtils . base64UrlEncode (cipherText ), EncodingUtils . base64UrlEncode (authTag ));
8081 }
8182
8283 private static String serialize (String header , String encryptedKey , String iv , String cipherText , String authTag ) {
@@ -90,14 +91,6 @@ private static String serialize(String header, String encryptedKey, String iv, S
9091 authTag ;
9192 }
9293
93- /**
94- * BASE64URL as per https://datatracker.ietf.org/doc/html/rfc7515#appendix-C
95- */
96- private static String base64UrlEncode (byte [] bytes ) {
97- return Base64 .getUrlEncoder ().encodeToString (bytes )
98- .replace ("=" , "" ); // Remove any trailing '='s
99- }
100-
10194 public static JweObject parse (String encryptedPayload , JsonEngine jsonEngine ) {
10295 String [] payloadParts = encryptedPayload .trim ()
10396 .split ("\\ ." );
0 commit comments