# Perform Encryption source: https://developer.mastercard.com/mastercard-checkout-solutions/documentation/tutorials-and-guides/perform-encryption-tutorial/index.md Use this tutorial to learn about the required steps to encrypt an object in Mastercard Checkout Solutions. ## Prerequisites {#prerequisites} Complete onboarding through the Mastercard Checkout Solutions Onboarding Application. 1. Order the [Mastercard Checkout Solutions - Sandbox](https://www.mastercardconnect.com/-/store-plus/item-details/A/srconbrmtf) on [Mastercard Connect](https://www.mastercardconnect.com/). 2. [Enroll in the programs](https://developer.mastercard.com/mastercard-checkout-solutions/documentation/tutorials-and-guides/onboarding-sandbox-tutorial/index.md) you require for your sandbox projects. 3. [Create a sandbox project](https://developer.mastercard.com/mastercard-checkout-solutions/documentation/tutorials-and-guides/onboarding-sandbox-tutorial/index.md). You can create up to 30 sandbox projects to allow testing in different lower environments. 4. Download the auto-generated API Signing Key (OAuth key), Consumer Key, and Payload Encryption Key. Read about these keys in the [Key Management](https://developer.mastercard.com/mastercard-checkout-solutions/documentation/tutorials-and-guides/key-management-tutorial/index.md) tutorial. Note: For detailed steps on onboarding in Unified Checkout Solutions, please refer to the [Onboard to Sandbox](https://developer.mastercard.com/mastercard-checkout-solutions/documentation/tutorials-and-guides/onboarding-sandbox-tutorial/index.md) tutorial. ## 1. Set Up API Authentication and Encryption {#1-set-up-api-authentication-and-encryption} * To get started with the API integration, you can generate an API client using an [OpenAPI Generator](https://openapi-generator.tech/), which provides generators and library templates for supporting multiple languages and frameworks. * To help you sign requests and perform payload encryption/decryption, Mastercard provides [client authentication libraries](https://github.com/Mastercard?q=oauth1-signer) and [encryption libraries](https://github.com/Mastercard?&q=client-encryption). * Follow the steps in [Generating and Configuring a Mastercard API Client](https://developer.mastercard.com/platform/documentation/security-and-authentication/generating-and-configuring-a-mastercard-api-client/) tutorial to enable authentication and perform encryption/decryption. * Mastercard supports two protocols for authenticating client applications to integrate with Mastercard APIs. Learn more on [API Basics](https://developer.mastercard.com/mastercard-checkout-solutions/documentation/api-basics/index.md) page. Note: **In case of OAuth Authentication:** * Site identifier `X-Src-Cx-Flow-Id` is added as a part of all inbound API response headers for resolving site affinity issues on subsequent calls. * When making back-to-back API calls, the client must include the `x-Src-Cx-Flow-Id` of the initial API response in the header of the subsequent API request. This ensures that the consecutive API calls will be sent to the same Mastercard server that returned the initial response. **In case of Mutual TLS (MTLS) Authentication:** * The `X-Src-Response-Host` containing a site-specific URL is initially sent from Mastercard in response to the client. * Use this same URL instead of the standard services.mastercard.com domain to ensure that the subsequent calls are directed to the same server that returned the initial response. * MTLS is an alternate authentication mechanism, available on request. Work with your [Mastercard representative](https://developer.mastercard.com/mastercard-checkout-solutions/documentation/support/index.md#get-help) for details. ## 2. Encyrpt a Card {#2-encyrpt-a-card} Encryption involves 2048-bit RSA public/private key pairs. A FPAN encryption key (`src-fpan-encryption`) is a public key that Mastercard provides to protect FPANs in transit. Data encrypted using a public key can only be decrypted using the corresponding private key. To encrypt a card: 1. Access the updated public keys from the following URLs: | Environment | URLs | |-------------|-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------| | Sandbox | * Payload Verification Key: * FPAN Encryption Key: | | Production | * Payload Verification Key: * FPAN Encryption Key: | Refer to [Public Key Mastercard Provides](https://developer.mastercard.com/mastercard-checkout-solutions/documentation/tutorials-and-guides/key-management-tutorial/index.md) for more details. 2. Format your `card` object as shown in the below example: ```json { "primaryAccountNumber":"5299920971401623", "panExpirationMonth":"08", "panExpirationYear":"2031", "cardSecurityCode":"000" } ```
3. Import the following Java packages: * com.nimbusds.jose.EncryptionMethod * com.nimbusds.jose.JOSEException * com.nimbusds.jose.JWEAlgorithm * com.nimbusds.jose.JWECryptoParts * com.nimbusds.jose.JWEEncrypter * com.nimbusds.jose.JWEHeader * com.nimbusds.jose.JWEObject * com.nimbusds.jose.crypto.RSAEncrypter * com.nimbusds.jose.jwk.RSAKey * java.nio.charset.Charset * java.text.ParseException
4. Create a public class and include the main method. Use the `nimbus-jose-jwt` library for creating the JWE token. ```java public class JWEEncryptor { public static final String KID = "20230207223521-sandbox-fpan-encryption-src-mastercard-int"; public static final String PAYLOAD = "{\"primaryAccountNumber\":\"5455031247681061\"," + "\"cardSecurityCode\":\"123\"," + "\"panExpirationMonth\":\"11\",\"panExpirationYear\":\"2024\"}"; public static final String PUBLIC_KEY = "{\n" + " \"kty\": \"RSA\",\n" + " \"x5t#S256\": \"NgYAgjb6lJhuCqoB-3aIPc7km_QvU5nM65xdmxp2bFk\",\n" + " \"e\": \"AQAB\",\n" + " \"use\": \"enc\",\n" + " \"kid\": \"20221214224510-mtf-mc-srci-card-encryption-mtf-srci-mastercard-int\",\n" + " \"x5c\": [\n" + " \"MIIF5zCCA8+gAwIBAgIIDzMLWnXusKEwDQYJKoZIhvcNAQELBQAwgbgxEzARBgoJkiaJk/IsZAEZFgNjb20xGjAYBgoJkiaJk/IsZAEZFgptYXN0ZXJjYXJkMQswCQYDVQQGEwJCRTEdMBsGA1UEChMUTWFzdGVyQ2FyZCBXb3JsZHdpZGUxGzAZBgNVBAsTEkNvcnBvcmF0ZSBTZWN1cml0eTE8MDoGA1UEAxMzTWFzdGVyQ2FyZCBQUkQgQXBwbGljYXRpb24gSW5mcmFzdHJ1Y3R1cmUgU3ViIENBIEcyMB4XDTIzMDIwNzIxMzc0M1oXDTI1MDUxMzIyMjM0M1owga8xMzAxBgNVBAMMKnNhbmRib3gtZnBhbi1lbmNyeXB0aW9uLXNyYy5tYXN0ZXJjYXJkLmludDEMMAoGA1UECwwDc3JjMTQwMgYDVQQKDCtNYXN0ZXJDYXJkIFdvcmxkV2lkZSAtIENvbW1vbiBQcm9kSW5mcmEgU1NMMRQwEgYDVQQHDAtTYWludCBMb3VpczERMA8GA1UECAwITWlzc291cmkxCzAJBgNVBAYTAlVTMIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAt06I8sjlS+rrs7uCagH8evoeumaRovKziZSI9S2N9IDQ9tW2PapfRa9Lc1KveEBDVs27PkhkU5OyHgP0iEjTuKVpvh59T4la+mBSIls7UecUQLLa0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+bOL8A6b8QUtSo7AcAh4Kygtxry9zU3J8rgkeqtj+00H/5e1ZvxHI75CMnZaLtMhO9mnuTB+maOeFPa45VYK8iU8oGSYui6fIunw/0FQhkaAgXWbUTcyRy4pCyH3jQFa6MmpaJhh5lbdynK9pbMEjMZ86P6InbUNgGhUZ2fYg0HUUTfwn1ChE4es4dzjgzbnoBvv0l0iT7\"\n" + " ],\n" + " \"key_ops\": [\n" + " \"encrypt\",\n" + " \"wrapKey\"\n" + " ],\n" + " \"alg\": \"RSA-OAEP-256\",\n" + " \"n\": \"t06I8sjlS-rrs7uCagH8evoeumaRovKziZSI9S2N9IDQ9tW2PapfRa9Lc1KveEBDVs27PkhkU5OyHgP0iEjTuKVpvh59T4la-mBSIls7UecUQLLa0WkmbtL7jNdltA5fq7AhcAr5qca988qrLd7Iyr9E0C3TxbT9to1iQcpz8ocZODRXoidFAnOVL5YGFmlsreDbJ4Vhsi0pAdccQcil-ydSgurKB-rqKppb9epomu4QUh3382Cv8NobYmF3os8nGGgGP-cyXo0nsKcPAgfrlQzos7qHxUOrFe2x_lZ1G1AE-XrkrxjBys9q53GMRSNCQ8c-_mcF9pbq4HYBs-vD5Q\"\n" + "}"; public static void main(String[] args) throws ParseException, JOSEException { //generate key by parsing your public key RSAKey key = RSAKey.parse(PUBLIC_KEY); //define JWE header JWEHeader header = new JWEHeader.Builder(JWEAlgorithm.RSA_OAEP_256, EncryptionMethod.A128GCM).keyID(KID).build(); // Create an RSA encrypted with the specified public RSA key JWEEncrypter joseRsaEncryptor = new RSAEncrypter(key); // Initialized the JWECryptoParts object JWECryptoParts jweCryptoParts = joseRsaEncryptor.encrypt(header, PAYLOAD.getBytes(Charset.defaultCharset())); // Doing the actual encryption JWEObject jweObject = new JWEObject(header.toBase64URL(), jweCryptoParts.getEncryptedKey(), jweCryptoParts.getInitializationVector(), jweCryptoParts.getCipherText(), jweCryptoParts.getAuthenticationTag()); // Serialize to JWT compact form String jwtString = jweObject.serialize(); printIt(jwtString); } private static void printIt(String jwtString) { System.out.println(""); System.out.println("========================= Encrypted JWE token =================================="); System.out.println(""); System.out.println("\n JWE token : " + jwtString); System.out.println(""); } } ```
Note: In the above code sample, * `PUBLIC_KEY` and `PAYLOAD` are required parameters to perform encryption. * For `PUBLIC_KEY` parameter, ensure to use the updated public key value from the URLs mentioned in [Step 1](https://developer.mastercard.com/mastercard-checkout-solutions/documentation/tutorials-and-guides/perform-encryption-tutorial/index.md#2-encyrpt-a-card). 5. Encrypt your object using the above details. Pass the encrypted value in `encryptedCard` parameter during [Enroll Card](https://developer.mastercard.com/mastercard-checkout-solutions/documentation/api-reference/apis/index.md#card) API or `digitalAccountReferenceValue` in [Guest Checkout](https://developer.mastercard.com/mastercard-checkout-solutions/documentation/api-reference/apis/index.md#checkout) API call.