试图在python中解密旧的openssl 3des加密数据



我正试图将一些加密数据从一个古老的mysql数据库系统迁移到一个新的系统。

旧系统简单地封装了openssl来执行一些表数据的加密和解密,使用的是旧版本的openssl,该版本使用带有md5摘要和base64的3des。

我已经设法获得了一个更现代的openssl来解密数据,使用:

openssl enc -des3 -md md5 -a -A -pass pass:<PASSPHRASE> -d

然而,如果可能的话,我希望在不必对openssl进行系统调用的情况下实现迁移,从而将数据转换为更现代、更安全的加密标准。

我知道我可以用反转base64编码

import base64
b64_decoded = base64.b64decode(b64_encoded)

它给了我一个字节串,行为:

b'Salted__xcb\x92|xa60xabxb4x93xbexffxf2"Wxb7:xc0xfbtxc2xbax11x00xd0xbd$--sQzJx10xce0xc78=hXx95ox82Txe1xc3x9dx14Qm-9`x9c>zbVxa3xb5hxc5w_x93xbcx05x8f?x0exe7xdaSuX?xddwnx7fxb5xaf[x0bx02x80xb0-Nx03^x1cxa5xd6?6Qx10.xd5xe7gx7fZ!Hx14xc0xbbxf4xe6P<xed~xbcxdaAxd7xe5xc2Ixe8Kx04xfexc5xab2#>)xc80 ... '

是否有人对openssl的密钥和iv派生的内部工作原理有足够的了解,能够告诉我如何从密码短语和解码数据中派生密钥、iv等,以便能够将正确的数据传递给Oscrypto或Cryptography库3des函数?

干杯,

Tek

终于找到了正确的搜索,并在mti2935 的一篇帖子中找到了我最终想要的答案

以下代码将给出密钥和iv:

import base64
import hashlib

b64_encoded_ciphertext = "<BASE64 encoded ciphertext>"
passphrase = "<passphrase>"
# base64 decode to give the raw bytes of the ciphertext
ciphertext = base64.b64decode(b64_encoded_ciphertext)
# passphrase must be raw bytes too
passphrase = passphrase.encode('utf-8')
# the 8 bytes following "Salted__" in the ciphertext are the salt
salt = ciphertext[8:16]
# we hash a couple of times
round1 = hashlib.md5(passphrase + salt).digest()
round2 = hashlib.md5(round1 + passphrase + salt).digest()
# sum the two rounds
final_hash = round1 + round2
# the key is the first 24 bytes of that final hash
key = final_hash[0:24]
# and the iv is the remaining 8
iv = final_hash[24:]

最新更新