package visa.api;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.annotation.JsonInclude.Include;
import com.nimbusds.jose.*;
import com.nimbusds.jose.crypto.RSAEncrypter;
import com.nimbusds.jose.util.Base64;
import com.nimbusds.jose.util.IOUtils;
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.IOException;
import java.nio.charset.Charset;
import java.security.cert.Certificate;
import java.security.cert.CertificateException;
import java.security.cert.CertificateFactory;
import java.security.interfaces.RSAPublicKey;
public class EncryptionUtils {
private static final String BEGIN_CERT = "-----BEGIN CERTIFICATE-----";
private static final String END_CERT = "-----END CERTIFICATE-----";
// Update the path to your public certificate
private static String mleServerPublicCertificatePath = "C:\\Users\\supriya.mahajan\\Downloads\\server_cert.pem";
public static String getEncryptedPayload(Object payload, String keyId) throws CertificateException, JOSEException, IOException {
ObjectMapper mapper = new ObjectMapper();
mapper.setSerializationInclusion(Include.NON_NULL);
mapper.setSerializationInclusion(Include.NON_EMPTY);
String plainText = payload == null ? "" : mapper.writeValueAsString(payload);
JWEHeader.Builder headerBuilder = new JWEHeader.Builder(JWEAlgorithm.RSA_OAEP_256, EncryptionMethod.A128GCM);
headerBuilder.keyID(keyId);
headerBuilder.customParam("iat", System.currentTimeMillis());
JWEObject jweObject = new JWEObject(headerBuilder.build(), new Payload(plainText));
jweObject.encrypt(new RSAEncrypter(getRSAPublicKey()));
return "{\"encData\":\"" + jweObject.serialize() + "\"}";
}
/*
* Converts PEM file content to RSAPublicKey
*/
private static RSAPublicKey getRSAPublicKey() throws CertificateException, IOException {
String pemEncodedPublicKey = IOUtils.readFileToString(new File(mleServerPublicCertificatePath), Charset.forName("UTF-8"));
Base64 base64 = new Base64(pemEncodedPublicKey.replaceAll(BEGIN_CERT, "").replaceAll(END_CERT, "").trim());
Certificate cf = CertificateFactory.getInstance("X.509").generateCertificate(new ByteArrayInputStream(base64.decode()));
return (RSAPublicKey) cf.getPublicKey();
}
This is my EncryptionUtils.java file
package visa.api;
import visa.api.EncryptionUtils;
public class App {
public String getGreeting() {
return "Hello World!";
}
public static void main(String[] args) {
System.out.println(new App().getGreeting());
try {
// Sample payload and keyId
String payload = "{\n" +
" \"Envt\": {\n" +
" \"Card\": {\n" +
" \"CardTp\": \"PHYS\",\n" +
" \"XpryDt\": \"1230\",\n" +
" \"PAN\": \"4000123456781234\",\n" +
" \"Seed\": \"123456ABCDEF\",\n" +
" \"CardSeqNb\": 1\n" +
" }\n" +
" },\n" +
" \"Hdr\": {\n" +
" \"msgIdentfctn\": {\n" +
" \"clientId\": \"TEST_CLIENT_123\",\n" +
" \"correlatnId\": \"456987456\"\n" +
" },\n" +
" \"PrtcolVrsn\": \"VIP.1.1\"\n" +
" }\n" +
"}";
String keyId = "ccc10b08-04ae-4c4a-bdb9-0e8216148e9c";
// Encrypt the payload
String encryptedPayload = EncryptionUtils.getEncryptedPayload(payload, keyId);
System.out.println("Encrypted Payload: " + encryptedPayload);
} catch (Exception e) {
// Print stack trace for debugging
e.printStackTrace();
}
}
}
This is my App.java file
After running this code I am getting an encrypted payload.
That encrpyted code I am passing as postman request body as :
I am using the api to create new card and doing sandbox testing
Hi @supriya7768, Thank you for reaching out. An agent will look into this and get back to you soon. Until then, if any community member knows a solution, please feel free to reply in this thread.