Guide
How to encrypt a PDF with AES in C#
Call EncryptAsync(...) with PdfEncryptionAlgorithm.Aes128 or
PdfEncryptionAlgorithm.Aes256, then save the document.
ZingPDF defaults to RC4-128 for compatibility, so if you specifically need AES output, pass the algorithm explicitly.
The shortest working example
If you want AES-256 output, set the algorithm explicitly before saving.
using ZingPDF;
using var input = File.OpenRead("input.pdf");
using var output = File.Create("protected.pdf");
using var pdf = Pdf.Load(input);
await pdf.EncryptAsync(
"user-password",
"owner-password",
PdfEncryptionAlgorithm.Aes256);
await pdf.SaveAsync(output);
That writes an AES-256 protected output file. Use Aes128 instead if that is the level you
need.
AES-128 vs AES-256
The API supports both AES-128 and AES-256 through the standard security handler.
await pdf.EncryptAsync(
"user-password",
"owner-password",
PdfEncryptionAlgorithm.Aes128);
If you do not pass an algorithm, ZingPDF keeps the default RC4-128 writer behavior for compatibility. For AES output, always pass the algorithm explicitly.
User password and owner password
The first password controls access to the document. The second is the owner password.
If you omit the owner password, ZingPDF uses the same value for both.
await pdf.EncryptAsync(
"shared-password",
algorithm: PdfEncryptionAlgorithm.Aes256);
Encrypting an existing PDF
You can encrypt a plain PDF that you just loaded, or one you have already modified in memory.
If the source PDF is already encrypted, authenticate it first so the document can be rewritten before you save the new protected output.
using var pdf = Pdf.Load(File.OpenRead("already-encrypted.pdf"));
await pdf.AuthenticateAsync("old-password");
await pdf.EncryptAsync(
"new-user-password",
"new-owner-password",
PdfEncryptionAlgorithm.Aes256);
await pdf.SaveAsync(File.Create("re-encrypted.pdf"));
Need to remove protection instead of adding it?
If you already have the password and need to write a plain output file, use the decryption path instead.