Skip to content
Dashboard

Encrypt PIN

Some APIs require the merchant PIN to be encrypted before it is sent in the request body. Use this page to generate an encrypted PIN and to review implementation examples in different languages.

Use your HesabPay API key as the encryption key when encrypting the PIN. The API key should be handled as a secret and must not be exposed in client-side code.

Encrypted PIN

Enter a PIN and encryption key, then click Encrypt PIN.

The encryption process uses AES-CBC with a 32-byte key. The key is derived by taking the first 32 bytes of the provided key and padding with zero bytes if it is shorter. A random 16-byte IV is generated for every encryption. The final value is:

base64(iv + ciphertext)
import base64
from Crypto.Cipher import AES
from Crypto.Random import get_random_bytes
from Crypto.Util.Padding import pad
def encrypt_pin(data, key):
key = key.encode("utf-8")[:32]
key = key.ljust(32, b"\0")
iv = get_random_bytes(16)
cipher = AES.new(key, AES.MODE_CBC, iv)
padded_data = pad(data.encode("utf-8"), AES.block_size)
ciphertext = cipher.encrypt(padded_data)
encrypted_data = base64.b64encode(iv + ciphertext).decode("utf-8")
return encrypted_data