请求将AES加密Java代码转换为可运行的Python代码
Let’s walk through translating your Java AES code to Python, focusing on matching Java’s typical AES implementation (most commonly AES/CBC/PKCS5Padding, since PKCS5 and PKCS7 padding are interchangeable for AES’s 16-byte block size).
Step 1: Install the Required Library
First, we’ll use the pycryptodome library (a popular, maintained crypto tool for Python). Install it via pip:
pip install pycryptodome
Step 2: Python Implementation
Below is code that mirrors Java’s standard AES/CBC workflow, using your provided initialization vector (IV) constant. We’ll include both encryption and decryption functions for verification.
Encryption Function
from Crypto.Cipher import AES from Crypto.Util.Padding import pad import base64 def aes_encrypt(plaintext, secret_key, init_vector): # Convert strings to bytes (Java uses UTF-8 by default) plaintext_bytes = plaintext.encode("utf-8") key_bytes = secret_key.encode("utf-8") iv_bytes = init_vector.encode("utf-8") # Initialize AES cipher in CBC mode (matches Java's common setup) cipher = AES.new(key_bytes, AES.MODE_CBC, iv_bytes) # Apply PKCS5/PKCS7 padding (Java's PKCS5Padding works for AES) padded_data = pad(plaintext_bytes, AES.block_size) # Encrypt and encode as Base64 (common for readable ciphertext in Java) encrypted_bytes = cipher.encrypt(padded_data) return base64.b64encode(encrypted_bytes).decode("utf-8")
Decryption Function (for Validation)
If you need to reverse the encryption (to confirm it matches Java’s output):
from Crypto.Util.Padding import unpad def aes_decrypt(ciphertext_b64, secret_key, init_vector): # Decode Base64 ciphertext back to bytes ciphertext_bytes = base64.b64decode(ciphertext_b64) key_bytes = secret_key.encode("utf-8") iv_bytes = init_vector.encode("utf-8") cipher = AES.new(key_bytes, AES.MODE_CBC, iv_bytes) decrypted_bytes = cipher.decrypt(ciphertext_bytes) # Remove padding to get the original plaintext return unpad(decrypted_bytes, AES.block_size).decode("utf-8")
Step 3: Test with Your Sample Data
Use your provided IV and sample string to test the code. Replace SECRET_KEY with the actual key used in your Java code:
# Your constants from the Java code INIT_VECTOR = "IqtY8jgALtjZNLM5" SECRET_KEY = "your_java_secret_key_here" # Swap with your real key plaintext_to_encrypt = "pUypiz-7hJ0y_JtpKWaydp" # Run encryption encrypted_result = aes_encrypt(plaintext_to_encrypt, SECRET_KEY, INIT_VECTOR) print("Encrypted output:", encrypted_result) # Verify by decrypting decrypted_result = aes_decrypt(encrypted_result, SECRET_KEY, INIT_VECTOR) print("Decrypted output:", decrypted_result)
Key Notes to Match Java Exactly
- Key Length: AES requires keys of 16, 24, or 32 bytes (for 128, 192, or 256-bit AES). Ensure your Python key matches the length used in Java.
- Key Derivation: If your Java code uses a password to generate the key (e.g., via PBKDF2 with HMAC-SHA256), let me know and I can add that derivation logic to the Python code.
- Encoding: We used UTF-8 for string-to-byte conversion, which is Java’s default. If your Java code uses a different encoding, adjust the
encode()calls accordingly.
内容的提问来源于stack exchange,提问作者ifandroid

