# How to encrypt and decrypt data

## Encryption

***API Encryption***

All of the body data will need to be encrypted using AES-256. The response also will be in encrypted AES-256

**Detail:**

● Encryption Method: openssl\_encrypt AES-256

● body: request body

● Method: aes-256-cbc

● Ciphertext: <mark style="color:red;">Provided by Innov8tif</mark>

● API\_key: <mark style="color:red;">Provided by Innov8tif</mark>

● **Encryption Formula:**

d<mark style="color:blue;">ata = openssl\_encrypt(body, method, ciphertext+API\_key, OPENSSL\_RAW\_DATA, ciphertext)</mark>

{% tabs %}
{% tab title="PHP" %}

```php

$body = 'your_body';
$method = 'AES-256-CBC';
$ciphertext = 'your_ciphertext';
$API_key = 'your_api_key';

$key = $ciphertext . $API_key;

$encryptedText = openssl_encrypt($body, $method, $key, OPENSSL_RAW_DATA, $ciphertext);

echo base64_encode($encryptedText);
?>
```

{% endtab %}

{% tab title="JAVA" %}

```java
import javax.crypto.Cipher;
import javax.crypto.spec.SecretKeySpec;
import java.nio.charset.StandardCharsets;
import java.util.Base64;

public class OpenSSLExample {
    public static void main(String[] args) throws Exception {
        String body = "your_body";
        String method = "AES/CBC/PKCS5Padding";
        String ciphertext = "your_ciphertext";
        String API_key = "your_api_key";

        SecretKeySpec keySpec = new SecretKeySpec((ciphertext + API_key).getBytes(StandardCharsets.UTF_8), "AES");
        Cipher cipher = Cipher.getInstance(method);
        cipher.init(Cipher.ENCRYPT_MODE, keySpec);

        byte[] encryptedBytes = cipher.doFinal(body.getBytes(StandardCharsets.UTF_8));
        String encryptedText = Base64.getEncoder().encodeToString(encryptedBytes);

        System.out.println(encryptedText);
    }
}

```

{% endtab %}

{% tab title="PYTHON" %}

```python
from Crypto.Cipher import AES
from Crypto.Util.Padding import pad
import base64

body = b'your_body'
method = 'AES/CBC/PKCS5Padding'
ciphertext = 'your_ciphertext'
API_key = 'your_api_key'

key = (ciphertext + API_key).encode('utf-8')
cipher = AES.new(key, AES.MODE_CBC)

# Padding the data to match the block size
padded_data = pad(body, AES.block_size)

encrypted_bytes = cipher.encrypt(padded_data)
encrypted_text = base64.b64encode(encrypted_bytes).decode('utf-8')

print(encrypted_text)

```

{% endtab %}
{% endtabs %}

## Decryption

{% tabs %}
{% tab title="PHP" %}

```php
<?php

$encryptedText = 'your_encrypted_text';
$method = 'AES-256-CBC';
$ciphertext = 'your_ciphertext';
$API_key = 'your_api_key';

$key = $ciphertext . $API_key;

$decryptedText = openssl_decrypt(base64_decode($encryptedText), $method, $key, OPENSSL_RAW_DATA, $ciphertext);

echo $decryptedText;
?>

```

{% endtab %}

{% tab title="JAVA" %}

```java
import javax.crypto.Cipher;
import javax.crypto.spec.SecretKeySpec;
import java.nio.charset.StandardCharsets;
import java.util.Base64;

public class OpenSSLDecryptionExample {
    public static void main(String[] args) throws Exception {
        String encryptedText = "your_encrypted_text";
        String method = "AES/CBC/PKCS5Padding";
        String ciphertext = "your_ciphertext";
        String API_key = "your_api_key";

        SecretKeySpec keySpec = new SecretKeySpec((ciphertext + API_key).getBytes(StandardCharsets.UTF_8), "AES");
        Cipher cipher = Cipher.getInstance(method);
        cipher.init(Cipher.DECRYPT_MODE, keySpec);

        byte[] encryptedBytes = Base64.getDecoder().decode(encryptedText);
        byte[] decryptedBytes = cipher.doFinal(encryptedBytes);
        String decryptedText = new String(decryptedBytes, StandardCharsets.UTF_8);

        System.out.println(decryptedText);
    }
}

```

{% endtab %}

{% tab title="PYTHON" %}

```python
from Crypto.Cipher import AES
from Crypto.Util.Padding import unpad
import base64

encrypted_text = 'your_encrypted_text'
method = 'AES/CBC/PKCS5Padding'
ciphertext = 'your_ciphertext'
API_key = 'your_api_key'

key = (ciphertext + API_key).encode('utf-8')
cipher = AES.new(key, AES.MODE_CBC)

# Decoding base64 and decrypting the text
encrypted_bytes = base64.b64decode(encrypted_text)
decrypted_bytes = unpad(cipher.decrypt(encrypted_bytes), AES.block_size)
decrypted_text = decrypted_bytes.decode('utf-8')

print(decrypted_text)

```

{% endtab %}
{% endtabs %}


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://api2-ekycapis.innov8tif.com/okayface-search/okayface-search-all/how-to-encrypt-and-decrypt-data.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
