Atitit aes 加密法php实现
<?php
$data="aaaaccc";
$secret_key="abcdefgh";
//echo openssl_encrypt($data, $this->method, $this->secret_key, $this->options, $this->iv);
echo openssl_encrypt($data, 'AES-128-ECB', $secret_key,0, "");
echo " ";
echo openssl_decrypt("qjuIItb9e3IuYk0hi8ZN4g==", 'AES-128-ECB', $secret_key,0, "");
- 常见的填充模式有: pkcs5、pkcs7、iso10126、ansix923、zero。
- 加密模式有:DES-ECB、DES-CBC、DES-CTR、DES-OFB、DES-CFB。
常见参数$iv 加解密的向量,有些方法需要设置比如CBC
$method 加密方式
switch($this->bit) {
case 192:$this->cipher = MCRYPT_RIJNDAEL_192; break;
case 256:$this->cipher = MCRYPT_RIJNDAEL_256; break;
default: $this->cipher = MCRYPT_RIJNDAEL_128;
}
switch($this->mode) {
case 'ecb':$this->mode = MCRYPT_MODE_ECB; break;
case 'cfb':$this->mode = MCRYPT_MODE_CFB; break;
case 'ofb':$this->mode = MCRYPT_MODE_OFB; break;
case 'nofb':$this->mode = MCRYPT_MODE_NOFB; break;
case 'cbc':$this->mode = MCRYPT_MODE_CBC; break;
default: $this->mode = MCRYPT_MODE_CBC;
$options 数据格式选项(可选)【选项有:】
- 0
- OPENSSL_RAW_DATA=1
- OPENSSL_ZERO_PADDING=2
- OPENSSL_NO_PADDING=3
需要注意:如果$method为DES-ECB,则$iv无需填写
使用案例