0

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:])
BuckTurgidson
  • 289
  • 3
  • 8
  • 17

2 Answers2

1

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

Gianluca
  • 3,227
  • 2
  • 34
  • 35
  • Why random chars and not just blank spaces? What am I missing? – BuckTurgidson Apr 17 '15 at 14:35
  • My bad. You can use also blank spaces. I normally use random chars just to have a unpredictable pattern, but this implies that you know the length of the original message so when you decrypt it you can delete the added chars (which is the case in my code;) ), but blank spaces are good. – Gianluca Apr 17 '15 at 14:40
  • @Gianuluca. Accepted you answer by principle but actually not using the code above. Cant see how `toAdd = len(msg) - (len(msg)/8)*8` would give the right number of chars to add. – BuckTurgidson Apr 20 '15 at 12:19
  • You can rewrite as `toAdd = len(msg)%8` which results (as remainder of the operation) in the number of chars you need to add to have a length of the message that is a multiple of 8. – Gianluca Apr 20 '15 at 12:30
  • I'm surely missing something basic: 37%8=5 while the number we're looking for is 3 no? Bcs 37+3=40 which is the closest multiple of 8... – BuckTurgidson Apr 20 '15 at 13:04
0

Bloomberg supply a command line decryption tool, having implemented the decryption in Java myself I would say it was not worth the effort and we should have just continued to call out of process for decryption.

You can find a sample Java implementation here although I warn you there is a bug when the decryption message is exactly the size of the buffer you are loading it into.

Jon Freedman
  • 9,469
  • 4
  • 39
  • 58
  • Okay, that's sad news. I was expecting it to be simple and clean. Tks for the feedback. – BuckTurgidson Apr 20 '15 at 07:35
  • I have redeveloped this command line using C# and Java. If you still need send a message here. – Dmitry Kazakov Nov 28 '16 at 13:31
  • @DmitryKazakov can you please share the code in java. I am trying to decrypt it through java code written in JAVA transformation in Informatica – NKapoor May 12 '22 at 14:58
  • @NKapoor Take a look at this post: https://stackoverflow.com/questions/34923553/des-decrypting-in-java-for-file-from-des-exe/40869756#comment122741638_40869756 – Dmitry Kazakov Oct 21 '22 at 13:32