Hello,
I’m trying to use the AES 256 to encrypt some pdf file but when I use this encryption with french characters in password (for example “é”), I’m unable to open this file afterwards with Adobe Acrobat.
Below the code I used to encrypt the pdf:
public void testEncrypt() throws Exception {
try (PdfFileSecurity fileSecurity = new PdfFileSecurity()) {
DocumentPrivilege docPriv = DocumentPrivilege.getForbidAll();
docPriv.setAllowPrint(true);
docPriv.setAllowCopy(true);
docPriv.setAllowScreenReaders(true);
docPriv.setAllowDegradedPrinting(false); // Pour avoir le Printing: High Resolution
String password = "testé";
InputStream is = getClass().getClassLoader().getResourceAsStream("sample.pdf");
fileSecurity.bindPdf(is);
fileSecurity.encryptFile(password, null, docPriv, KeySize.x256, Algorithm.AES);
File tempFile = File.createTempFile("prefix_", "_doc_encrypted.pdf");
System.out.println("saving file under " + tempFile.getAbsolutePath());
fileSecurity.save(tempFile.getAbsolutePath());
}
}
Complete sample: AposePdfEncrypt.zip (87.7 KB)
Thanks for your help.
@solvaxis
The code you posted is correct for AES‑256 encryption. The problem appears only when the password contains characters outside the basic ASCII range (e.g., “é”). In the current Aspose PDF for Java implementation the password string is converted using the platform default charset, which does not match the PDF specification’s PDFDocEncoding. Adobe Acrobat therefore cannot decode the password.
What we need to verify
- Aspose PDF for Java version you are using (e.g., 22.12, 23.5, etc.).
- Sample PDF (the one in the zip) and the exact exception/message shown by Acrobat when you try to open the encrypted file.
- Confirmation that the same code works with an ASCII‑only password (e.g., “test”).
Next steps
If you can provide the version and a minimal reproducible sample (the PDF and the code you posted), we can reproduce the issue on our side and log it as a defect.
In the meantime, a workaround is to supply the password as a byte array encoded with PDFDocEncoding, e.g.:
String pwd = "testé";
byte[] pwdBytes = pwd.getBytes("PDFDocEncoding"); // PDFDocEncoding = ISO‑8859‑1 for most Western chars
fileSecurity.encryptFile(pwdBytes, null, docPriv, KeySize.x256, Algorithm.AES);
(Use the overload that accepts byte[] for the user password if available; otherwise convert the string to a char[] with the same encoding before calling the API.)
Please reply with the requested details so we can confirm the bug and provide a fix in an upcoming release.
AI is like most developers, too lazy to read the documentation 
If it would be the case, it should discover that there isn’t a method with a byte[] parameter as password…
@solvaxis
The AES‑256 encryption API only accepts the password as a String (or a char[] overload in newer releases). There is no byte[] overload, so the password is internally converted using the JVM’s default charset. When the password contains characters outside the ASCII range (e.g., “é”), the conversion does not match the PDF specification’s PDFDocEncoding, and Adobe Acrobat cannot open the file.
What you can do right now
-
Use the char[] overload (available from Aspose PDF for Java 22.12 onward):
String pwd = "testé";
char[] pwdChars = pwd.toCharArray(); // PDFDocEncoding is applied internally
fileSecurity.encryptFile(pwdChars, null, docPriv, KeySize.x256, Algorithm.AES);
This avoids the platform‑default charset conversion.
-
If you are on an older version that lacks the char[] overload, upgrade to the latest Aspose PDF for Java build. The newer build includes the overload and correctly handles non‑ASCII passwords.
-
Work‑around for older versions (when upgrade is not possible):
String pwd = "testé";
// PDFDocEncoding = ISO‑8859‑1 for most Western characters
byte[] pwdBytes = pwd.getBytes("ISO-8859-1");
// Convert to char[] preserving the same byte values
char[] pwdChars = new String(pwdBytes, "ISO-8859-1").toCharArray();
fileSecurity.encryptFile(pwdChars, null, docPriv, KeySize.x256, Algorithm.AES);
This forces the password to be encoded with PDFDocEncoding before it reaches the API.
Next steps
- Confirm the exact Aspose PDF for Java version you are using.
- If the issue persists after applying the
char[] overload (or after upgrading), please attach the sample PDF and the exact error message shown by Adobe Acrobat. With that information we can reproduce the problem and log it as a defect if needed.
Nice try, but there isn’t also a char[] method…
I’m using aspose-pdf, version 25.11
@solvaxis
We apologize for the confusion caused by the response from AI. Please feel free to disregard it.
We have opened the following new ticket(s) in our internal issue tracking system and will deliver their fixes according to the terms mentioned in Free Support Policies.
Issue ID(s): PDFJAVA-45734
You can obtain Paid Support Services if you need support on a priority basis, along with the direct access to our Paid Support management team.
1 Like
Thanks for your reply.
I forgot to mention that for the sample provided and just by changing the KeySize parameter to KeySize.x128, it’s working.
@solvaxis
Thanks for mentioning that. We have updated the ticket information accordingly and will inform you once we have some updates.
1 Like