Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Encrypt hex literals when needed #162

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open

Conversation

leppie
Copy link

@leppie leppie commented Feb 18, 2022

Hex literal strings werent getting encrypted so it would break images with indexed colorspace.

Sample program:

using System;
using PdfSharp.Pdf;
using PdfSharp.Pdf.IO;

class Program
{
    static void Main()
    {
        var ts = (DateTime.Now - new DateTime(1970, 1, 1)).TotalMilliseconds;

        using (var targetDoc = new PdfDocument())
        {
            using (var src = PdfReader.Open("test.pdf", PdfDocumentOpenMode.Import))
            {
                for (var i = 0; i < src.PageCount; i++)
                {
                    targetDoc.AddPage(src.Pages[i]);
                }
            }

            targetDoc.Save($"out1-{ts:F0}.pdf");

            targetDoc.SecuritySettings.UserPassword = "1234";

            targetDoc.Save($"out2-{ts:F0}.pdf");
        }

        // round trip test
        using (var targetDoc = new PdfDocument())
        {
            using (var src = PdfReader.Open($"out2-{ts:F0}.pdf", "1234", PdfDocumentOpenMode.Import))
            {
                for (var i = 0; i < src.PageCount; i++)
                {
                    targetDoc.AddPage(src.Pages[i]);
                }
            }

            targetDoc.Save($"out5-{ts:F0}.pdf");
        }
    }
}

PDF used for testing test.pdf

Output would become this without this PR: out2-1645190757122.pdf (password 1234)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants