php5模式
https://github.com/gunnzhao/AES-CBC-PKCS7Padding-/blob/master/AesCrypter.php
public function encrypt($orig_data) {
$encrypter = mcrypt_module_open($this->algorithm, ‘‘,
$this->mode, ‘‘);
$orig_data = $this->pkcs7padding(
$orig_data, mcrypt_enc_get_block_size($encrypter)
);
mcrypt_generic_init($encrypter, $this->key, substr($this->key, 0, 16));
$ciphertext = mcrypt_generic($encrypter, $orig_data);
mcrypt_generic_deinit($encrypter);
mcrypt_module_close($encrypter);
return base64_encode($ciphertext);
}
php7模式:
$cipher = "aes-256-cbc";
$key = hash(‘sha256‘, ‘@parkingwang.com‘ , true);
$encrypted = openssl_encrypt(json_encode($data), ‘AES-256-CBC‘, $key, 1, substr($key, 0, 16));
$encrypt_msg = base64_encode($encrypted);
注意:加密后的字节码使用Base64转换成字符串
加密模式: CBC
填充模式: PKCS7Padding
加密密钥: 用户密钥 SHA256 的32 bytes
AES IV : 加密密钥的前 16 bytes
Base 64: Base64.DEFAULT
加密过程:
加密:padding->CBC加密->base64编码
解密:base64解码->CBC解密->unpadding
AES加密结果基准测试:
用户密钥: 909ed2d5fcf907c79fb9aa341a98febb65291c39
明文: AABBCC测试数据
密文: noMrTUS2A0YTcYaaPQSy9peqF6Mv/faMkI4yYHDvKjw=