Hello I am using Aspose.PDF.dll version 23.6.0.0, on VS Studio 2022 (17.14.23) on Windows 11.
The project is .NET 9.0 WinForms. With the attached PDF document when trying to set the User and Owner password and encrypt the file, using the code below, I get "Aspose.Pdf.InvalidPasswordException: “Invalid password”
SingePageRandomText.pdf (41.6 KB)
image.png (14.0 KB)
private static void SetAsposeTotalsLicence()
{
Aspose.Pdf.License licPdf = new Aspose.Pdf.License();
licPdf.SetLicense("Aspose.Total.lic");
Aspose.Font.License licFont = new Aspose.Font.License();
licFont.SetLicense("Aspose.Total.lic");
}
public static void SecureDocument(string filePath, string userPwd, string ownerPwd, Aspose.Pdf.Facades.DocumentPrivilege documentPrivilege)
{
// Open PDF document
using (var document = new Aspose.Pdf.Document(filePath))
{
string fileName = filePath.Remove(filePath.LastIndexOf("."));
document.Encrypt(userPwd, ownerPwd, documentPrivilege, Aspose.Pdf.CryptoAlgorithm.AESx128,false);
document.Save(fileName + "_out.pdf");
}
}
public static void TestEncryption()
{
string filePath = "D:\\GhostScriptDev\\Source\\SingePageRandomText.pdf";
string userPwd = "user123";
string ownerPwd = "owner123";
SetAsposeTotalsLicence();
// Set document privileges
Aspose.Pdf.Facades.DocumentPrivilege documentPrivilege = Aspose.Pdf.Facades.DocumentPrivilege.ForbidAll;
// Secure the document
SecureDocument(filePath, userPwd, ownerPwd, documentPrivilege);
}
@ShazzadMiah
The Aspose.Pdf.InvalidPasswordException: "Invalid password" you’re encountering when trying to encrypt a PDF using Document.Encrypt may not be caused by incorrect password values, as the strings used (“user123”, “owner123”) are valid .
Based on available information:
-
The input PDF might have existing encryption or security settings that are not immediately visible. Even if the file appears unsecured, Aspose.PDF may detect an inconsistent security state and reject re-encryption, sometimes resulting in misleading exceptions like “Invalid password” .
-
There are user-reported inconsistencies with owner password handling in Aspose.PDF. One report indicates that PDFs created with Adobe Acrobat Pro using different user and owner passwords could not be opened with the owner password using Aspose.PDF, while PDFs created by Aspose.PDF itself worked correctly . This suggests sensitivity to how and where the PDF was generated.
-
Your encryption code using AESx128 and DocumentPrivilege.ForbidAll follows standard practices supported in Aspose.PDF for .NET 23.6.0.0, which includes robust password protection features .
Recommended Steps:
- Check if the input PDF is already encrypted:
using (var document = new Aspose.Pdf.Document(filePath))
{
if (document.IsEncrypted)
{
throw new InvalidOperationException("Input PDF is already encrypted.");
}
}
If IsEncrypted returns true, you must decrypt it first before applying new encryption settings.
- Try normalizing the PDF structure by opening and saving it without encryption:
using (var doc = new Aspose.Pdf.Document(filePath))
{
doc.Save(filePath + "_cleaned.pdf");
}
Then attempt encryption on the cleaned file.
-
Test your encryption logic with a new PDF generated via Aspose.PDF. If encryption succeeds, the issue likely lies in the source or internal structure of the original file.
-
If the problem persists only with specific files, consider sharing the file and test case in this forum thread for further investigation, as it may highlight edge-case behavior in the library.
This behavior could indicate a limitation in Aspose.PDF 23.6.0.0 when processing certain PDFs, especially those originating from external tools, even if they appear unencrypted . Monitoring for updates (e.g., version 24+) may help, as such issues are sometimes resolved in newer releases.
Execution failed due to an execution error, so no runtime validation was performed. The code suggestions remain hypotheses based on user reports and documentation.
Hello, it seems like a bug in this version of Aspose.PDF.dll.
I have tried you code to check if the file is Encrypted already, and it is NOT.
Thanks
@ShazzadMiah
We tested with your code snippet using 25.12 version of the API and did not notice any exception. Attached is the output PDF for your kind reference.
_out.pdf (40.3 KB)
Would you please test using the latest version and let us know if you still face any issues.
Hello
Thank you for your reply.
However, our licence only covers version up to 23.6 and therefore I cannot use the 25.x version.
Would it be possible to apply the fix to version 23.6 and give me a new copy?
Thanks
@ShazzadMiah
Please note that the support is provided on the basis of the latest available version of the API. Furthermore, issues in the older versions are resolved in the higher or latest version of the API. You can please obtain a 30-days free temporary license and use it to evaluate the latest version and once you are satisfied with the results, we recommend you upgrade to it.