Skip to content

Encrypting - Image color index unchanged #2188

Open
@pilotandy

Description

@pilotandy

When encrypting a PDF with CMYK Indexed color, the colors render incorrectly on the encrypted PDF.
It appears the stream gets encrypted, but the color palette does not, as it is in the dictionary, not the stream of the object.

14 0 obj
<<
/Type/XObject
/Subtype/Image
/Width 576
/Height 229
/BitsPerComponent 8
/ColorSpace [/Indexed/DeviceCMYK 18 <0000001200000011000000100000000f0000000e0000000d0000000c0000000b0000000a00000009000000080000000700000006000000050000000400000003000000020000000100000000>]
/Filter/FlateDecode
/Length 2036
>>
stream
....
endstream
endobj

The output shows the same CMYK values as the input

32 0 obj
<<
/Type /XObject
/Subtype /Image
/Width 576
/Height 229
/BitsPerComponent 8
/ColorSpace [ /Indexed /DeviceCMYK 18 <0000001200000011000000100000000f0000000e0000000d0000000c0000000b0000000a00000009000000080000000700000006000000050000000400000003000000020000000100000000> ]
/Filter /FlateDecode
/Length 2036
>>
stream
...
endstream
endobj

In _encryption.py, starting around line 75, we encrypt the stream, but not the dictionary

elif isinstance(obj, StreamObject):
    obj2 = StreamObject()
    obj2.update(obj)
    obj2.set_data(self.stmCrypt.encrypt(b_(obj._data)))
    obj = obj2

Adding the items of the dictionary, and evaluating them as well

elif isinstance(obj, StreamObject):
    obj2 = StreamObject()
    obj2.update(obj)
    obj2.set_data(self.stmCrypt.encrypt(b_(obj._data)))
    # We're encrypting the stream, consider the dictionary too.
    for key, value in obj.items():
        obj2[key] = self.encrypt_object(value)
    obj = obj2

Now, the CMYK values are encrypted too.

32 0 obj
<<
/Type /XObject
/Subtype /Image
/Width 576
/Height 229
/BitsPerComponent 8
/ColorSpace [ /Indexed /DeviceCMYK 18 <64cda2cefbf39121492b5dfbe74cb864ec4c6101b2675d53c47ae2b5e7591ea3ba6bd2b6bcb1f13eff191f47b9495db796077831f49ac3771b0d0c23eb61418a4df5ddc4945cb16b3b217c86> ]
/Filter /FlateDecode
/Length 2036
>>
stream
....
endstream
endobj

Adobe Acrobat will then decrypt the CMYK color values correctly.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Has MCVEA minimal, complete and verifiable example helps a lot to debug / understand feature requestsworkflow-encryptionFrom a users perspective, encryption is the affected feature/workflow

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions