This guide provides a step-by-step explanation of the decryption process for the encrypted file in the result of document/download. The scheme combines RSA (asymmetric encryption) to encrypt a symmetric key and AES (symmetric encryption) in CBC mode to encrypt the file contents. The goal is to decrypt an encrypted file (998159345291_OUT.enc) into a decrypted output file (result.zip) using the provided RSA private key, encrypted symmetric key, and initialization vector (IV).
After the document/download request - you will receive a response for a .zip file containing the required files.
Required Files:
RSA private key (e.g., keypair.pem in PEM format).
Encrypted symmetric key (e.g., 998159345291_OUT.enc.key, base64-encoded).
Initialization vector (IV, e.g., 998159345291_OUT.enc.iv, plaintext or trimmed text).
Encrypted file (e.g., 998159345291_OUT.enc, containing base64-encoded AES ciphertext).
Cryptographic Library: Use a library supporting RSA and AES (e.g., OpenSSL, PyCryptodome for Python, Crypto++ for C++, or Java's JCE).
Hybrid Encryption: The file uses a hybrid approach:
A 256-bit symmetric key (AES key) was used to encrypt the file contents.
The symmetric key was encrypted with an RSA public key.
The file is encrypted using AES-256 in CBC mode, with a fixed IV.
File Structure:
The encrypted file (998159345291_OUT.enc) contains base64-encoded AES ciphertext.
The encrypted symmetric key (998159345291_OUT.enc.key) is base64-encoded.
The IV (998159345291_OUT.enc.iv) is plaintext (trimmed of whitespace).
The original plaintext (before encryption) is base64-encoded, requiring an additional decode after decryption.
Output: The decrypted file will be a ZIP file (result.zip).
Objective: Read and parse the RSA private key to decrypt the symmetric key.
Steps:
Read the private key file (e.g., keypair.pem) into memory.
Parse the key using your cryptographic library (e.g., load as PEM format).
Configure RSA decryption with PKCS1 padding (RSA PKCS1 encryption was likely used to encrypt the symmetric key).
Example Libraries:
Python (PyCryptodome): RSA.import_key(open("keypair.pem").read()).
OpenSSL (CLI): Use openssl rsautl -decrypt -inkey keypair.pem.
Java (JCE): KeyFactory.getInstance("RSA").generatePrivate() with a PEM parser.
Security Note: Ensure the private key is stored securely and not exposed.
Objective: Use the RSA private key to decrypt the encrypted symmetric key.
Steps:
Read the encrypted symmetric key file (998159345291_OUT.enc.key).
Base64-decode the contents to get the raw encrypted key (typically 256 bytes for RSA-2048).
Decrypt the key using the RSA private key with PKCS1 padding.
Verify the decrypted key is 32 bytes (256 bits) for AES-256. If not, there may be an issue with the key or padding.
Output: A 256-bit (32-byte) AES symmetric key.
Error Handling: If decryption fails, check the private key, ensure the encrypted key matches the public key used, or verify padding.
Objective: Retrieve the IV for AES-CBC decryption.
Steps:
Read the IV file (998159345291_OUT.enc.iv).
Trim any whitespace (e.g., newlines) to get the raw IV.
Ensure the IV is 16 bytes (128 bits) for AES-CBC (standard AES block size).
Note: If the IV is base64-encoded or in another format, decode it accordingly. The provided code assumes plaintext.
Steps:
Open the encrypted file (998159345291_OUT.enc) in read mode and the output file (result.zip) in binary write mode.
Read the encrypted file in chunks (e.g., 7296 bytes, or adjust based on encryption chunking).
For each chunk:
Base64-decode the chunk to get the raw AES ciphertext.
Initialize an AES-256-CBC cipher with the decrypted symmetric key and IV.
Use PKCS7 padding (standard for AES).
Decrypt the ciphertext to obtain plaintext.
Trim trailing null bytes.
Base64-decode the plaintext.
Write the final plaintext to the output file.
Close both files.
