Encryption
Learn how to secure your payment data.
When you directly call the direct card charge endpoints, you'll need to encrypt your payload containing the card details before making the request.
Please Note
You do not need to worry about encryption if you use one of our backend sdks. You can simply pass your encryption key to the library, and it will automatically encrypt the payload before sending the request.
To encrypt your payload manually, you will need an encryption key from the Settings > API section of your dashboard. This uses the 3DES algorithm to encrypt the payload.
Below is an example of an encryption function in different languages. In each case, the function takes the payload as a hash, converts it to JSON, encrypts and encodes it in base64:
function encrypt(encryptionKey, payload) {
const text = JSON.stringify(payload);
const forge = require("node-forge");
const cipher = forge.cipher.createCipher(
"3DES-ECB",
forge.util.createBuffer(encryptionKey)
);
cipher.start({iv: ""});
cipher.update(forge.util.createBuffer(text, "utf-8"));
cipher.finish();
const encrypted = cipher.output;
return forge.util.encode64(encrypted.getBytes());
}
function encrypt(string $encryptionKey, array $payload)
{
$encrypted = openssl_encrypt(json_encode($payload), 'DES-EDE3', $encryptionKey, OPENSSL_RAW_DATA);
return base64_encode($encrypted);
}
def encrypt(encryption_key, payload)
cipher = OpenSSL::Cipher.new "des-ede3"
cipher.encrypt # Call this before setting key
cipher.key = encryption_key
text = payload.to_json
cipher_text = cipher.update text
cipher_text << cipher.final
Base64.encode64 cipher_text
end
import json, requests
import base64
from Crypto.Cipher import DES3
import hashlib
def encryptData(self, key, plainText):
blockSize = 8
padDiff = blockSize - (len(plainText) % blockSize)
cipher = DES3.new(key, DES3.MODE_ECB)
plainText = "{}{}".format(plainText, "".join(chr(padDiff) * padDiff))
encrypted = base64.b64encode(cipher.encrypt(plainText))
return encrypted
class PayloadEncryptor {
private static final String ALGORITHM = "DESede";
private static final String TRANSFORMATION = "DESede/ECB/PKCS5Padding";
public PayloadEncryptor() {}
public String TripleDESEncrypt(String data, String encryptionKey) {
final String defaultString = "";
if (data == null || encryptionKey == null) return defaultString;
try {
final byte[] encryptionKeyBytes = encryptionKey.getBytes();
final SecretKeySpec secretKey = new SecretKeySpec(encryptionKeyBytes, ALGORITHM);
final Cipher cipher = Cipher.getInstance(TRANSFORMATION);
cipher.init(Cipher.ENCRYPT_MODE, secretKey);
final byte[] dataBytes = data.getBytes();
byte[] encryptedValue = cipher.doFinal(dataBytes);
return Base64.encodeToString(encryptedValue, Base64.DEFAULT);
} catch (Exception exception) {
return exception;
}
}
}
Updated 25 days ago