Related to document/download result
Overview
The ZIP returned by document/download is decrypted in two stages: RSA unlocks a 32-byte symmetric key, then that key decrypts the file with Rijndael-256 in CBC mode.
The ZIP contains three files; the fourth (your private key) you already hold:
| File | Content |
|---|---|
keypair.pem | Your RSA-2048 private key (the pair whose public key you sent in the request for signing or eIdentification inpublicKey) |
<id>_OUT.enc.key | Symmetric key, RSA-encrypted, then Base64-encoded |
<id>_OUT.enc.iv | IV as plain text (32 characters, may end with a newline) |
<id>_OUT.enc | File contents: concatenated Base64 chunks of ciphertext |
Parameters at a glance
Parameters at a glance
| Parameter | Value |
|---|---|
| Key decryption | RSA-2048, PKCS#1 v1.5 padding |
| Symmetric key | 32 bytes (256 bits) |
| File cipher | Rijndael, 256-bit key, 256-bit block, CBC mode |
| IV | 32 bytes: the .iv file contents, trimmed, used as-is (not decoded) |
| Padding | None (zero bytes) |
| Encrypted chunk size | 7296 Base64 characters (5472 bytes of ciphertext) |
| Cipher state | Re-initialised with the same key and IV for every chunk |
| Plaintext per chunk | Base64 text; decode it to get the original bytes |
| Output | ZIP file |
Steps
- Unzip the document/download response. You get:
- the .enc
- .enc.key
- .enc.iv files
- Load your RSA private key (keypair.pem, PEM format) with PKCS#1 v1.5 padding.
- Decrypt the symmetric key.
◦ Read .enc.key and Base64-decode it (256 bytes for RSA-2048).
◦ RSA-decrypt it with your private key.
◦ Check the result is exactly 32 bytes. If not check for: wrong private key or wrong padding. - Load the IV. Read .enc.iv, trim whitespace and newlines, and use the text as raw bytes. Check it is exactly 32 bytes.
- Open the files. The .enc file for reading, the output result.zip for binary writing.
- Read the next 7296 characters from the .enc file (the last chunk may be shorter).
- Decrypt the chunk.
◦ Base64-decode the chunk.
◦ Create a new Rijndael-256-CBC cipher with the key from step 3 and the IV from step 4, padding disabled.
◦ Decrypt, then strip trailing \0 bytes. - Base64-decode the result and append the bytes to result.zip.
- Repeat steps 6–8 until the end of the .enc file, then close both files.
- Verify that result.zip opens (e.g. unzip -t result.zip).
## Install: pip install pycryptodome py3rijndael
<?php
require 'vendor/autoload.php';
use phpseclib3\Crypt\RSA;
use phpseclib3\Crypt\Rijndael;
function decrypt_file(string $privateKeyPath, string $keyPath, string $ivPath,
string $encPath, string $outPath): void
{
// Steps 2-3: decrypt the symmetric key
$rsa = RSA::load(file_get_contents($privateKeyPath))->withPadding(RSA::ENCRYPTION_PKCS1);
$key = $rsa->decrypt(base64_decode(file_get_contents($keyPath)));
if (strlen($key) !== 32) throw new Exception('Symmetric key must be 32 bytes');
// Step 4: load the IV
$iv = trim(file_get_contents($ivPath));
if (strlen($iv) !== 32) throw new Exception('IV must be 32 bytes');
// Steps 5-9: decrypt chunk by chunk
$in = fopen($encPath, 'rb');
$out = fopen($outPath, 'wb');
while (($chunk = fread($in, 7296)) !== false && $chunk !== '') {
$cipher = new Rijndael('cbc');
$cipher->setKeyLength(256);
$cipher->setBlockLength(256);
$cipher->setKey($key);
$cipher->setIV($iv);
$cipher->disablePadding();
$plain = rtrim($cipher->decrypt(base64_decode($chunk)), "\0");
fwrite($out, base64_decode($plain));
}
fclose($in);
fclose($out);
}
decrypt_file('keypair.pem', '998159345291_OUT.enc.key', '998159345291_OUT.enc.iv',
'998159345291_OUT.enc', 'result.zip');Other languages:
Use a library that supports a 256-bit Rijndael block size, e.g. Bouncy Castle RijndaelEngine(256) with CBCBlockCipher (Java, .NET).