Encryption
This type of encryption only applies if you're using direct card charge.
When using our direct card charge API to charge a card directly, you'll need to encrypt the payload containing the card details before making the request.
If you're using one of our backend SDKs, you don't need to worry about encryption. Pass your encryption key to the library, and it will automatically encrypt the payload before sending.
To encrypt the payload manually, you'll need your encryption key (from the Settings > API section of your dashboard). You'll use the 3DES algorithm to encrypt the payload.
Here's 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 it 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;
}
}
}