php加密解密类
<?php
class Aes{
private $key = '6b4d63211b4ba869';
private $iv = 'dbbf079b95004f65';
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(mcrypt_encrypt(MCRYPT_RIJNDAEL_128, $key, $data, MCRYPT_MODE_CBC, $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 = mcrypt_decrypt(MCRYPT_RIJNDAEL_128, $key, $this->decode_bytes($data), MCRYPT_MODE_CBC, $iv);
$padding = ord($data[strlen($data) - 1]);
return substr($data, 0, -$padding);
} 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);
}
} $encrypted = 'ppmbnoahlffbpbjjhfegchdhjclbfpfaiopkmlflbohfphaagkndomcehakaopefmakbgenlomplmmpdmehffndpbmdmhgdaagddnhaifblckgendbajmjiafhcgpmjeabmbhaechabcadgpbcaiipcjmkdjidpnljhkedmghmepmmmkmcilpdaajcpdgkjknjlmfhmmdmeppdgppdamagokhjpmfggkkkcoihmaoakachkkomhjnienajiafojbbhfcohnjakcpneacofbmcdlfodhldhipipkbkghjiebejcahfcpoecbbdfmhdibodabhhklkjdgaljkhcffjkocahdkmchdhpegpgbapgcjdcigjchpjpogkgdaifaaipipccbmkeemhjecojohmcbfaacffgcfgidhhlijnminobnkjkphpdgkagpniimebjcfjlmaahllkpgplacbmlomajjilceffnfpljjaaddcpemjidpflenhjcjdebkcmbikfdjoefoallfnphflodmmbfinbfjhkbccgkppodahlnegdjmgliodhogmdelpiodogpkhahmjioojppmlikfkkkandkahdemccanfjlpikcccbhfkomdhlbndckeldlpdbnlngeclnjlleijlckpgleccbgbikjdlffcinkcknohdgejdhhnafmmagmcklekcalgehdgjnopfbclloolfpemdlaeigmibhgfpekdliebdjaekmbhclggkompigfddggmlnfgmaiehchlbjolmpnoddkihdfpfhpalbmgiilibooenngbbjblcbdleenhjcefnkdgmpbimhckgajlabljcjklfccmjncelphgggjilenhlnpjichiakijoohkppciblgnjdfpjlejglomdapccobblikhkbldffdljidbijblhfbdbglmihdhcclokagoolbpmlhagambeiphldgijnljkeibeaoofbniaifaambdidhchhomjndfmgffonbemboampjiolcndgnhhmnphofojpcnganphhfdmidmonmgbbafpfehigjmbjgdpkchjcodfiffmhladiockoaenljibmnadajdejanihgnohdagfennjkpepfmohemihojcehlephiegmmepaechepmpgejaafphnmkbnklldpliiadihdncdmobaodoghnpfopfejecglnmhhjlminmpfpbkjlhkegdioogefmoocbchnmkddcojjnhdalbcffmldfaihgpgncdjlnijeieddecdmhdoihoccfekcjgmpfhjpcfgdjbenkmmoknlobaihedjfgkonlglcbckpmgcgdeghjfefghhjfmnimhdpjejkelbofojeiloccdpmpembfpkdgignmkcopdganmmfjhhghbhghmflnldhhlaakijpggdbddiiamdnpljdobmckoaddlidiakepmbcfflineldjlefhdpdbmajmhcnnkcggombeonjheocojbhcjmfncdfjnlkibipkfjlbolcicgacdiebnchaafhjbiaalddcghpelbninjfcnbfmebkekbenliclimoknlpppgpkcbeaokfkgdafndepkbfjohdhdplmgpanamhimgkimmjpfcbcnbpjhglnpcfejkennjpieiceflbfbphgmdgkjliaaejgemfgmfiddcaecacfmdhmkdhblkijnandiddjobeelbahhhkaddffiphgnlmghjpdjbihcoekckfmkbokonoddfikaakaejaopekbhldpbmhhgdldjhbckjfjimccfchgnbcjpmiidiijkekijgcmgaoglimaoknmhgpghpjcodoblageapioildelbgibcdbfaiomcmlf'; $decrypt = '{"otaOrderId":"ctriptest-750476","sequenceId":"2019032816551049c5e34f57fc4a8ca9e4af8caf5b0ccb","confirmType":2,"items":[{"passengers":[{"cardIssuePlace":"","lastName":"","cardIssueCountry":"","shoeSize":0,"gender":"M","cardIssueDate":"","intlCode":"","nationalityName":"","mobile":"17600768354","cardType":"1","weight":0,"myopiaDegreeL":0,"birthDate":"1993-07-24","cardNo":"1301333199307243318","ageType":"ADU","myopiaDegreeR":0,"firstName":"","birthPlace":"","nationalityCode":"","name":"测试","cardValidDate":"","height":0}],"cost":0.00,"quantity":1,"remark":"这是备注","useEndDate":"2019-03-28","useStartDate":"2019-03-28","itemId":"ctriptest-750476-0","priceCurrency":"CNY","adjunctions":[],"price":49.00,"PLU":"472","deposit":{},"distributionChannel":"XCW","costCurrency":"HKD","expressDelivery":{"type":1}}],"contacts":[{"intlCode":"","name":"测试","mobile":"17600768354","optionalMobile":"","optionalIntlCode":"","email":"[email protected]"}]}'; $aec = new Aes();
//加密
echo $aec->aes128_cbc_decrypt($encrypted);
echo "<br>";
//解密
echo $aec->aes128_cbc_encrypt($decrypt);