Python AES 256 CBC Encryption

Viewed 61

I am trying to transfer a hash key generated encryption via PHP to Python. I couldn't figure out how to edit openssl_encrypt. I would be glad if you help.

PHP Code


function generateSaveCardCreateHashKey(
    $merchant_key,
    $customer_number,
    $card_number,
    $card_holder_name,
    $expiry_month,
    $expiry_year,
    $app_secret
){
    $data = $merchant_key.'|'. $customer_number .'|'.$card_holder_name.'|'.$card_number.'|'.$expiry_month.'|'.$expiry_year;
    $iv = substr(sha1(mt_rand()), 0, 16);
    $password = sha1($app_secret);
    $salt = substr(sha1(mt_rand()), 0, 4);
    $saltWithPassword = hash('sha256', $password . $salt);
    $encrypted = openssl_encrypt("$data", 'aes-256-cbc', "$saltWithPassword", null, $iv);
    $msg_encrypted_bundle = "$iv:$salt:$encrypted";
    $msg_encrypted_bundle = str_replace('/', '__', $msg_encrypted_bundle);

    return $msg_encrypted_bundle;

}  

Python Code

from Crypto.Hash import SHA1
from Crypto.Hash import SHA256

def generateSaveCardCreateHashKey(
    merchant_key , 
    customer_number , 
    card_number , 
    card_holder_name , 
    expiry_month , 
    expiry_year , 
    app_secret
    ) :
    data = merchant_key + ":" + customer_number + "|" + card_holder_name + "|" + card_number + "|" + expiry_month + "|" + expiry_year

    randNumIv = str(random.randint(10000000000000000,99999999999999999))
    hashNumIv = SHA1.new()
    hashNumIv.update(randNumIv.encode("UTF-8"))
    hashNumber = hashNumIv.hexdigest()
    iv = hashNumber[:16]
    
    hashAppSec = SHA1.new()
    hashAppSec.update(app_secret.encode("UTF-8"))
    password = hashAppSec.hexdigest()
    
    randNumSalt = str(random.randint(10000000000000000,99999999999999999))
    hashNumSalt = SHA1.new()
    hashNumSalt.update(randNumSalt.encode("UTF-8"))
    hashSalt = hashNumSalt.hexdigest()
    salt = hashSalt[:4]
    
    strPassSalt = password + salt
    hashStr = SHA256.new()
    hashStr.update(strPassSalt.encode("UTF-8"))
    saltWithPassword = hashStr.hexdigest()
    
    encrypted = ""
    
    
    msg_encrypted_bundle = iv + ":" + salt + ":" + encrypted
    msg_encrypted_bundle = msg_encrypted_bundle.replace("/" , "_")
    
    return msg_encrypted_bundle

PHP code is working. I need to write the python code correctly because it is used in the payment system. The bank system checks the generated hash key and provides the transaction. I couldn't figure out how to edit the encrypted value. So I couldn't run the code properly. I would appreciate it if you could guide me to edit it.

1 Answers

I found the way to get the code working with more tries. I wanted to share in case it might be helpful. Or there may be a better way :)

def encrypt(plain_text: bytes, app_secret: str):
    
    randNumIv = str(random.randint(100000000,900000000))
    hashNumber = hashlib.sha1(randNumIv.encode("utf-8")).hexdigest()
    iv = hashNumber[:16]
    
    password = hashlib.sha1(app_secret.encode("utf-8")).hexdigest()
    
    randNumSalt = str(random.randint(100000000,900000000))
    hashSalt = hashlib.sha1(randNumSalt.encode("utf-8")).hexdigest()
    salt = hashSalt[:4]
    
    strPassSalt = password + salt
    saltWithPassword = hashlib.sha256(strPassSalt.encode("utf-8")).hexdigest()
    
    padded_text = pad(plain_text)

    cipher_config = AES.new(bytes(saltWithPassword[:32], "utf-8"), AES.MODE_CBC, bytes(iv, "utf-8"))
    cipher = cipher_config.encrypt(padded_text)
    
    encrypted = base64.b64encode(cipher).decode('utf-8')
    
    msg_encrypted_bundle = iv + ":" + salt + ":" + encrypted
    msg_encrypted_bundle = msg_encrypted_bundle.replace("/" , "__")
    
    return msg_encrypted_bundle

I was able to create a function and send the necessary information and get results.

Related