我目前在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
我需要能够获取密文并在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'
我不确定它的做法有何不同。它是模式CBC的3DES。我不完全确定ede部分的含义,但我似乎找不到任何可以模拟那个确切的openssl模式的东西。
版本信息:
Python 3.6.5
PHP 7.1.3
答案 0 :(得分:2)
您从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