问题描述
我有一个使用RSA加密的硬件设备的工作测试,在Python中使用M2Crypto。现在我需要测试使用3DES加密的类似设备。但我不知道如何使用M2Crypto做三重DES加密。
I have a working test of a hardware device that uses RSA encryption, in Python using M2Crypto. Now I need to test a similar device that uses 3DES encryption. But I can't figure out how to use M2Crypto to do triple DES encryption.
我知道应该可以从。但不幸的是,我发现是粗略的。 (,位于似乎已经和Chandler一起走了。)
I know it should be possible from this chart. But unfortunately the documentation of M2Crypto I've found is sketchy. (The homepage at http://chandlerproject.org/ seems to be gone, along with Chandler.)
我搜索了3DES和OpenSSL API 并发现一些难以grok C代码解密,这使它看起来像我需要使用M2Crypto.EVP.Cipher。但我还没有找到任何使用它的DES的例子。我找到的最近的是。它看起来像我只需要找出正确的参数 M2Crypto.EVP.Cipher .__ init __()
。我会继续挖掘,但我认为值得发布这个问题。
I've searched for 3DES and "OpenSSL API" and found some hard to grok C code for decrypting which makes it look like I need to use M2Crypto.EVP.Cipher. But I haven't found any examples of using it for DES. The closest I've found is this blog post on using it for AES encryption. It looks like I just need to figure out the correct arguments to M2Crypto.EVP.Cipher.__init__()
. I'll keep digging, but I thought it worth posting this question.
推荐答案
以下代码适用于我:
with open(keyfile, 'rb') as f:
key = f.read()
encrypt = 1
cipher = Cipher(alg='des_ede3_ecb', key=key, op=encrypt, iv='\0'*16)
ciphertext = cipher.update(plaintext)
ciphertext += cipher.final()
注意密钥文件是一个24字节
Note the keyfile is a 24-byte (binary) file with parity set as sometimes required for DES.
注意,当使用'des_ede3_ecb'时,我的iv参数被忽略,但我不能通过 None
。)
Note also that the iv argument is (I believe) ignored when using 'des_ede3_ecb', but I couldn't pass None
.)
这篇关于如何使用M2Crypto封装在Python中进行3DES加密?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!