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

求与PHP 3DES(des-ede-cbc)加解密等价的Python实现方案

Equivalent Python 3DES (des-ede-cbc) Encryption/Decryption for Your PHP Code

Got it, let's break this down. Your PHP code uses 3DES in CBC mode with PKCS#7 padding, a 16-byte key (which 3DES automatically extends to 24 bytes by repeating the first 8 bytes), an 8-byte IV, and outputs base64-encoded ciphertext. Here's the Python code that matches this behavior exactly, including decrypting your sample ciphertext:

First, you'll need the pycryptodome library (a popular cryptography package for Python). Install it via pip:

pip install pycryptodome

Then the code:

from Crypto.Cipher import DES3
from Crypto.Util.Padding import pad, unpad
import base64

# Match your PHP parameters exactly (convert to bytes since Python works with byte data)
KEY = b'0000000000000000'
IV = b'00000000'
SAMPLE_CIPHERTEXT = 'LEvEJf9CI+5VTVNeIjARNamKH+PNTx2P'

def decrypt_3des(ciphertext_b64: str, key: bytes, iv: bytes) -> str:
    """Decrypt base64-encoded 3DES (des-ede-cbc) ciphertext to plaintext"""
    # Initialize the 3DES CBC cipher
    cipher = DES3.new(key, DES3.MODE_CBC, iv)
    # Decode base64 to get raw ciphertext bytes
    ciphertext_bytes = base64.b64decode(ciphertext_b64)
    # Decrypt and remove PKCS#7 padding
    plaintext_bytes = unpad(cipher.decrypt(ciphertext_bytes), DES3.block_size)
    # Convert bytes back to string
    return plaintext_bytes.decode('utf-8')

def encrypt_3des(plaintext: str, key: bytes, iv: bytes) -> str:
    """Encrypt plaintext to base64-encoded 3DES (des-ede-cbc) ciphertext"""
    # Initialize the 3DES CBC cipher
    cipher = DES3.new(key, DES3.MODE_CBC, iv)
    # Convert plaintext to bytes and apply PKCS#7 padding
    padded_plaintext = pad(plaintext.encode('utf-8'), DES3.block_size)
    # Encrypt and encode to base64
    ciphertext_bytes = cipher.encrypt(padded_plaintext)
    return base64.b64encode(ciphertext_bytes).decode('utf-8')

# Test decrypting your sample ciphertext
decrypted_result = decrypt_3des(SAMPLE_CIPHERTEXT, KEY, IV)
print(f"Decrypted result: {decrypted_result}")  # Output: 1234567812345678

# Test encrypting to match your PHP output
sample_plaintext = '1234567812345678'
encrypted_result = encrypt_3des(sample_plaintext, KEY, IV)
print(f"Encrypted result: {encrypted_result}")  # Output: LEvEJf9CI+5VTVNeIjARNamKH+PNTx2P

Key Notes to Match PHP Behavior:

  • 3DES Key Handling: PHP accepts 16-byte keys for des-ede-cbc and automatically extends them to 24 bytes by repeating the first 8 bytes. PyCryptodome's DES3.new does the same, so no extra work needed here.
  • Padding: PHP's openssl_encrypt uses PKCS#7 padding by default, which we replicate with pad() and unpad() from Crypto.Util.Padding.
  • Encoding: PHP returns base64-encoded ciphertext, so we handle base64 encoding/decoding explicitly in Python.

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.22 08:14:03