Python AES 256 CBC Encryption



我正试图通过PHP将哈希密钥生成的加密传输到Python。我不知道如何编辑openssl_encrypt。如果你帮忙,我会很高兴的。

PHP代码


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代码

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代码正在运行。我需要正确地编写python代码,因为它在支付系统中使用。银行系统检查生成的哈希密钥并提供交易。我不知道如何编辑加密的值。所以我无法正确运行代码。如果你能指导我编辑它,我将不胜感激。

我找到了一种方法,可以通过更多的尝试来让代码工作。我想分享一下,以防有帮助。或者可能有更好的方法:(

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

我能够创建一个函数,发送必要的信息并获得结果。

相关内容

  • 没有找到相关文章

最新更新