Aes

object Aes

Pure-Kotlin AES (FIPS 197), modes ECB-decrypt + CBC-decrypt for PDF V4/V5 Standard Security Handlers. Supports 128-bit and 256-bit keys.

PDF V4 encrypts streams with AES-128-CBC using an IV prepended to the ciphertext (first 16 bytes). PDF V5/V6 uses AES-256-CBC the same way.

Not for general use. This is a textbook implementation: no side-channel hardening, no timing-attack resistance, no AES-NI. It's here because PDF needs decryption to open the document; the underlying threat model is "rightful owner who has the password," not "attacker with timing."

Functions

Link copied to clipboard
fun decryptCbc(key: ByteArray, ciphertext: ByteArray, removePadding: Boolean = true): ByteArray

Decrypt CBC-mode ciphertext. The first 16 bytes are the IV.

Link copied to clipboard

Decrypt one ECB block (used for the key-string encryption in V4).

Link copied to clipboard
fun encryptCbc(key: ByteArray, iv: ByteArray, plaintext: ByteArray): ByteArray

Encrypt CBC-mode plaintext, prepending the IV. PKCS#7 padding.

Link copied to clipboard

Encrypt CBC-mode plaintext with NO padding and NO IV-prepend. The output is exactly the same length as the input, which must already be a multiple of 16 bytes. Used by the R6 Algorithm 2.B hardening loop, which encrypts a length-multiple-of-16 buffer and inspects the raw ciphertext.

Link copied to clipboard

Encrypt one ECB block (used for the V5 /Perms entry, Algorithm 10).