划DES加密。密码/ ValueError:输入字符串长度必须是8的倍数。

时间:2023-01-20 18:30:26

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

我正在尝试解密被DES加密的Bloomberg文件。

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?

我得到了一个值ValueError:输入字符串必须是长度为8的倍数,我理解这意味着我需要将数据“搅拌”到合适的字节大小。在这个正确吗?

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

如果是的话,我如何使用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:])

2 个解决方案

#1


0  

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.

你需要确保msg的长度是8的倍数。它不是,只是在末尾添加一些随机的字符。

Updated after BuckTurgidson comments

更新后BuckTurgidson评论

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

但只有当msg是字符串时,它才会工作

The logic is valid also for binary buffers

这种逻辑对于二进制缓冲区也是有效的

#2


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.

布隆伯格提供了一个命令行解密工具,在我自己用Java实现了解密之后,我想说不值得这么做,我们应该继续调用进程进行解密。

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.

您可以在这里找到一个示例Java实现,但我警告您,当解密消息的大小与您正在加载的缓冲区的大小完全相同时,存在一个错误。

#1


0  

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.

你需要确保msg的长度是8的倍数。它不是,只是在末尾添加一些随机的字符。

Updated after BuckTurgidson comments

更新后BuckTurgidson评论

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

但只有当msg是字符串时,它才会工作

The logic is valid also for binary buffers

这种逻辑对于二进制缓冲区也是有效的

#2


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.

布隆伯格提供了一个命令行解密工具,在我自己用Java实现了解密之后,我想说不值得这么做,我们应该继续调用进程进行解密。

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.

您可以在这里找到一个示例Java实现,但我警告您,当解密消息的大小与您正在加载的缓冲区的大小完全相同时,存在一个错误。