Decrypt Downloaded Documents

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:

FileContent
keypair.pemYour RSA-2048 private key (the pair whose public key you sent in the request for signing or eIdentification inpublicKey)
<id>_OUT.enc.keySymmetric key, RSA-encrypted, then Base64-encoded
<id>_OUT.enc.ivIV as plain text (32 characters, may end with a newline)
<id>_OUT.encFile contents: concatenated Base64 chunks of ciphertext


Parameters at a glance

ParameterValue
Key decryptionRSA-2048, PKCS#1 v1.5 padding
Symmetric key32 bytes (256 bits)
File cipherRijndael, 256-bit key, 256-bit block, CBC mode
IV32 bytes: the .iv file contents, trimmed, used as-is (not decoded)
PaddingNone (zero bytes)
Encrypted chunk size7296 Base64 characters (5472 bytes of ciphertext)
Cipher stateRe-initialised with the same key and IV for every chunk
Plaintext per chunkBase64 text; decode it to get the original bytes
OutputZIP file

Steps

  1. Unzip the document/download response. You get:
    1. the .enc
    2. .enc.key
    3. .enc.iv files
  2. Load your RSA private key (keypair.pem, PEM format) with PKCS#1 v1.5 padding.
  3. 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.
  4. Load the IV. Read .enc.iv, trim whitespace and newlines, and use the text as raw bytes. Check it is exactly 32 bytes.
  5. Open the files. The .enc file for reading, the output result.zip for binary writing.
  6. Read the next 7296 characters from the .enc file (the last chunk may be shorter).
  7. 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.
  8. Base64-decode the result and append the bytes to result.zip.
  9. Repeat steps 6–8 until the end of the .enc file, then close both files.
  10. 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).