Secret Key Based
A few of the Rest APIs require an encrypted request payload. The merchant application must encrypt payload with shared secret key in order to call Rocketfuel REST API's.
const CryptoJS = require("crypto-js");
//Enryption
const stringifyData = JSON.stringify(payload)
const encrypted = CryptoJS.AES.encrypt(stringifyData, key).toString();
//Decryption
const bytes = CryptoJS.AES.decrypt(value, key);
const data = JSON.parse(bytes.toString(CryptoJS.enc.Utf8)); from Crypto.Cipher import AES
from Crypto.Random import get_random_bytes
import hashlib
import base64
def generate_salt():
return get_random_bytes(8)
def evpkdf(passphrase, salt, key_size=32, iv_size=16):
"""
Derives a key and IV from the passphrase and salt using OpenSSL compatible method.
"""
d = d_i = b'' # Initialize empty bytes for key and IV derivation
while len(d) < key_size + iv_size:
d_i = hashlib.md5(d_i + passphrase.encode('utf-8') + salt).digest()
d += d_i
return d[:key_size], d[key_size:key_size + iv_size]
def pad(data):
"""Pads data to a multiple of AES block size (16 bytes)."""
padding_len = 16 - len(data) % 16
return data + chr(padding_len) * padding_len
def encrypt_data(data, passphrase, salt):
key, iv = evpkdf(passphrase, salt)
cipher = AES.new(key, AES.MODE_CBC, iv)
encrypted_data = cipher.encrypt(pad(data).encode())
return encrypted_data
def create_encrypted_payload(data, passphrase):
salt = generate_salt()
encrypted_data = encrypt_data(data, passphrase, salt)
encrypted_data_with_salt = b"Salted__" + salt + encrypted_data
return base64.b64encode(encrypted_data_with_salt).decode('utf-8')
# Data to be encrypted
data = {
'merchantId': MERCHANT_ID,
'totp': ''
}
# Convert the data to a JSON string
data_str = json.dumps(data)
# Create the encrypted payload
encrypted_payload = create_encrypted_payload(data_str, CLIENT_SECRET)Last updated
Was this helpful?