用PHP加密,用Python(des)解密

2024-04-29 05:18:31 发布

您现在位置:Python中文网/ 问答频道 /正文

我有PHP代码:

$source = 'test message';
$crypt_key = '01234567';
$td = mcrypt_module_open('des', '', 'ecb', '');
$key = substr($crypt_key, 0, mcrypt_enc_get_key_size($td));
$iv_size = mcrypt_enc_get_iv_size($td);
# MCRYPT_RAND = 2
$iv = mcrypt_create_iv($iv_size, MCRYPT_RAND); 

if (mcrypt_generic_init($td, $key, $iv) != -1) {
  $crypt = mcrypt_generic($td, $source);
  mcrypt_generic_deinit($td);
  mcrypt_module_close($td);
}
$tokenHex = '';
$tokenLength = strlen($crypt);
for ($i = 0; $i < $tokenLength; $i++) {
  $tokenHex .= sprintf("%02x", ord($crypt{$i}));
}
echo($tokenHex);

结果:

^{pr2}$

求求你,救命!如何在Python中解密?谢谢!在

我试着这样做:

from Crypto.Cipher import Blowfish
import binascii
key = b'01234567'
c1  = Blowfish.new(key, Blowfish.MODE_ECB)
res = c1.decrypt(binascii.unhexlify('8998c67c86cd5d4d0221e1bee3f3e03e'))
print(res)

但结果是:

b'\xbbU;\x8d\x97{\xbc\x01I\x8bV\x10f%\xabh'

我也尝试:

from Crypto.Cipher import DES
des = DES.new('01234567', DES.MODE_ECB)
s1 = binascii.unhexlify('8998c67c86cd5d4d0221e1bee3f3e03e')
print( des.encrypt(s1))

但结果又错了:

b't\xceS\x0b\x97\x14S}\x14\xcc\xf8\x91e\x83e\xd9

Tags: keyimportsourcesizegenerictdmoduledes