我目前在 PHP 中对 3DES (des-ede-cbc) 进行加密和解密,如下所示:
php > $key = '0000000000000000';
php > $iv = '00000000';
php > $plaintext = '1234567812345678';
php > $ciphertext = openssl_encrypt($plaintext, 'des-ede-cbc', $key, 0, $iv);
php > echo $ciphertext;
LEvEJf9CI+5VTVNeIjARNamKH+PNTx2P
php > $plaintext = openssl_decrypt($ciphertext, 'des-ede-cbc', $key, 0, $iv);
php > echo $plaintext;
1234567812345678
Run Code Online (Sandbox Code Playgroud)
我需要能够获取密文并在 python 中解密它。我发现的最接近的是 pycrypto: https: //gist.github.com/komuw/83ddf9b4ae8f995f15af
我的尝试:
>>> key = '0000000000000000'
>>> iv = '00000000'
>>> cipher_decrypt = DES3.new(key, DES3.MODE_CBC, iv)
>>> plaintext = cipher_decrypt.decrypt('LEvEJf9CI+5VTVNeIjARNamKH+PNTx2P')
>>> plaintext
b']v\xdf\xa7\xf7\xc0()\x08\xdf\xcb`4\xa7\x10\x9e\xaf\x8c\xb6\x00+_\xb3?2\x1d\\\x08\x01\xfa\xf2\x99'
Run Code Online (Sandbox Code Playgroud)
我不确定它有什么不同。它是 CBC 模式的 3DES。我不太确定 ede 部分的含义,但我似乎找不到任何东西来模拟确切的 openssl 模式。
版本信息:
Python 3.6.5
PHP 7.1.3
从 PHP 获得的字符串是 base64 编码的。
from Crypto.Cipher import DES3
from base64 import decodebytes
key = b'0000000000000000'
iv = b'00000000'
cipher_decrypt = DES3.new(key, DES3.MODE_CBC, iv)
plaintext = cipher_decrypt.decrypt(decodebytes(b'LEvEJf9CI+5VTVNeIjARNamKH+PNTx2P'))
print(plaintext.decode("utf-8"))
>>>1234567812345678
Run Code Online (Sandbox Code Playgroud)