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
- ID Length: The
idparameter 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. - License Requirement:
EncryptAsrequires a Premium license. If you are using the free version, the method will returnfalse. Ensure your app has a valid license initialized. - Method Confusion: Do not confuse
method=3(AES-256) withmethod=4(AES-128). If you are targeting modern security standards, always use3. - Permissions: The
permparameter is a bitmask. If you set it to0, no permissions are granted (the user can only view). If you want to allow printing and editing, you would use0x4 | 0x8(which equals12).
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
- RadaeePDF SDK for Android
- AES-256 encryption on iOS, Windows and Server
- Fill and flatten PDF form fields on Android and iOS
- Download the free trial
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.