加密解密时出现最终块填充错误,求代码修复方案
Hey there! Let’s work through this encryption/decryption snag together—you’ve already got your 16-bit key and random message sorted, so the issue is almost certainly in how your encryption logic is implemented. Let’s break this down step by step:
Let’s start with the most common pitfalls that cause encryption errors:
- Key Length vs. Algorithm Compatibility: When you say "16-bit key," do you mean 2 bytes? Most standard encryption libraries (like those for AES) expect 128-bit (16-byte) keys by default. If you’re passing a 2-byte key to an algorithm designed for 16 bytes, that’s a guaranteed error. This is a super common mix-up—double-check your key generation code to confirm you’re not generating a 2-byte key when you meant 16 bytes.
- Encoding Consistency: Are you converting your random string message to bytes using the same encoding (e.g., UTF-8) for both encryption and decryption? Mismatched encodings will turn valid plaintext into garbage during decryption, or throw encoding errors upfront.
- Block Cipher Padding: If you’re using a block cipher (like AES), your input message must be a multiple of the block size (16 bytes for AES). If your random message isn’t padded properly (e.g., with PKCS#7 padding) before encryption, the library will throw errors or produce un-decryptable data.
Let’s fix the encryption first before tackling brute-force. Below is a simplified Python example using the cryptography library (adjust to your language if needed) that works with a 128-bit (16-byte) key (the standard for AES):
from cryptography.hazmat.primitives.ciphers import Cipher, algorithms, modes from cryptography.hazmat.backends import default_backend import os # Generate 16-byte (128-bit) key (standard for AES) key = os.urandom(16) # Random message (convert string to bytes with consistent encoding) message = "This is my random test string".encode("utf-8") # Add PKCS#7 padding to match AES block size (16 bytes) pad_length = 16 - len(message) % 16 padded_message = message + bytes([pad_length]) * pad_length # Use AES in CBC mode (requires an Initialization Vector/IV) iv = os.urandom(16) # Generate a unique IV for each encryption cipher = Cipher(algorithms.AES(key), modes.CBC(iv), backend=default_backend()) # Encrypt encryptor = cipher.encryptor() ciphertext = encryptor.update(padded_message) + encryptor.finalize() # Decrypt to verify decryptor = cipher.decryptor() decrypted_padded = decryptor.update(ciphertext) + decryptor.finalize() # Remove padding pad_length = decrypted_padded[-1] decrypted_message = decrypted_padded[:-pad_length].decode("utf-8") print(f"Original message: {message.decode('utf-8')}") print(f"Decrypted message: {decrypted_message}")
If you really need to use a 16-bit (2-byte) key, you’ll have to use a stream cipher (like RC4) or a custom implementation—standard libraries don’t support 2-byte keys for secure block ciphers, which is likely a source of your errors.
- Missing IV/Nonce: Modes like CBC or CTR require a unique initialization vector (IV) for each encryption. Forgetting to generate/pass an IV, or reusing the same IV, will break decryption.
- Skipping Finalization: Most cipher implementations require calling a
finalize()method to process leftover data. Skipping this will result in incomplete ciphertext or decrypted data. - Invalid Key Format: Ensure your key is stored as bytes, not a string. Passing a string key directly to an encryption function will often cause encoding or length errors.
Once your encryption/decryption is stable, brute-forcing a 16-bit (2-byte) key is totally feasible (only 65,536 possible combinations). Here’s a rough outline:
- Iterate through all possible 2-byte keys (from
0x0000to0xFFFF). - For each key, attempt to decrypt the ciphertext.
- Validate the decrypted output (e.g., check for readable text, valid padding, or a known plaintext snippet).
Just note that even with 65k keys, brute-force can take a few seconds depending on your hardware, but it’s manageable with a simple loop.
内容的提问来源于stack exchange,提问作者John Doe




