Encryption / Decryption Logic:

Note:These steps must be followed for the below APIs.

Step1:iServeU will share the Client ID, Client Secret, Encryption Key for Request-Response Payload, and Encryption Key for Header Secrets, API user name in a separate email chain.

 

Step2:Every request payload should be encrypted using the Encryption Key for Request Payload using AES256 algorithm and should be shared in the following format:

{
"RequestData":"sdfghi;lhkgjfhgffjgkhlj;jadaljflkalhkfahflahfa"
}

Client will get the below response

{
"ResponseData": "R4dcjsGEYXOWPRP9t8x8s8mJ6hNQT4FWii2WCfR4pKxeXzioW8WDokqs3a 3VDWr/mELpJRWpXw3+Z0fp/0EnBNgvjjENmmCcC8qm1gYi8wIv2VuvRBy3012VOvK3J2ZlKDfQd2+ApsQI7ESDe/eoA=="
}

Need to decrypt using shared Encryption Key for Request-Response Payload.

 

Step3:Header Secrets generation process:

Prepare JSON request payload in the following format :

{
"apiusername": <iServeU will share in separate email chain>,
"client_secret": <iServeU will share in separate email chain>,
"epoch": It will generate current timestamp
}

Epoch example data: "1726656337"

Encrypt the above JSON data using the Encryption Key for Header Secrets using AES256 algorithm and share it in the header_secrets key in the header. It is required to generate every time while consuming the iServeU API. In Header also required to send client_id, which iServeU shares.

 

Encryption code

----------------------
import javax.crypto.Cipher;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;
import java.security.SecureRandom;
import java.util.Base64;
import java.util.Arrays;
import java.nio.charset.StandardCharsets;
public class AESUtils {
// —------- Encryption Method —----------
public static String EncryptRequest(Object incomingJsonReq, String key) throws Exception { byte[]
decodedKey = Base64.getDecoder().decode(key);
// Generate a random IV byte[] iv = new byte[16];
SecureRandom random = new SecureRandom();
random.nextBytes(iv);
// Convert payload to byte array
byte[] payloadBytes = incomingJsonReq.toString().getBytes();
// Initialize AES cipher
SecretKeySpec secretKeySpec = new SecretKeySpec(decodedKey, "AES"); Cipher cipher =
Cipher.getInstance("AES/CBC/PKCS5Padding");
IvParameterSpec ivSpec = new IvParameterSpec(iv);
cipher.init(Cipher.ENCRYPT_MODE, secretKeySpec, ivSpec);
// Encrypt the data
byte[] encryptedBytes = cipher.doFinal(payloadBytes);
// Combine IV and encrypted data
byte[] result = new byte[iv.length + encryptedBytes.length];
System.arraycopy(iv, 0, result, 0, iv.length);
System.arraycopy(encryptedBytes, 0, result, iv.length, encryptedBytes.length);
// Return Base64 encoded result
return Base64.getEncoder().encodeToString(result);
}

 

Decryption code

 // —-------- Decryption Method ----------
public String decryptRequest(String encryptedString, String key) throws Exception {
// Decode Base64 encoded input and key
byte[] byteCipherText = Base64.getDecoder().decode(encryptedString);
byte[] byteKey = Base64.getDecoder().decode(key);
// Extract IV and cipher text from the encrypted input byte[] iv =
Arrays.copyOfRange(byteCipherText, 0,16);
byte[] cipherText = Arrays.copyOfRange(byteCipherText, 16, byteCipherText.length);
// Initialize AES cipher for decryption
SecretKeySpec secretKey = new SecretKeySpec(byteKey, "AES");
IvParameterSpec ivParams = new IvParameterSpec(iv);
Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
cipher.init(Cipher.DECRYPT_MODE, secretKey, ivParams);
// Decrypt the cipher text
byte[] bytePlainText = cipher.doFinal(cipherText);
// Clean the decrypted data and return
return removeNoise(new String(bytePlainText, StandardCharsets.UTF_8).trim());
}
// Removing noise and returning clean string up to } or ] private String removeNoise(String data) {
int lastCurlyBrace = data.lastIndexOf('}');
int lastSquareBracket = data.lastIndexOf(']');
int lastIndex = Math.max(lastCurlyBrace, lastSquareBracket);
if (lastIndex != -1) {
return data.substring(0, lastIndex + 1);
}
return data;
} }