Search code examples
pythonpaddingdesbloomberg

Paddling DES with Crypto.Cipher / ValueError: Input strings must be a multiple of 8 in length


I'm trying to decrypt Bloomberg files which are DES encrypted.

I'm getting a 'ValueError: Input strings must be a multiple of 8 in length ' which I understand means I need to 'paddle' the data to the proper byte size. In this correct?

If so, how can I do it using Crypto.Cipher?

f = open(SourcePath+FileName, 'r')
content = f.readlines()
key = b'Eight888'
msg=content[0]
from Crypto.Cipher import DES
decCipher = DES.new(key, DES.MODE_OFB, msg[:DES.block_size])
msgback = decCipher.decrypt(msg[DES.block_size:])

Solution

  • You need to be sure that msg has a length that is a multiple of 8. It not, just add some random chars at the end of it.

    Updated after BuckTurgidson comments

    A quick test can be

    if len(msg) % 8 != 0:
        toAdd = 8 - len(msg) % 8
        # add toAdd chars to msg
    

    but this work only if msg is a string

    The logic is valid also for binary buffers