# Encryption & Decryption Guide for EnKash API Integration

EnKash APIs require **AES encryption with ECB mode and PKCS5 padding** to secure request payloads. This guide walks you through implementing encryption and decryption in **Java** before sending and receiving API requests.  

<Container>
  ### <span style="color:#7b2cbf">Encryption of API Requests </span>

To encrypt your request data before sending it to EnKash APIs, follow these steps:  

<Steps>
  <Step title="Import Required Libraries">
    Add the following imports in your Java code:  

```java
import java.nio.charset.StandardCharsets;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.Arrays;
import javax.crypto.Cipher;
import javax.crypto.spec.SecretKeySpec;
import java.util.Base64;
```
  </Step>
  <Step title="Implement the Encryption Function">
    Create a method to encrypt request data using **AES-128 with ECB mode and PKCS5 padding**.  

```java
public static String encrypt(String strToEncrypt, String secret) {
    try {
        if (strToEncrypt == null || strToEncrypt.isEmpty()) {
            return "";
        }

        SecretKeySpec secretKeySpec = getKeySpec(secret);
        Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
        cipher.init(Cipher.ENCRYPT_MODE, secretKeySpec);

        byte[] encryptedBytes = cipher.doFinal(strToEncrypt.getBytes(StandardCharsets.UTF_8));
        return Base64.getEncoder().encodeToString(encryptedBytes);
    } catch (Exception e) {
        System.err.println("Error while encrypting: " + e.getMessage());
    }
    return null;
}
```
  </Step>
  <Step title="Generate a SecretKeySpec for Encryption">
    This method ensures the secret key is hashed and trimmed to **128 bits** for AES encryption.  

```java
private static SecretKeySpec getKeySpec(String secretKey) throws NoSuchAlgorithmException {
    byte[] key = secretKey.getBytes(StandardCharsets.UTF_8);
    MessageDigest sha = MessageDigest.getInstance("SHA-256");
    key = sha.digest(key);
    key = Arrays.copyOf(key, 16); // Use only the first 128 bits for AES-128
    return new SecretKeySpec(key, "AES");
}
```
  </Step>
    <Step title="Encrypt Data Before Sending an API Request">
    Use the encryption method to secure your request data before passing it to the API.  

```java
String stringToEncrypt = "This is a confidential message.";
String secretKey = "YourSecretKey123"; // Replace with actual key

String encryptedData = encrypt(stringToEncrypt, secretKey);
System.out.println("Encrypted Data: " + encryptedData);
```
  </Step>
</Steps>

</Container>


<Container>
  ### <span style="color:#7b2cbf">Decryption of API Responses </span>

To decrypt the API response, implement the following 

<Steps>
  <Step title="Decryption Function">
    ```java
public static String decrypt(String strToDecrypt, String secret) {
    try {
        if (strToDecrypt == null || strToDecrypt.isEmpty()) {
            return "";
        }

        SecretKeySpec secretKeySpec = getKeySpec(secret);
        Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5PADDING");
        cipher.init(Cipher.DECRYPT_MODE, secretKeySpec);

        return new String(cipher.doFinal(Base64.getDecoder().decode(strToDecrypt)), StandardCharsets.UTF_8);
    } catch (Exception e) {
        System.err.println("Error while decrypting: " + e.getMessage());
    }
    return null;
}
```
  </Step>
  <Step title="Decrypt API Response">
      After receiving an encrypted API response, decrypt it using the following:  

    ```java
String encryptedResponse = "ENCRYPTED_RESPONSE_STRING"; // Replace with actual API response
String secretKey = "YourSecretKey123"; // Use the same secret key

String decryptedData = decrypt(encryptedResponse, secretKey);
System.out.println("Decrypted Data: " + decryptedData);
```
  </Step>
</Steps>


</Container>

:::warning[]
<Icon icon="solar-bold-round-arrow-right"color="#7b2cbf"/> Do not include double quotes (`" "`) in input strings. 
<Icon icon="solar-bold-round-arrow-right"color="#7b2cbf"/> Ensure no trailing whitespaces exist in the encrypted string. 
<Icon icon="solar-bold-round-arrow-right"color="#7b2cbf"/> Use the same secret key for both encryption and decryption.  
<Icon icon="solar-bold-round-arrow-right"color="#7b2cbf"/> Always handle exceptions to avoid unexpected failures. 
:::

