本文基于AES-CBC加密解密方法
一、php加密=>python解密
php加密:
$key = 'ABCDEFGHIJKLMNOP';
$vi = '0102030405060708';
$str = '需加密的字符串';
$sign = openssl_encrypt(str, 'AES-128-CBC', $key, OPENSSL_RAW_DATA, $vi);
$sign = base64_encode($sign);
var_dump($sign);die();
python解密:
import base64
import re
from Crypto.Cipher import AES
key = 'ABCDEFGHIJKLMNOP'
def AES_Decrypt(data):
vi = '0102030405060708'
data = data.encode('utf8')
encodebytes = base64.decodebytes(data)
cipher = AES.new(key.encode('utf8'), AES.MODE_CBC, vi.encode('utf8'))
text_decrypted = cipher.decrypt(encodebytes)
zhmodel = re.compile(u'[\u4e00-\u9fff]')
match = zhmodel.search(text_decrypted.decode())
if match == False:
unpad = lambda s: s[0:-s[-1]]
text_decrypted = unpad(text_decrypted)
text_decrypted = text_decrypted.decode('utf8').rstrip()
return text_decrypted
print(AES_Decrypt("F8dIExttsNN1WK5BCLHhkRZVwVIoI70W1G+pjPhgNRk="))
二、python加密=>php解密
python加密:
import base64
import re
from Crypto.Cipher import AES
key = 'ABCDEFGHIJKLMNOP'
def AES_Encrypt(data):
vi = '0102030405060708'
cryptor = AES.new(key.encode('utf8'), AES.MODE_CBC, vi.encode('utf8'))
zhmodel = re.compile(u'[\u4e00-\u9fff]')
match = zhmodel.search(data)
if match == None:
add = 16 - len(data) % 16
pad = lambda s: s + add * chr(add)
data = pad(data)
enctext = cryptor.encrypt(data.encode('utf8'))
else:
data = data.encode()
add = 16 - len(data) % 16
data = data + add * (chr(add)).encode()
enctext = cryptor.encrypt(data)
encodestrs = base64.b64encode(enctext).decode('utf8')
return encodestrs
print(AES_Encrypt('abcd123'))
php解密:
key = 'ABCDEFGHIJKLMNOP';
$vi = '0102030405060708';
$data = 'KeAJB/1sMcGk6HUmKeC5jA==';
$sign = openssl_decrypt(base64_decode($data), 'AES-128-CBC', $key, 1, $vi);
var_dump($sign);die();
|