On Wed, 2022-02-02 at 14:38 +0100, Christian Sakowski wrote: > I cannot open the PDF with the password „user“!
Hi, neither I can open it, using evince. It rejects both passwords. By any chance, is your compiler/source-code using UTF-16 instead of ASCII/ UTF-8? I'm only wondering whether the constant is like wchar_t or char. Anyway, attached is my little test code, which does work. I attached also one of the result PDF files. They do work properly here. Did you try the podofoencrypt tool, if you compile it? Bye, zyx By the way, 500KB+ mail attachment is a very bad idea, especially on the mailing lists. You should avoid that, in general.
#include <stdio.h> #include "podofo/podofo.h" using namespace PoDoFo; static void drawPage(PdfMemDocument *memDocument) { PODOFO_RAISE_LOGIC_IF (!memDocument, "!memDocument"); PdfFont *pFont; PdfPage *pPage; PdfPainter painter; pPage = memDocument->CreatePage(PdfPage::CreateStandardPageSize(ePdfPageSize_A4)); PODOFO_RAISE_LOGIC_IF (!pPage, "!pPage"); painter.SetPage(pPage); pFont = memDocument->CreateFont("Helvetica"); PODOFO_RAISE_LOGIC_IF (!pFont, "!pFont"); pFont->SetFontSize(18.0); painter.SetFont(pFont); painter.DrawText(56.69, pPage->GetPageSize().GetHeight() - 56.69, "Hello World! Encrypted"); painter.FinishPage(); } static void testEnctyption2(PdfEncrypt::EPdfEncryptAlgorithm algo, PdfEncrypt::EPdfKeyLength keyLength, const char *pFileName) { PdfEncrypt *encrypt; if (pFileName) printf("Testing %s:\n", pFileName); encrypt = PdfEncrypt::CreatePdfEncrypt("user", "owner", 0, algo, keyLength); PODOFO_RAISE_LOGIC_IF (!encrypt, "!encrypt"); PdfMemDocument *memDocument = new PdfMemDocument(); PODOFO_RAISE_LOGIC_IF (!memDocument, "!memDocument"); memDocument->SetEncrypted(*encrypt); delete encrypt; PdfInfo *info = memDocument->GetInfo(); PODOFO_RAISE_LOGIC_IF (!info, "!info"); info->SetProducer("test"); drawPage(memDocument); PdfRefCountedBuffer *buffer = new PdfRefCountedBuffer(); PODOFO_RAISE_LOGIC_IF (!buffer, "!buffer"); PdfOutputDevice outputDevice(buffer); memDocument->Write(&outputDevice); delete memDocument; if (pFileName) { FILE *f = fopen(pFileName, "wb"); PODOFO_RAISE_LOGIC_IF (!f, "!f"); fwrite(buffer->GetBuffer(), buffer->GetSize(), 1, f); fclose(f); } memDocument = new PdfMemDocument(); PODOFO_RAISE_LOGIC_IF (!memDocument, "!memDocument (2)"); PdfRefCountedInputDevice inputDevice(buffer->GetBuffer(), buffer->GetSize()); try { memDocument->LoadFromDevice(inputDevice); } catch (const PdfError &error) { if (error.GetError() == ePdfError_InvalidPassword) { memDocument->SetPassword("user"); } else { throw; } } info = memDocument->GetInfo(); PODOFO_RAISE_LOGIC_IF (!info, "!info"); if (info->GetProducer().GetStringUtf8() != "test") { fprintf(stderr, " Producer not set as expected 'test', but '%s' instead\n", info->GetProducer().GetString()); } else { printf(" OK\n"); } delete memDocument; delete buffer; } static void testEnctyption1(PdfEncrypt::EPdfEncryptAlgorithm algo, PdfEncrypt::EPdfKeyLength keyLength, const char *pFileName) { PODOFO_RAISE_LOGIC_IF (!pFileName, "!pFileName"); printf("Testing %s:\n", pFileName); PdfMemDocument *memDocument = new PdfMemDocument(); PODOFO_RAISE_LOGIC_IF (!memDocument, "!memDocument"); try { memDocument->Load(pFileName); } catch (const PdfError &error) { if (error.GetError() == ePdfError_InvalidPassword) { memDocument->SetPassword("user"); } else { throw; } } PdfInfo *info = memDocument->GetInfo(); PODOFO_RAISE_LOGIC_IF (!info, "!info"); if (info->GetProducer().GetStringUtf8() != "test") { fprintf(stderr, " Producer not set as expected 'test', but '%s' instead\n", info->GetProducer().GetString()); } else { printf(" OK\n"); } delete memDocument; } int main(void) { PdfError::EnableLogging(false); printf ("Check existing files\n"); try { testEnctyption1(PdfEncrypt::ePdfEncryptAlgorithm_RC4V1, PdfEncrypt::ePdfKeyLength_40, "rc4v1.pdf"); testEnctyption1(PdfEncrypt::ePdfEncryptAlgorithm_RC4V2, PdfEncrypt::ePdfKeyLength_128, "rc4v2.pdf"); testEnctyption1(PdfEncrypt::ePdfEncryptAlgorithm_AESV2, PdfEncrypt::ePdfKeyLength_128, "aesv2.pdf"); testEnctyption1(PdfEncrypt::ePdfEncryptAlgorithm_AESV3, PdfEncrypt::ePdfKeyLength_256, "aesv3.pdf"); } catch (const PdfError &error) { fprintf (stderr, "Failed with error:\n"); error.PrintErrorMsg(); } printf ("Check new files\n"); try { testEnctyption2(PdfEncrypt::ePdfEncryptAlgorithm_RC4V1, PdfEncrypt::ePdfKeyLength_40, "rc4v1.pdf"); testEnctyption2(PdfEncrypt::ePdfEncryptAlgorithm_RC4V2, PdfEncrypt::ePdfKeyLength_128, "rc4v2.pdf"); testEnctyption2(PdfEncrypt::ePdfEncryptAlgorithm_AESV2, PdfEncrypt::ePdfKeyLength_128, "aesv2.pdf"); testEnctyption2(PdfEncrypt::ePdfEncryptAlgorithm_AESV3, PdfEncrypt::ePdfKeyLength_256, "aesv3.pdf"); } catch (const PdfError &error) { fprintf (stderr, "Failed with error:\n"); error.PrintErrorMsg(); } return 0; }
aesv3.pdf
Description: Adobe PDF document
_______________________________________________ Podofo-users mailing list Podofo-users@lists.sourceforge.net https://lists.sourceforge.net/lists/listinfo/podofo-users