1. python版本
import base64
from Crypto.Cipher import AES
from Crypto.Util.Padding import pad, unpadclass AESUtilCBC:def __init__(self, key, iv):self.key = key.encode('utf-8')self.iv = iv.encode('utf-8')self.pad_length = AES.block_sizedef encrypt(self, data):try:data = data.encode('utf-8')cipher = AES.new(self.key, AES.MODE_CBC, self.iv)padded_data = pad(data, self.pad_length, style='pkcs7')encrypted_data = cipher.encrypt(padded_data)encoded_data = base64.b64encode(encrypted_data).decode('utf-8')return encoded_dataexcept Exception as e:raise Exception(f"Encryption failed: {str(e)}")def decrypt(self, encrypted_data):try:encrypted_bytes = base64.b64decode(encrypted_data)cipher = AES.new(self.key, AES.MODE_CBC, self.iv)decrypted_data = cipher.decrypt(encrypted_bytes)unpadded_data = unpad(decrypted_data, self.pad_length, style='pkcs7')return unpadded_data.decode('utf-8')except Exception as e:raise Exception(f"Decryption failed: {str(e)}")# 示例使用 AESUtilCBC 类进行加密和解密
# 定义密钥和初始向量(IV)
key = 'Nhic0EAJqsIdMBfs' # 16字节的密钥
iv = 'Nhic0EAJqsIdMBfs' # 16字节的初始向量# 创建 AESUtilCBC 类的实例
aes_util = AESUtilCBC(key, iv)# 要加密的数据
data = '123456'# 调用加密方法
encrypted_data = aes_util.encrypt(data)
print(f"加密后的数据: {encrypted_data}")# 调用解密方法
decrypted_data = aes_util.decrypt(encrypted_data)
print(f"解密后的数据: {decrypted_data}")
执行以后是
加密后的数据: Qun8LbhlewyVvRQDzrNCIQ==
解密后的数据: 123456
2. php代码
<?phpuse Exception;class AESUtilCBC
{private $key = 'Nhic0EAJqsIdMBfs';private $iv = 'Nhic0EAJqsIdMBfs';private $algo = 'AES-128-CBC';private $options = OPENSSL_RAW_DATA;public function __construct($key = null, $iv = null, $algo = null, $options = null){if ($key) {$this->key = $key;}if ($iv) {$this->iv = $iv;}if ($algo) {$this->algo = $algo;}if ($options) {$this->options = $options;}}/*** 加密** @param $data* @return false|string*/public function encrypt($data){try {$encryptedData = openssl_encrypt($data, $this->algo, $this->key, $this->options, $this->iv);return base64_encode($encryptedData);} catch (Exception $e) {return false;}}/*** 解密** @param $encryptedData* @return false|string*/public function decrypt($encryptedData){try {$decodedData = base64_decode($encryptedData);return openssl_decrypt($decodedData, $this->algo, $this->key, $this->options, $this->iv);} catch (Exception $e) {return false;}}/*** hex数据加密** @param $hexData* @return string*/public function encryptHexData($hexData){try {$encrypted = openssl_encrypt($hexData, $this->algo, $this->key, OPENSSL_RAW_DATA, $this->iv);return bin2hex($encrypted);} catch (Exception $e) {return false;}}/*** hex数据加密** @param $hexData* @return string*/public function decryptHexData($hexData){try {$hexData = pack("H*", $hexData);return openssl_decrypt($hexData, $this->algo, $this->key, OPENSSL_RAW_DATA, $this->iv);} catch (Exception $e) {return false;}}
}$encrypted_data = (new AESUtilCBC())->encrypt('123456');
print("加密后的数据: {$encrypted_data}<br>");$decrypted_data = (new AESUtilCBC())->decrypt($encrypted_data);
print("解密后的数据: {$decrypted_data}<br>");$encrypted_data = (new AESUtilCBC())->encryptHexData('123456');
print("hex加密后的数据: {$encrypted_data}<br>");$decrypted_data = (new AESUtilCBC())->decryptHexData($encrypted_data);
print("hex解密后的数据: {$decrypted_data}");
执行以后是
加密后的数据: Qun8LbhlewyVvRQDzrNCIQ==
解密后的数据: 123456
hex加密后的数据: 42e9fc2db8657b0c95bd1403ceb34221
hex解密后的数据: 123456
3. 其他
1. 在线测试
可以在一些在线网站来进行测试是否加密一致
例如 https://www.ssleye.com/ssltool/aes_cipher.html
2. 128 256 php和python如何修改
php: $algo可以设置为 AES-128-CBC,AES-256-CBC
python:根据 key 的长度而变化,16位为128, 32位为256
3. iv长度
iv长度必须为16位,key和iv允许一致
4. 加密不一致
如果key的长度不足16或者32位,在php或python代码中进行补全空位,如果补全长度的字符不一致,则会导致加密的不一致
5. php 修改模式
修改 AES-128-CBC 的 CBC 为 ECB 等
6. php hex解密
bin2hex 函数 加密
pack("H*", $hexData); 函数解密 (十六进制字符串 $hexData 转换成二进制数据)
在在线网站测试时,将base64 修改为 hex即可