赞
踩
- class CtripAesService
- {
- private $key = 'xxx';
- private $iv = 'xxx';
- public function aes128_cbc_encrypt($data) {
- $key = $this->key;
- $iv = $this->iv;
- if(16 !== strlen($key)) $key = hash('MD5', $key, true);
- if(16 !== strlen($iv)) $iv = hash('MD5', $iv, true);
- $padding = 16 - (strlen($data) % 16);
- $data .= str_repeat(chr($padding), $padding);
- return $this->encode_bytes(openssl_encrypt($data,'AES-128-CBC',$key,OPENSSL_RAW_DATA,$iv));
- }
-
- public function aes128_cbc_decrypt($data) {
- $key = $this->key;
- $iv = $this->iv;
- if(16 !== strlen($key)) $key = hash('MD5', $key, true);
- if(16 !== strlen($iv)) $iv = hash('MD5', $iv, true);
- $data=openssl_decrypt( $this->decode_bytes($data), "AES-128-CBC", $key, OPENSSL_RAW_DATA, $iv);
- return $data;
- }
-
- public function decode_bytes($text){
- $bytes=array();
- for($i=0;$i<strlen($text);$i+=2){
- $char=$text[$i];
- $bytes[$i/2]=chr((ord($char)-ord('a'))<<4);
- $char=$text[$i+1];
- $bytes[$i/2]=chr(ord($bytes[$i/2])+(ord($char)-ord('a')));
- }
-
- return join('',$bytes);
- }
-
- public function encode_bytes($text){
- $bytes=array();
- for($i=0;$i<strlen($text);$i++){
- $bytes[]=chr(((ord($text[$i])>>4)&15)+ord('a'));
- $bytes[]=chr((ord($text[$i]) & 15)+ord('a'));
- }
-
- return join('',$bytes);
- }
- }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。