encrypt (encrypt.cpp) – this example creates four documents, each encrypted with a different algorithm (RC4 v1, RC4 v2, AES v2 and AES v3). One can be opened without a password prompt, while the other have set a 'user' and 'owner' passwords. After the documents are created, they are opened with different passwords. The example also shows how to deal with encrypted files when the password is not know (how to react on ERROR_WRONG_PASSWORD error).
Note: PoDoFo allows AES encryption for memory-based documents only.

    1 /*
    2  * (c) 2013-2017 http://www.litePDF.cz
    3  *
    4  * The example code is supplied "AS IS". It disclaims all warranties, expressed
    5  * or implied, including, without limitation, the warranties of merchantability
    6  * and of fitness for any purpose. It assumes no liability for direct, indirect,
    7  * incidental, special, exemplary, or consequential damages, which may result
    8  * from the use of the code, even if advised of the possibility of such damage.
    9  *
   10  * Permission is hereby granted to use, copy, modify, and distribute this
   11  * source code, or portions hereof, for any purpose, without fee.
   12  */ 
   13 
   14 #include <windows.h>
   15 #include <stdio.h>
   16 #include <string.h>
   17 #include <string>
   18 
   19 #include "share/litePDF.h"
   20 
   21 static void drawPage(litePDF::TLitePDF &litePDF,
   22                      const char *msg)
   23 {
   24    // add a new page to it, with large-enough pixel scale
   25    HDC hDC = litePDF.AddPage(litePDF.MMToUnit(210), litePDF.MMToUnit(297), 2100, 2970, LitePDFDrawFlag_SubstituteFonts);
   26 
   27    // draw the text
   28    LOGFONTA lf = {0, };
   29    lf.lfHeight = -50; // ~5mm
   30    strcpy(lf.lfFaceName, "Helvetica");
   31 
   32    HFONT fnt;
   33    HGDIOBJ oldFnt;
   34 
   35    fnt = CreateFontIndirect(&lf);
   36    oldFnt = SelectObject(hDC, fnt);
   37 
   38    SetTextColor(hDC, RGB(128, 0, 0));
   39    TextOut(hDC, 100, 100, msg, strlen(msg));
   40 
   41    SelectObject(hDC, oldFnt);
   42    DeleteObject(fnt);
   43 
   44    // finish drawing
   45    litePDF.FinishPage(hDC);
   46 }
   47 
   48 static void createEncryptedFiles(void)
   49 {
   50    using namespace litePDF;
   51    TLitePDF litePDF;
   52 
   53    //-----------------------------------------------------------------
   54 
   55    // setup encryption to be used when creating the document
   56    litePDF.PrepareEncryption(NULL, "owner",
   57                              LitePDFEncryptPermission_All,
   58                              LitePDFEncryptAlgorithm_RC4V1);
   59 
   60    // begin write-only PDF file
   61    litePDF.CreateFileDocument("encrypt-rc4v1-no-user-pass.pdf");
   62 
   63    // fill a page
   64    drawPage(litePDF, "Encrypted, without user password, RC4 V1");
   65 
   66    // close the document
   67    litePDF.Close();
   68 
   69    //-----------------------------------------------------------------
   70 
   71    // setup encryption to be used when creating the document
   72    litePDF.PrepareEncryption("user", "owner",
   73                              LitePDFEncryptPermission_All,
   74                              LitePDFEncryptAlgorithm_RC4V2);
   75 
   76    // begin memory-based PDF document
   77    litePDF.CreateMemDocument();
   78 
   79    // fill a page
   80    drawPage(litePDF, "Encrypted, with user and owner password, RC4 V2");
   81 
   82    // save to file
   83    litePDF.SaveToFile("encrypt-rc4v2.pdf");
   84 
   85    // close the document
   86    litePDF.Close();
   87 
   88    //-----------------------------------------------------------------
   89 
   90    // setup encryption to be used when creating the document
   91    litePDF.PrepareEncryption("user", "owner",
   92                              LitePDFEncryptPermission_All,
   93                              LitePDFEncryptAlgorithm_AESV2);
   94 
   95    // begin memory-based PDF document
   96    litePDF.CreateMemDocument();
   97 
   98    // fill a page
   99    drawPage(litePDF, "Encrypted, with user and owner password, AES V2");
  100 
  101    // save to file
  102    litePDF.SaveToFile("encrypt-aesv2.pdf");
  103 
  104    // close the document
  105    litePDF.Close();
  106 
  107    //-----------------------------------------------------------------
  108 
  109    // setup encryption to be used when creating the document
  110    litePDF.PrepareEncryption("user", "owner",
  111                              LitePDFEncryptPermission_All,
  112                              LitePDFEncryptAlgorithm_AESV3);
  113 
  114    // begin memory-based PDF document
  115    litePDF.CreateMemDocument();
  116 
  117    // fill a page
  118    drawPage(litePDF, "Encrypted, with user and owner password, AES V3");
  119 
  120    // save to file
  121    litePDF.SaveToFile("encrypt-aesv3.pdf");
  122 
  123    // close the document
  124    litePDF.Close();
  125 
  126    //-----------------------------------------------------------------
  127 
  128    // setup encryption to be used when creating the document
  129    litePDF.PrepareEncryption("user", "owner",
  130                              LitePDFEncryptPermission_All,
  131                              LitePDFEncryptAlgorithm_AESV2);
  132 
  133    try {
  134       // begin file-based PDF document
  135       litePDF.CreateFileDocument("encrypt-aesv2-file.pdf");
  136 
  137       throw TLitePDFException(ERROR_CANNOT_MAKE,
  138             "Should fail with an exception, because AES encryption works only with memory-based documents currently");
  139    } catch (TLitePDFException &ex) {
  140       if (ex.getCode() != ERROR_NOT_SUPPORTED) {
  141          throw TLitePDFException(ex);
  142       }
  143    }
  144 
  145    // close the document
  146    litePDF.Close();
  147 
  148    //-----------------------------------------------------------------
  149 
  150    // setup encryption to be used when creating the document
  151    litePDF.PrepareEncryption("user", "owner",
  152                              LitePDFEncryptPermission_All,
  153                              LitePDFEncryptAlgorithm_AESV3);
  154 
  155    try {
  156       // begin file-based PDF document
  157       litePDF.CreateFileDocument("encrypt-aesv3-file.pdf");
  158 
  159       throw TLitePDFException(ERROR_CANNOT_MAKE,
  160             "Should fail with an exception, because AES encryption works only with memory-based documents currently");
  161    } catch (TLitePDFException &ex) {
  162       if (ex.getCode() != ERROR_NOT_SUPPORTED) {
  163          throw TLitePDFException(ex);
  164       }
  165    }
  166 
  167    // close the document
  168    litePDF.Close();
  169 }
  170 
  171 static void checkDocumentDecrypt(litePDF::TLitePDF &litePDF)
  172 {
  173    std::wstring value;
  174 
  175    value = litePDF.GetDocumentInfo(LitePDFDocumentInfo_Producer);
  176 
  177    if (value.find_first_of(L"litePDF") == value.npos) {
  178       fprintf (stderr, "Text 'litePDF' not found in '%S'\n", value.c_str());
  179    }
  180 
  181 }
  182 
  183 int main(void)
  184 {
  185    int res = 0;
  186 
  187    using namespace litePDF;
  188 
  189    try {
  190       TLitePDF litePDF;
  191 
  192       // create the files
  193       createEncryptedFiles();
  194 
  195       //-----------------------------------------------------------------
  196       // now try to open them
  197       //-----------------------------------------------------------------
  198 
  199       // no user password, then open it as a user
  200       litePDF.LoadFromFile("encrypt-rc4v1-no-user-pass.pdf", NULL, false);
  201 
  202       checkDocumentDecrypt(litePDF);
  203 
  204       // close the document
  205       litePDF.Close();
  206 
  207       //-----------------------------------------------------------------
  208 
  209       try {
  210          // this should fail, because no password was provided
  211          litePDF.LoadFromFile("encrypt-rc4v2.pdf", NULL, false);
  212 
  213          throw TLitePDFException(ERROR_CANNOT_MAKE,
  214                "Should fail to open encrypted file without provided password");
  215       } catch (TLitePDFException &ex) {
  216          if (ex.getCode() != ERROR_WRONG_PASSWORD) {
  217             throw TLitePDFException(ex);
  218          }
  219 
  220          // re-try with user's password
  221          litePDF.LoadFromFile("encrypt-rc4v2.pdf", "user", false);
  222 
  223          checkDocumentDecrypt(litePDF);
  224       }
  225 
  226       // close the document
  227       litePDF.Close();
  228 
  229       //-----------------------------------------------------------------
  230 
  231       // try to open as owner
  232       litePDF.LoadFromFile("encrypt-rc4v1-no-user-pass.pdf", "owner", false);
  233 
  234       checkDocumentDecrypt(litePDF);
  235 
  236       // close the document
  237       litePDF.Close();
  238 
  239       //-----------------------------------------------------------------
  240 
  241       // try to open as user
  242       litePDF.LoadFromFile("encrypt-rc4v2.pdf", "user", false);
  243 
  244       checkDocumentDecrypt(litePDF);
  245 
  246       // close the document
  247       litePDF.Close();
  248 
  249       //-----------------------------------------------------------------
  250 
  251       // try to open as owner
  252       litePDF.LoadFromFile("encrypt-rc4v2.pdf", "owner", false);
  253 
  254       checkDocumentDecrypt(litePDF);
  255 
  256       // close the document
  257       litePDF.Close();
  258 
  259       //-----------------------------------------------------------------
  260 
  261       // try to open as user
  262       litePDF.LoadFromFile("encrypt-aesv2.pdf", "user", false);
  263 
  264       checkDocumentDecrypt(litePDF);
  265 
  266       // close the document
  267       litePDF.Close();
  268 
  269       //-----------------------------------------------------------------
  270 
  271       // try to open as owner
  272       litePDF.LoadFromFile("encrypt-aesv2.pdf", "owner", false);
  273 
  274       checkDocumentDecrypt(litePDF);
  275 
  276       // close the document
  277       litePDF.Close();
  278 
  279       //-----------------------------------------------------------------
  280 
  281       // try to open as user
  282       litePDF.LoadFromFile("encrypt-aesv3.pdf", "user", false);
  283 
  284       checkDocumentDecrypt(litePDF);
  285 
  286       // close the document
  287       litePDF.Close();
  288 
  289       //-----------------------------------------------------------------
  290 
  291       // try to open as owner
  292       litePDF.LoadFromFile("encrypt-aesv3.pdf", "owner", false);
  293 
  294       checkDocumentDecrypt(litePDF);
  295 
  296       // close the document
  297       litePDF.Close();
  298 
  299       //-----------------------------------------------------------------
  300 
  301    } catch (TLitePDFException &ex) {
  302       fprintf(stderr, "litePDF Exception: %x: %s\n", ex.getCode(), ex.getMessage());
  303       res = 1;
  304    }
  305 
  306    return res;
  307 }
TOPlist