README

#pdfcrypt

PDF encryption and decryption operations.

#Overview

The pdfcrypt package provides:

  • Encryption methods (AES, RC4)
  • Permission flags for document restrictions
  • Decryption of encrypted documents
  • Encryption of output documents

#Encryption Methods

///|
pub(all) enum EncryptionMethod {
AES256 // AES 256-bit (recommended, PDF 2.0)
AES128 // AES 128-bit (PDF 1.6+)
ARC4(Int) // RC4 with key length (legacy)
}

#Permissions

Control what users can do with encrypted documents:

///|
pub(all) enum Permission {
Print // Allow printing
Edit // Allow editing
Copy // Allow copying text
Annot // Allow annotations
Forms // Allow form filling
Extract // Allow accessibility extraction
Assemble // Allow page assembly
Hqprint // Allow high-quality printing
}

#Getting Permissions

let perms = @pdfread.PdfRead::new().permissions(pdf)
for perm in perms {
match perm {
@pdfcrypt.Permission::Print => println("Print allowed")
@pdfcrypt.Permission::Copy => println("Copy allowed")
_ => ()
}
}

#Permission Conversion

// Convert permission flags to/from integer

///|
let banlist = @pdfcrypt.PdfCrypt::new().banlist_of_p(flags)

#Encrypting Documents

When writing PDFs, use @pdfwrite with encryption:

let encryption = @pdfwrite.Encryption::new(
@pdfcrypt.EncryptionMethod::AES256,
user_password="user123",
owner_password="owner456",
permissions=[
@pdfcrypt.Permission::Print,
@pdfcrypt.Permission::Copy,
],
)

@pdfwritefs.PdfWriteFs::new().pdf_to_file_options(
encryption=Some(encryption),
build_new_id=true,
pdf~,
filename="encrypted.pdf",
)

#Decrypting Documents

When reading encrypted PDFs:

// With user password

///|
let pdf = @pdfreadfs.PdfReadFs::new().pdf_of_file(
user_password=Some("user123"),
owner_password=None,
filename="encrypted.pdf",
)

// With owner password (full access)

///|
let pdf = @pdfreadfs.PdfReadFs::new().pdf_of_file(
user_password=None,
owner_password=Some("owner456"),
filename="encrypted.pdf",
)

#Checking Encryption

let method = @pdfread.PdfRead::new().what_encryption(pdf) match method { None => println("Not encrypted") Some(@pdfcrypt.EncryptionMethod::AES256) => println("AES 256-bit") Some(@pdfcrypt.EncryptionMethod::AES128) => println("AES 128-bit") Some(@pdfcrypt.EncryptionMethod::ARC4(40)) => println("RC4 40-bit") Some(@pdfcrypt.EncryptionMethod::ARC4(128)) => println("RC4 128-bit") _ => println("Other encryption") }

#Security Notes

  • AES256 is recommended for new documents
  • ARC4 is considered legacy and less secure
  • User password allows restricted access based on permissions
  • Owner password allows full access regardless of permissions

#
EncryptionMethod

pub(all) enum EncryptionMethod {
PDF40bit
PDF128bit
AES128bit(Bool)
AES256bit(Bool)
AES256bitISO(Bool)
AlreadyEncrypted
}

Encryption methods.

#
PdfCrypt

pub struct PdfCrypt {
unit : Unit
}

#
PdfCrypt::banlist_of_p

fn PdfCrypt::banlist_of_p(_self : PdfCrypt, p : Int) -> Array[Permission]

Parse a /P value, giving the permissions.

#
PdfCrypt::decrypt_pdf

fn PdfCrypt::decrypt_pdf(self : PdfCrypt, user_pw : String, pdf :
Pdf
, keyfromowner? : String) -> (
Pdf
?, Array[Permission]) raise

Decrypt a PDF document using the user password.

#
PdfCrypt::decrypt_pdf_owner

fn PdfCrypt::decrypt_pdf_owner(self : PdfCrypt, owner_pw : String, pdf :
Pdf
) ->
Pdf
? raise

Decrypt a PDF document using the owner password.

#
PdfCrypt::decrypt_single_stream

fn PdfCrypt::decrypt_single_stream(self : PdfCrypt, user_pw : String?, owner_pw : String?, pdf :
Pdf
, obj : Int, gen : Int, stream :
PdfObject
) ->
PdfObject
raise

Just decrypt a single stream, given the user password and pdf.

#
PdfCrypt::encrypt_pdf_128bit

fn PdfCrypt::encrypt_pdf_128bit(_self : PdfCrypt, user_pw : String, owner_pw : String, banlist : Array[Permission], pdf :
Pdf
) ->
Pdf
raise

Encrypt a PDF document using 128-bit encryption.

#
PdfCrypt::encrypt_pdf_40bit

fn PdfCrypt::encrypt_pdf_40bit(_self : PdfCrypt, user_pw : String, owner_pw : String, banlist : Array[Permission], pdf :
Pdf
) ->
Pdf
raise

Encrypt a PDF document using 40-bit encryption.

#
PdfCrypt::encrypt_pdf_AES

fn PdfCrypt::encrypt_pdf_AES(_self : PdfCrypt, encrypt_metadata : Bool, user_pw : String, owner_pw : String, banlist : Array[Permission], pdf :
Pdf
) ->
Pdf
raise

Encrypt a PDF document using AES V2.

#
PdfCrypt::encrypt_pdf_AES256

fn PdfCrypt::encrypt_pdf_AES256(_self : PdfCrypt, encrypt_metadata : Bool, user_pw : String, owner_pw : String, banlist : Array[Permission], pdf :
Pdf
) ->
Pdf
raise

Encrypt a PDF document using AESV3 (non-ISO).

#
PdfCrypt::encrypt_pdf_AES256ISO

fn PdfCrypt::encrypt_pdf_AES256ISO(_self : PdfCrypt, encrypt_metadata : Bool, user_pw : String, owner_pw : String, banlist : Array[Permission], pdf :
Pdf
) ->
Pdf
raise

Encrypt a PDF document using AESV3 (ISO).

#
PdfCrypt::get_encryption_values

fn PdfCrypt::get_encryption_values(_self : PdfCrypt, pdf :
Pdf
) -> (
Encryption
, String, String, Int, String, String?, String?) raise

Read encryption parameters from an encrypted PDF.

#
PdfCrypt::is_encrypted

fn PdfCrypt::is_encrypted(_self : PdfCrypt, pdf :
Pdf
) -> Bool

Is a PDF encrypted?

#
PdfCrypt::new

fn PdfCrypt::new() -> PdfCrypt

#
PdfCrypt::recrypt_pdf

fn PdfCrypt::recrypt_pdf(self : PdfCrypt, pdf :
Pdf
, pw : String, renumber? : Bool) ->
Pdf
raise

Re-encrypt a PDF document, using the saved encryption metadata.

#
Permission

pub(all) enum Permission {
NoEdit
NoPrint
NoCopy
NoAnnot
NoForms
NoExtract
NoAssemble
NoHqPrint
}

Permissions for PDF encryption.
impl Eq for Permission
impl Debug for Permission

Powered by MoonBit

Site sourceReport issuePackagesBuild queueSkillsStatistics

© 2026 mooncakes.io