Encrypting PDFs with AES-256: A Cross-Platform Guide

AES-256 PDF encryption with user and owner passwords

This guide explains how to encrypt PDF with AES-256 using RadaeePDF on Android, iOS, Windows and Server, with the same EncryptAs method on every platform.

Applies to: RadaeePDF Classic and Master SDK for Android, Master SDK for iOS (and the matching Classic iOS API), Master SDK for Windows UWP, and RadaeePDF SDK Server (Java and CLI). Encryption requires a Premium license.

Key takeaways:

  • To encrypt PDF with AES-256 in RadaeePDF, call EncryptAs with method 3 (Acrobat X, V=5, R=6).
  • The user password opens the file; the owner password unlocks every permission.
  • The file ID must be 32 bytes: generate it with a cryptographic random generator.
  • Encryption requires a Premium license on every platform.

Password-protecting a PDF looks like a single operation, but you actually have to decide four things: which passwords to set, which permissions to grant, which cipher to use, and which file ID to write. RadaeePDF gives you one method for all of this on every platform, EncryptAs. This guide compares how it behaves on Android, iOS, Windows and Server, using the API documentation in the official GitHub repositories. If you only target Android, the companion article Encrypt PDF with AES-256 on Android covers that platform in more depth.

How to encrypt PDF with AES-256 using EncryptAs

EncryptAs never changes the open document in place. It writes an encrypted copy to a new path and leaves the source file alone. All editions use the same six parameters:

  1. dst: the path of the encrypted copy.
  2. upswd: the user password.
  3. opswd: the owner password.
  4. perm: the permission flags.
  5. method: selects the cipher (see below).
  6. id: the file ID. It must be 32 bytes long. In the Android, Server and Windows sources it is described as “divided to 2 array in native library, as each 16 bytes”.

The Android, Server and Windows doc comments say the method “require[s] premium license”, and the iOS headers say the same. Without a Premium activation, the call returns false.

User password vs owner password

When you open a protected file, the SDK tries the password you pass as the user password first and then as the owner password. The Open doc comment puts it this way: “first time, SDK try password as user password, and then try password as owner password.”

  • User password. This is the password you share with readers. According to KB article #11, it is “sufficient to open the file with applied permission mask set”.
  • Owner password. It opens the file “without any restriction from the permission mask” (same KB article).

The Android and Server Java docs say both values “can be null”. The command-line tool in RadaeePDF Server refuses to encrypt with an empty owner password, though. Always setting a strong owner password is the safest habit, because without one the permission mask has nothing to fall back on.

Permission bits

The perm parameter uses the permission flags from PDF Reference 1.7. The doc comment for EncryptAs in the Android, Server and Windows sources lists:

  • bits 1-2: reserved
  • bit 3 (0x4): print
  • bit 4 (0x8): modify
  • bit 5 (0x10): extract text or image
  • others: see the PDF reference

A set bit grants the permission. The rdsec command-line tool in RadaeePDF Server passes -1 (every bit set, so everything is allowed). Starting from -1 and clearing the bits you want to block is the simplest way to keep the reserved bits set, as the PDF reference requires. For example, ~(0x8 | 0x10) allows printing but blocks modification and text or image extraction.

To read the flags of a file you opened, call Document.GetPermission() on Android and Server Java (the docs say it needs “a professional or premium license”), or read the Permission property of PDFDoc on Windows.

AES-256 vs AES-128 by platform

According to KB #11, RadaeePDF writes only AES-encrypted output. What changes from platform to platform is how the key length is chosen:

  • Android (Classic and Master), Server Java, Windows UWP. The doc comment reads: “set 3 means using AES 256bits encrypt(Acrobat X), V=5 and R = 6 mode, others AES with V=4 and R=4 mode.” So method = 3 gives AES-256, and any other value gives AES-128. The Server Java Document.java carries exactly the same comment and signature as the Android one, so AES-256 is documented on the Server too.
  • iOS (Classic and Master). In PDFObjc.h and the C header RDPDF.h, the method parameter of encryptAs is documented only as “reserved”. The iOS headers don’t say which value selects AES-256, and the published iOS example (KB #75) passes 4. If AES-256 is a hard requirement on iOS, check the output file’s encryption dictionary (V=5, R=6) in a PDF inspector, or ask RadaeePDF support before you rely on it.
  • Server CLI. rdsec -enc calls the native encryptAs with method 0 and permissions -1, which according to the doc comment above means AES-128. If you need AES-256 on a server, use the Java binding with method = 3.

The original KB #11 sample also uses method = 4 (AES-128). For new code on Android, Windows and Server, pass 3.

Generating the 32-byte file ID

The id must be exactly 32 bytes. Avoid fixed strings such as “123456789abcdefghijklmnopqrstuvw”: every file you encrypt would carry the same ID. Use a cryptographic random generator instead: java.security.SecureRandom on Android and Server, SecRandomCopyBytes on iOS, CryptographicBuffer.GenerateRandom on UWP.

The Server rdsec tool takes a different approach: it reuses the file’s existing ID when there is one and falls back to random bytes when there isn’t. In Java you can do the same with Document.GetID(0) and Document.GetID(1), which return the first and last 16 bytes, or null “if no id for this document”.

Android (Master or Classic), Java

This works unchanged on Master and Classic Android, which share the same EncryptAs signature. Call Global.Init(context) (in com.radaee.comm) with a Premium key before any document call.

import com.radaee.pdf.Document;

import java.security.SecureRandom;

public final class PdfEncryptor {

    /**
     * Writes an AES-256 encrypted copy of srcPath to dstPath.
     * Global.Init(context) must already have activated a Premium license.
     */
    public static boolean encryptAes256(String srcPath, String srcPassword,
                                        String dstPath, String userPassword,
                                        String ownerPassword) {
        Document doc = new Document();
        int err = doc.Open(srcPath, srcPassword);
        if (err != 0) {
            // -1: password needed, -2: unknown encryption,
            // -3: damaged file, -10: access denied or invalid path
            return false;
        }
        try {
            byte[] fileId = new byte[32];
            new SecureRandom().nextBytes(fileId);

            int perm = ~(0x8 | 0x10); // allow print, block modify and extract
            int method = 3;           // AES-256 (V=5, R=6)

            return doc.EncryptAs(dstPath, userPassword, ownerPassword,
                                 perm, method, fileId);
        } finally {
            doc.Close();
        }
    }
}

Server, Java

RadaeePDF SDK Server ships the same com.radaee.pdf.Document class through JNI, so the method above runs unchanged. The only difference is initialization: Server calls Global.Init(serial), which loads the RDDJni native library and activates the key. The demo’s Global.java loads fonts and cmaps from /root/res on Linux, so adjust those paths for your deployment.

import com.radaee.comm.Global;
import com.radaee.pdf.Document;

public class EncryptTool {
    public static void main(String[] args) {
        if (!Global.Init(args[0])) {          // args[0] = Premium serial
            System.err.println("License not activated");
            return;
        }
        boolean ok = PdfEncryptor.encryptAes256(
                args[1], null, args[2], "userPwd", "ownerPwd");
        System.out.println(ok ? "Encrypted" : "Encryption failed");
    }
}

iOS (Master), Objective-C

On iOS the document class is RDPDFDoc, declared in PDFObjc.h. It has no explicit close method: the native document is closed in dealloc, so release your reference when you are done. The license must be active first. The demo projects do this with [RDVGlobal Init].

#import <Security/Security.h>
#import "PDFObjc.h"

BOOL EncryptPDFCopy(NSString *srcPath, NSString *dstPath,
                    NSString *userPwd, NSString *ownerPwd)
{
    RDPDFDoc *doc = [[RDPDFDoc alloc] init];
    int err = [doc open:srcPath :nil];
    if (err != 0) {
        NSLog(@"open failed: %d", err); // -1 needs password
        return NO;
    }

    unsigned char fid[32];
    if (SecRandomCopyBytes(kSecRandomDefault, sizeof(fid), fid) != errSecSuccess) {
        return NO;
    }

    int perm = ~(0x8 | 0x10); // allow print, block modify and extract
    // "method" is documented as reserved in the iOS headers.
    bool ok = [doc encryptAs:dstPath :userPwd :ownerPwd :perm :0 :fid];
    doc = nil; // dealloc closes the native document
    return ok;
}

Classic iOS declares the same encryptAs selector. Its RadaeePDFPlugin wrapper also exposes encryptDocAs:userPwd:ownerPwd:permission:method:idString:, which converts an NSString to a C string as the ID. If you use that wrapper, pass exactly 32 ASCII characters. Swift projects call the Objective-C API through a bridging header.

Windows UWP, C#

The Windows SDK wraps the native engine in the RDDLib.pdf.PDFDoc C++/CX class, declared in RDPDF.h. Its EncryptAs(String dst, String User_pswd, String Owner_pswd, int perm, int method, Array<BYTE> fid) returns false straight away when fid is shorter than 32 bytes. Activate the license first. The demo’s PDFGlobal.init() calls RDGlobal.ActiveLicense(...).

using System.IO;
using System.Threading.Tasks;
using RDDLib.comm;
using RDDLib.pdf;
using Windows.Security.Cryptography;
using Windows.Storage;
using Windows.Storage.Streams;

public static class PdfEncryptor
{
    public static async Task<bool> EncryptAes256Async(StorageFile source,
        string userPassword, string ownerPassword)
    {
        using (IRandomAccessStream stream = await source.OpenAsync(FileAccessMode.Read))
        {
            PDFDoc doc = new PDFDoc();
            RD_ERROR err = doc.Open(stream, "");
            if (err != RD_ERROR.err_ok)
                return false; // err_password, err_encrypt, err_bad_file...

            try
            {
                byte[] fid;
                CryptographicBuffer.CopyToByteArray(
                    CryptographicBuffer.GenerateRandom(32), out fid);

                string dst = Path.Combine(
                    ApplicationData.Current.LocalFolder.Path, "encrypted.pdf");
                int perm = ~(0x8 | 0x10);

                return doc.EncryptAs(dst, userPassword, ownerPassword,
                                     perm, 3, fid);
            }
            finally
            {
                doc.Close();
            }
        }
    }
}

The wrapper reads both password strings directly, so pass non-null values. C++/CX callers use the same PDFDoc methods.

Opening and checking an encrypted file

Every edition reports password problems through the Open return value:

  • 0: opened.
  • -1: a password is needed (or the one you gave is wrong).
  • -2: unknown encryption.
  • -3: damaged or invalid file.
  • -10: access denied or invalid path.

On Windows, Open returns the RD_ERROR enum instead (err_ok, err_password, err_encrypt, err_bad_file, and so on), and the demo prompts for a password when it gets err_password. Once the file is open, IsEncrypted() (Java), isEncrypted (iOS) and the IsEncrypted property (Windows) tell you whether the file carries a security handler.

Removing a password

To decrypt, open the file with a valid password and save a copy without the security handler:

  • Android and Server Java: doc.SaveAs(dstPath, true). The second parameter is documented as “remove security info?” and needs a professional or premium license.
  • iOS: [doc saveAs:dstPath :YES], documented as “save as the document to another file. it remove any security information”.
  • Server CLI: rdsec -dec <file> <pswd> <file1>. Internally it calls the same save-as call with the remove-security flag set.
./rdsec -enc dec.pdf "" enc1.pdf 123456 ""
./rdsec -dec enc1.pdf 123456 plain.pdf

The Windows UWP PDFDoc.SaveAs(String path) has no remove-security parameter, and the Windows header documents no other decryption call.

Common pitfalls

  • No Premium license. EncryptAs returns false. Check your activation result before you look for a bug anywhere else.
  • A file ID that is not 32 bytes. Windows rejects it outright. On the other platforms the length is a documented requirement, so don’t count on a shorter ID working.
  • Assuming method = 3 means AES-256 on iOS. The iOS headers mark method as reserved. Verify the output instead.
  • Writing to an unwritable path. rdsec reports “please check saved path is writeable”, and the API simply returns false.

Frequently asked questions

Which value of the method parameter enables AES-256?

Pass 3. The SDK then uses AES-256 in Acrobat X mode (V=5, R=6). On Android and Server any other value selects AES-128 (V=4, R=4). On iOS the parameter is documented as reserved, so check the output file.

What is the difference between the user password and the owner password?

The user password is required to open the document. The owner password also opens it and grants full permissions, overriding the permission bits set at encryption time.

Do I need a special license to encrypt PDFs with RadaeePDF?

Yes. The SDK documentation marks EncryptAs as a Premium license feature on Android, iOS, Windows and Server.

Related on radaeepdf.com

Where to go next

Secure PDF SDK Workflows for Banking Apps

PDF SDK workflows for statements, contracts and signatures in a banking app

This article explains what a PDF SDK for banking apps has to handle, from statements and loan forms to signatures and encryption, and how RadaeePDF covers each step.

Applies to: RadaeePDF Classic and Master SDK for Android, iOS and Windows UWP, and RadaeePDF SDK Server (Windows and Linux). Code samples target the Master SDK for Android and the Server Java binding.

Key takeaways:

  • A PDF SDK for banking apps keeps statements, contracts and forms inside the app instead of handing them to an external viewer.
  • RadaeePDF covers form filling, signature verification and AES-256 encryption with documented APIs.
  • The same engine runs on Android, iOS, Windows and on Linux or Windows servers for statement generation.
  • Compliance depends on your processes: use this as a planning aid, not legal advice.

A customer who opens an account statement or a mortgage contract in a banking app expects to see it right away, inside the app, without being bounced to another viewer. For the engineering team, that convenience comes with responsibility. Every document carries account numbers, balances and identity data, and many of them need a signature or a password before they can leave the bank.

This article walks through the document steps of a typical banking or insurance app and maps each one to a capability you can verify in the RadaeePDF SDK sources and product pages. It is a planning aid, not legal advice. Regulatory obligations such as data protection rules depend on your own processes, so confirm them with your compliance team.

The document lifecycle in a PDF SDK for banking apps

RadaeePDF’s Bank Operations page lists the typical needs of the sector. Arranged as a workflow, they become five steps:

  1. Generate and archive. Create bank statements and other financial documents as PDF, and convert paper records to PDF for archiving and digital sharing.
  2. Collect data with forms. Let customers fill in loan or financing requests directly in the document form.
  3. Sign and verify. Insert digital signatures on contracts to simplify approval and underwriting, and check signatures on documents you receive.
  4. Protect. Restrict access to sensitive documents with passwords and encryption.
  5. Redact. Remove sensitive data permanently before a document is shared.

The sections below cover what the SDK offers for each step.

1. Statement generation and archiving

Monthly statements are usually produced in batch, on a server. The RadaeePDF SDK Server README describes the server edition as built from native C++ code. It ships prebuilt binaries for Windows (x64, x86) and Ubuntu 22.04/24.04 (x64, with an ARM64 build for 22.04), and offers C#, Java and command-line bindings. The Python binding is listed as “coming soon”.

The README lists creating and editing annotations, reading and writing AcroForm fields, text search and extraction, AES-256 protection, and signing and verifying documents. The Java binding exposes Document.Create(path) to start a new PDF, and its demo project includes a TestPDFCreate.java sample.

For batch jobs that don’t need custom code, the CLI tools cover common tasks:

  • rdpage extracts text, renders pages to images, and deletes, rotates or imports pages.
  • rdsec encrypts, decrypts, signs and watermarks files.
  • rdinfo shows file information and metadata.

Server licensing is per installation. The licensing fact sheet states that each workstation or server is identified by a unique ID, and that the server license “does not restrict concurrent processes or applications invoking the runtime”.

2. Loan and financing applications with AcroForms

The product pages for Android, iOS and Windows UWP say the same thing: if a PDF contains AcroForm fields, users can fill them in through the viewer UI, or your code can fill them programmatically. The pages also mention creating new AcroForm fields in existing documents.

In the Android Java API, form fields are annotations. Page.Annotation.GetFieldType() tells you the field type: 1 is a button, 2 a text field, 3 a choice field, 4 a signature field. You then write values with SetEditText, SetComboItem or SetCheckValue. All three require a Premium license.

To hand the data to a back end, Document.ExportForm() returns the form data as an XML string and Document.ExportXFDF(href) returns XFDF. Your loan system can then process the submitted values without scraping the PDF.

3. Contract signing and signature verification

Signatures are where a banking workflow gains or loses trust. The SDK covers both sides:

  • Signing. Page.Annotation.SignField(form, cert_file, pswd, name, reason, location, contact) signs an empty signature field with a .p12 or .pfx certificate and requires a Premium license. It returns 0 on success and negative codes for problems such as “cert file open failed” (-5). On servers, rdsec -sig does the same from the command line.
  • Verification. Document.VerifySign(sign) checks a signature object taken from a signature field. Per its doc comment, it returns “0 if verify OK, others are error” and needs a Premium license.

The following method lists every signature field in a document and verifies the signed ones. It targets the Master SDK for Android. The same classes and methods exist in the Server Java binding (com.radaee.pdf), so it compiles there too. Activate the license first: Global.Init(context) on Android, Global.Init(serial) on Server.

import com.radaee.pdf.Document;
import com.radaee.pdf.Page;
import com.radaee.pdf.Sign;

public final class SignatureChecker {

    /** Prints the status of every signature field. Returns false if the file cannot be opened. */
    public static boolean checkSignatures(String path, String password) {
        Document doc = new Document();
        int err = doc.Open(path, password);
        if (err != 0) {
            System.out.println("Open failed: " + err); // -1 = password required
            return false;
        }
        try {
            int pageCount = doc.GetPageCount();
            for (int p = 0; p < pageCount; p++) {
                Page page = doc.GetPage(p);
                if (page == null) continue;
                page.ObjsStart();
                int annotCount = page.GetAnnotCount();
                for (int i = 0; i < annotCount; i++) {
                    Page.Annotation annot = page.GetAnnot(i);
                    if (annot == null || annot.GetFieldType() != 4) continue; // 4 = signature field
                    String field = annot.GetFieldFullName();
                    if (annot.GetSignStatus() != 1) {                         // 1 = signed
                        System.out.println(field + ": not signed");
                        continue;
                    }
                    Sign sign = annot.GetSign();
                    if (sign == null) continue;
                    int result = doc.VerifySign(sign);
                    System.out.println(field + " signed by " + sign.GetName()
                            + " on " + sign.GetModDateTime()
                            + (result == 0 ? ": valid" : ": verification error " + result));
                }
                page.Close();
            }
            return true;
        } finally {
            doc.Close();
        }
    }
}

Document.GetPerm() adds another check. It reads the Perm entry of the catalog, which according to the doc comment “mostly … means the permission from signature”. A value of 1 means the document can’t be modified.

4. Protecting statements with passwords and AES encryption

Document.EncryptAs(dst, upswd, opswd, perm, method, id) writes an encrypted copy of a document and requires a Premium license. On Android, Windows UWP and the Server Java binding, method = 3 selects AES-256 (Acrobat X, V=5/R=6), and any other value selects AES-128 (V=4/R=4). The iOS headers document method as reserved. The id must be 32 bytes, so generate it with a cryptographic random source:

import com.radaee.pdf.Document;

import java.security.SecureRandom;

public final class StatementProtector {

    public static boolean protectStatement(String src, String dst,
                                           String customerPwd, String bankOwnerPwd) {
        Document doc = new Document();
        if (doc.Open(src, null) != 0) return false;
        try {
            byte[] fileId = new byte[32];
            new SecureRandom().nextBytes(fileId);
            int perm = ~(0x8 | 0x10); // allow print, block modify and text/image extraction
            return doc.EncryptAs(dst, customerPwd, bankOwnerPwd, perm, 3, fileId);
        } finally {
            doc.Close();
        }
    }
}

The customer opens the file with the user password and gets the permission mask you set. The bank keeps the owner password, which opens the file without restrictions. For a step-by-step walkthrough, see Encrypt PDF with AES-256 on Android and the Encrypt a PDF knowledge base article.

The rest of the security picture is outside the PDF. Store passwords and keys in the platform keystore and never in the app bundle. The PDF permission mask is honored by compliant viewers, but it doesn’t replace access control on your servers.

For on-screen deterrence, rdsec -wm adds a PNG watermark to every page of a file on the server, for example before a statement is exported.

5. Redaction

Redaction is the step where the original draft promised the most. The Bank Operations page lists redaction, “permanently removing all sensitive data included in a PDF document”. However, the API documentation in the public demo repositories for Android, iOS, Windows and Server doesn’t contain a redaction method. If your workflow depends on true redaction, which removes the underlying text and images rather than covering them with a black box, ask RadaeePDF which edition and API provide it before you design around it. Whether the result meets a specific privacy regulation depends on your process, not only on the tool.

Why native, in-app rendering matters

According to the product pages, the Android SDK’s Java framework runs on “a fast and stable native library optimized to work on ARM, x86 architectures (both 32bit and 64bit)”. The iOS and Windows frameworks sit on the same C++ library. The pages also list opening encrypted PDF files, and loading documents from local storage, HTTP/HTTPS and cloud sources.

For a banking app, the practical benefit is control. The document is parsed and rendered by a library inside your own app process, so you never hand the file to a third-party viewer through a share sheet or intent. The licensing terms reinforce this design. The SDK and its activation key “cannot be included in any external plugins, software modules, or printer drivers (i.e., via Android intent mechanisms)”.

What to decide before you build

  • One license per app identifier. Each license covers one application, identified by its package name, bundle ID or UUID, on one platform. Android build flavors with different applicationId values each need their own key, as KB #126 explains.
  • No shared modules. You cannot package the SDK as a shared plugin or module used by several of the bank’s apps.
  • Demo builds are not production builds. Unlicensed builds “impose limits on certain APIs and display a Standard Watermark”. For staging, testing and training, the fact sheet describes Not-For-Resale (NFR) licenses at 15% of the standard price. NFR builds must never reach production.
  • Private distribution needs an OEM-like license. Distribution through private channels requires one. That includes enterprise distribution through the App Store or Google Play, private websites, email and cloud storage. The license is subject to a qualification interview and a EULA addendum.
  • Server and mobile together. If statements are generated on a server and opened on phones, budget for both a server license and a mobile license per app and platform.

Frequently asked questions

Can RadaeePDF verify digital signatures in a banking app?

Yes. The Master SDK for Android and the Server Java binding expose signature fields and a signature verification call, so the app can check that a signed contract has not been altered.

Can statements be protected with AES-256?

Yes. EncryptAs with method 3 writes an AES-256 encrypted copy with user and owner passwords and permission bits. It requires a Premium license.

Can the same engine generate statements on the server?

Yes. The RadaeePDF Server SDK runs the same native engine on Windows and Linux, so statements can be generated, rendered and protected in backend jobs before they reach the mobile app.

Related on radaeepdf.com

Where to go next

Encrypt PDF with AES-256 on Android

Encrypting a PDF with AES-256 on an Android phone

Here is how to encrypt PDF on Android with AES-256 using the RadaeePDF Master SDK, and how to check that the encrypted copy really is protected.

Applies to: RadaeePDF Master SDK for Android

Short answer: Call Document.EncryptAs() with the method parameter set to 3 to enable AES-256 encryption (Acrobat X standard). You must also provide a 32-byte ID array and a valid Premium license for the SDK.

How to encrypt PDF on Android with AES-256

Developers often assume that PDF encryption is a single, monolithic operation. In the RadaeePDF SDK, however, the EncryptAs method gives you granular control over the cryptographic standard used. This is critical because the PDF specification (ISO 32000) defines different security handlers.

The method parameter in EncryptAs dictates which security handler is applied:

  • method = 3: Uses AES-256 encryption. This corresponds to the Acrobat X standard (V=5, R=6). This is the strongest encryption available in the PDF spec and is what you want for modern security compliance.
  • method = 4: Uses AES-128 encryption (V=4, R=4). This is the older standard.

If you pass 4 when you intend to use AES-256, your document will be protected, but with a weaker key size. Conversely, if you pass 3, the SDK will generate the appropriate encryption dictionary in the PDF header to signal AES-256 support to other PDF readers.

Additionally, the SDK requires a Premium license to perform any encryption. If you are using the free or standard edition, the EncryptAs call will fail silently or return false. This is a common source of confusion for developers testing in a development environment without a valid license key.

The fix

To encrypt a PDF with AES-256, you need to prepare a 32-byte ID array. This ID acts as a seed for the encryption process. The SDK documentation specifies that this ID is split into two 16-byte arrays internally by the native library.

Here is the complete implementation in Java for the RadaeePDF Master SDK for Android:

import com.radaee.pdf.Document;

public class PdfEncryptionExample {
    public static boolean encryptPdfWithAes256(String inputPath, String outputPath) {
        // 1. Prepare the 32-byte ID (encryption seed)
        // This string is exactly 32 characters long.
        // In a production app, you might generate this randomly or use a fixed identifier.
        String idString = "123456789abcdefghijklmnopqrstuvw";
        byte[] id = idString.getBytes();

        // 2. Initialize the Document object
        Document doc = new Document();

        // 3. Open the source PDF
        // The second parameter is the password if the source is already encrypted.
        // Pass null if the source is not encrypted.
        int openResult = doc.Open(inputPath, null);
        if (openResult != 0) {
            System.err.println("Failed to open PDF: " + openResult);
            return false;
        }

        // 4. Define permissions
        // Bit 3 (0x4): Allow printing
        // Bit 4 (0x8): Allow modifying content
        // Bit 5 (0x10): Allow extracting text/images
        // Here we set perm = 4, which allows ONLY printing.
        // If you want to allow all standard permissions, you would OR these flags.
        int perm = 0x4; // Print only

        // 5. Encrypt and save
        // Parameters:
        // dst: Output file path
        // upswd: User password (can be null for no user password)
        // opswd: Owner password (can be null for no owner password)
        // perm: Permission flags
        // method: 3 for AES-256 (Acrobat X), 4 for AES-128
        // id: 32-byte array
        boolean success = doc.EncryptAs(
            outputPath,
            "userPassword123", // User password
            "ownerPassword456", // Owner password
            perm,
            3, // AES-256
            id
        );

        // 6. Close the document
        doc.Close();

        return success;
    }
}

Verifying the Encryption

After calling EncryptAs, you can verify that the document is encrypted by opening the new file and checking the IsEncrypted method:

Document doc = new Document();
doc.Open("/sdcard/test_encrypted.pdf", "userPassword123");
if (doc.IsEncrypted()) {
    System.out.println("Document is successfully encrypted.");
} else {
    System.out.println("Document is NOT encrypted.");
}
doc.Close();

Common pitfalls

  1. ID Length: The id parameter must be exactly 32 bytes. If you pass a shorter or longer array, the encryption will fail. The example above uses a 32-character string, which converts to 32 bytes in UTF-8.
  2. License Requirement: EncryptAs requires a Premium license. If you are using the free version, the method will return false. Ensure your app has a valid license initialized.
  3. Method Confusion: Do not confuse method=3 (AES-256) with method=4 (AES-128). If you are targeting modern security standards, always use 3.
  4. Permissions: The perm parameter is a bitmask. If you set it to 0, no permissions are granted (the user can only view). If you want to allow printing and editing, you would use 0x4 | 0x8 (which equals 12).

Related questions

How do I enable AES-256 specifically instead of standard AES?

You enable AES-256 by passing the integer 3 to the method parameter of the Document.EncryptAs method. This value corresponds to the Acrobat X security handler (V=5, R=6), which uses AES-256. If you pass 4, the SDK will use the older AES-128 standard (V=4, R=4). There is no separate flag for AES-256; the method parameter is the sole determinant of the encryption strength.

What are the valid permission flags for restricting printing or editing?

The permission flags are defined in the PDF Reference 1.7 specification and are passed as an integer bitmask to the perm parameter. The key bits are:

  • Bit 3 (0x4): Allow printing the document.
  • Bit 4 (0x8): Allow modifying document contents.
  • Bit 5 (0x10): Allow extracting text or images.

To restrict printing, you simply do not include bit 3 in your mask. For example, to allow only editing but not printing, you would use perm = 0x8. To allow all three, use perm = 0x4 | 0x8 | 0x10 (which equals 28).

Is encryption available in the free version of the SDK?

No. The EncryptAs method is only available in the Premium edition of the RadaeePDF SDK. If you are using the free or standard license, the method will not function correctly and will likely return false. You must purchase a Premium license and initialize it in your application before calling encryption methods.

How can I verify that a PDF has been successfully encrypted?

You can verify encryption by opening the encrypted file with Document.Open() and then calling the IsEncrypted() method. If the method returns true, the document is encrypted.

Related on radaeepdf.com

See also

For more details on the EncryptAs method and license requirements, refer to the official documentation in the RadaeePDF-Master-Android repository and the Encrypt a PDF KB article.