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.