/*** DES加密类* User: gaowei* Date: 2017/12/12* Time: 19:23*/classDesEncrypt {private$key="";private$iv="";/*** 构造,传递二个已经进行base64_encode的KEY与IV**@paramstring $key*@paramstring $iv*/function__construct($key,$iv){if(empty($key) ||empty($iv)) {echo'key and iv is not valid';exit();}$this->key=$key;$this->iv=$iv;//8//$this->iv = $iv.'00000000000';//16}/***@title加密*@authorgaowei*@date2017/12/18*@paramstring $value 要传的参数* @ //OPENSSL_RAW_DATA|OPENSSL_ZERO_PADDING //AES-128-ECB|AES-256-CBC|BF-CBC*@returnjson* */public functionencrypt($value) {//参考地址:https://stackoverflow.com/questions/41181905/php-mcrypt-encrypt-to-openssl-encrypt-and-openssl-zero-padding-problems#$value=$this->PaddingPKCS7($value);$key= base64_decode($this->key);$iv= base64_decode($this->iv);//AES-128-ECB|不能用 AES-256-CBC|16 AES-128-CBC|16 BF-CBC|8 aes-128-gcm|需要加$tag DES-EDE3-CBC|8$cipher="DES-EDE3-CBC";if(in_array($cipher,openssl_get_cipher_methods())) {//$ivlen = openssl_cipher_iv_length($cipher);// $iv = openssl_random_pseudo_bytes($ivlen);$result= openssl_encrypt($value,$cipher,$key,OPENSSL_SSLV23_PADDING,$iv);//$result = base64_encode($result); //为3的时间要用//store $cipher, $iv, and $tag for decryption later/* $original_plaintext = openssl_decrypt($result, $cipher, $key, OPENSSL_SSLV23_PADDING, $iv);echo $original_plaintext."\n";*/}return$result;}/***@title解密*@authorgaowei*@date2017/12/18*@paramstring $value 要传的参数*@returnjson* */public functiondecrypt($value) {$key= base64_decode($this->key);$iv= base64_decode($this->iv);$decrypted= openssl_decrypt($value,'DES-EDE3-CBC',$key,OPENSSL_SSLV23_PADDING,$iv);$ret=$this->UnPaddingPKCS7($decrypted);return$ret;}private functionPaddingPKCS7($data) {//$block_size = mcrypt_get_block_size('tripledes', 'cbc');//获取长度//$block_size = openssl_cipher_iv_length('tripledes', 'cbc');//获取长度$block_size=8;$padding_char=$block_size- (strlen($data) %$block_size);$data.= str_repeat(chr($padding_char),$padding_char);return$data;}private functionUnPaddingPKCS7($text) {$pad= ord($text{strlen($text) -1});if($pad> strlen($text)) {return false;}if(strspn($text,chr($pad),strlen($text) -$pad) !=$pad) {return false;}returnsubstr($text,0,-1*$pad);}}
php7.1 aes加密,PHP7.1 OpenSSL_encrypt 替换 Mcrypt_encrypt 实现AES加密
最新推荐文章于 2022-11-23 16:05:26 发布