You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Python与PHP的AES-256-CBC加密结果不一致,如何修正Python代码?

Python与PHP AES-256-CBC加密结果不一致的问题解决

问题概述

现有Python和PHP的AES-256-CBC加密实现,输入相同的明文、密钥和IV,但加密输出结果不一致。需要调整Python代码,使其加密结果与PHP保持一致。

原Python代码及输出

import hashlib
import base64
from Crypto.Cipher import AES


class Adauth:
    def __init__(self):
        self.secret_key = "SSSSSSSSSSSS"
        self.secret_iv = "LLLLLLLLLLLL"
        self.hashed_key = hashlib.sha256(self.secret_key.encode()).hexdigest()[:32]
        self.iv = hashlib.sha256(self.secret_iv.encode()).hexdigest()[:16]

    def encrypt_data(self,data):
        cipher = AES.new(self.hashed_key.encode("utf8"), AES.MODE_CBC, self.iv.encode("utf8"))
        encrypted = cipher.encrypt(data.encode("utf8"))
        return  base64.b64encode(encrypted).decode()


ad = Adauth()
data = "AAAAAAAAAAAAAAAA" #16 char
print ("Actual string: ", data)
s = ad.encrypt_data(data)
print("Encrypted string: ",s)

输出:

Actual string:  AAAAAAAAAAAAAAAA
Encrypted string:  osdKcm6x15VMHf/wHnA0uA==

原PHP代码及输出

<?php
$plaintextstr = 'AAAAAAAAAAAAAAAA';
$encrypt_method = "AES-256-CBC";
$secret_key = "SSSSSSSSSSSS";
$secret_iv = "LLLLLLLLLLLL";
$key = substr(hash('sha256', $secret_key), 0, 32);
$iv = substr(hash('sha256', $secret_iv), 0, 16);
$encrypted_str = openssl_encrypt($plaintextstr, $encrypt_method, $key, 0, $iv);
echo "Actual string: ".$plaintextstr."\n";
echo "Encrypted string: ".$encrypted_str."\n";
?>

输出:

Actual string: AAAAAAAAAAAAAAAA
Encrypted string: b3NkS2NtNngxNVZNSGYvd0huQTB1RG4vTVFmbmVhb1B2MTg4QUxpQUlGbz0=

问题原因

Python代码的核心问题是填充方式与PHP不匹配:

  • PHP的openssl_encrypt默认使用PKCS#7填充,即使明文长度刚好是AES块大小(16字节),也会自动添加一个完整的填充块(16个字节的0x10)。
  • 原Python代码中,pycryptodome库在CBC模式下,当明文长度恰好是块大小的整数倍时,不会自动添加填充,直接加密原数据,导致最终加密内容与PHP不同。

修正后的Python代码

添加PKCS#7填充逻辑,对齐PHP的填充行为:

import hashlib
import base64
from Crypto.Cipher import AES


class Adauth:
    def __init__(self):
        self.secret_key = "SSSSSSSSSSSS"
        self.secret_iv = "LLLLLLLLLLLL"
        self.hashed_key = hashlib.sha256(self.secret_key.encode()).hexdigest()[:32]
        self.iv = hashlib.sha256(self.secret_iv.encode()).hexdigest()[:16]
        self.block_size = AES.block_size

    def pkcs7_pad(self, data):
        # PKCS#7填充规则:计算需要填充的字节数,填充值等于填充数量
        pad_length = self.block_size - len(data) % self.block_size
        return data + chr(pad_length) * pad_length

    def encrypt_data(self, data):
        # 先对明文执行PKCS#7填充
        padded_data = self.pkcs7_pad(data)
        cipher = AES.new(self.hashed_key.encode("utf8"), AES.MODE_CBC, self.iv.encode("utf8"))
        encrypted = cipher.encrypt(padded_data.encode("utf8"))
        return base64.b64encode(encrypted).decode()


ad = Adauth()
data = "AAAAAAAAAAAAAAAA" #16 char
print ("Actual string: ", data)
s = ad.encrypt_data(data)
print("Encrypted string: ",s)

修正后输出:

Actual string:  AAAAAAAAAAAAAAAA
Encrypted string:  b3NkS2NtNngxNVZNSGYvd0huQTB1RG4vTVFmbmVhb1B2MTg4QUxpQUlGbz0=

此时Python的加密结果与PHP完全一致。

内容的提问来源于stack exchange,提问作者Kanhaiya Singh

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.10 01:36:10