首页 > 解决方案 > 在 Python 上加密 aes-256-ctr 并在 PHP 上解密

问题描述

我尝试加密数据,例如 Python 上的“qwerty”

       encryption_key =  Random.new().read(32) 

       bs = AES.block_size
       iv = Random.new().read(AES.block_size)
       # Convert the IV to a Python integer.
       iv_int = int(binascii.hexlify(iv), 16) 
       
       countf = Counter.new(AES.block_size*8, initial_value=iv_int) 

       cipher = AES.new(encryption_key, AES.MODE_CTR, counter=countf)

       encrypted = cipher.encrypt(data)

       return b64encode(iv), b64encode(encryption_key), b64encode(encrypted)

并尝试在 PHP 上解密:

$result = openssl_decrypt(base64_decode($data['text']), "aes-256-ctr", base64_decode($data['key']), 0, base64_decode($data['iv']));

它没有用......在 $result 空字符串中。加密显然不是我的天赋:(

我需要在 python 中进行更改,以便 php 可以以这种方式解码。

标签: pythonphpencryptioncryptographyaes

解决方案


推荐阅读