logo
当前位置:首 页 > 编程技术 >后端开发 >PHP语言 > 查看文章

开发的票务系统的时候对接了各大的OTA[携程,美团,同程,驴妈妈等],因为我们系统用了PHP开发,但是呢,这几个OTA都是Java系统开发的,公司中也没有配备PHP专职的开发,造成的问题就是,他们的PHP的demo还停留在PHP5.6之前的时代。问题就是在升级PHP7之后,encrypt扩展直接就完全不支持了。那么问题就来了,下了demo的用PHP7的小伙伴就麻烦了。后来查了好多资料,发现了PHP的openssl扩展显现出了实力。下面就给大家讲下encrypt扩展 迁移到 openssl扩展的方式

 

demo上的代码时这样的:

 

  1. <?php
  2. class Aes{
  3. private $key = '6b4d63211b4ba869';
  4. private $iv = 'dbbf079b95004f65';
  5. public function aes128_cbc_encrypt($data) {
  6. $key = $this->key;
  7. $iv = $this->iv;
  8. if(16 !== strlen($key)) $key = hash('MD5', $key, true);
  9. if(16 !== strlen($iv)) $iv = hash('MD5', $iv, true);
  10. $padding = 16 - (strlen($data) % 16);
  11. $data .= str_repeat(chr($padding), $padding);
  12. return $this->encode_bytes(mcrypt_encrypt(MCRYPT_RIJNDAEL_128, $key, $data, MCRYPT_MODE_CBC, $iv));
  13. }
  14.  
  15. public function aes128_cbc_decrypt($data) {
  16. $key = $this->key;
  17. $iv = $this->iv;
  18. if(16 !== strlen($key)) $key = hash('MD5', $key, true);
  19. if(16 !== strlen($iv)) $iv = hash('MD5', $iv, true);
  20. $data = mcrypt_decrypt(MCRYPT_RIJNDAEL_128, $key, $this->decode_bytes($data), MCRYPT_MODE_CBC, $iv);
  21. $padding = ord($data[strlen($data) - 1]);
  22. return substr($data, 0, -$padding);
  23. }
  24.  
  25. public function decode_bytes($text){
  26. $bytes=array();
  27. for($i=0;$i<strlen($text);$i+=2){
  28. $char=$text[$i];
  29. $bytes[$i/2]=chr((ord($char)-ord('a'))<<4);
  30. $char=$text[$i+1];
  31. $bytes[$i/2]=chr(ord($bytes[$i/2])+(ord($char)-ord('a')));
  32. }
  33.  
  34. return join('',$bytes);
  35. }
  36.  
  37. public function encode_bytes($text){
  38. $bytes=array();
  39. for($i=0;$i<strlen($text);$i++){
  40. $bytes[]=chr(((ord($text[$i])>>4)&15)+ord('a'));
  41. $bytes[]=chr((ord($text[$i]) & 15)+ord('a'));
  42. }
  43.  
  44. return join('',$bytes);
  45. }
  46. }
  47.  
  48. $encrypted = 'ppmbnoahlffbpbjjhfegchdhjclbfpfaiopkmlflbohfphaagkndomcehakaopefmakbgenlomplmmpdmehffndpbmdmhgdaagddnhaifblckgendbajmjiafhcgpmjeabmbhaechabcadgpbcaiipcjmkdjidpnljhkedmghmepmmmkmcilpdaajcpdgkjknjlmfhmmdmeppdgppdamagokhjpmfggkkkcoihmaoakachkkomhjnienajiafojbbhfcohnjakcpneacofbmcdlfodhldhipipkbkghjiebejcahfcpoecbbdfmhdibodabhhklkjdgaljkhcffjkocahdkmchdhpegpgbapgcjdcigjchpjpogkgdaifaaipipccbmkeemhjecojohmcbfaacffgcfgidhhlijnminobnkjkphpdgkagpniimebjcfjlmaahllkpgplacbmlomajjilceffnfpljjaaddcpemjidpflenhjcjdebkcmbikfdjoefoallfnphflodmmbfinbfjhkbccgkppodahlnegdjmgliodhogmdelpiodogpkhahmjioojppmlikfkkkandkahdemccanfjlpikcccbhfkomdhlbndckeldlpdbnlngeclnjlleijlckpgleccbgbikjdlffcinkcknohdgejdhhnafmmagmcklekcalgehdgjnopfbclloolfpemdlaeigmibhgfpekdliebdjaekmbhclggkompigfddggmlnfgmaiehchlbjolmpnoddkihdfpfhpalbmgiilibooenngbbjblcbdleenhjcefnkdgmpbimhckgajlabljcjklfccmjncelphgggjilenhlnpjichiakijoohkppciblgnjdfpjlejglomdapccobblikhkbldffdljidbijblhfbdbglmihdhcclokagoolbpmlhagambeiphldgijnljkeibeaoofbniaifaambdidhchhomjndfmgffonbemboampjiolcndgnhhmnphofojpcnganphhfdmidmonmgbbafpfehigjmbjgdpkchjcodfiffmhladiockoaenljibmnadajdejanihgnohdagfennjkpepfmohemihojcehlephiegmmepaechepmpgejaafphnmkbnklldpliiadihdncdmobaodoghnpfopfejecglnmhhjlminmpfpbkjlhkegdioogefmoocbchnmkddcojjnhdalbcffmldfaihgpgncdjlnijeieddecdmhdoihoccfekcjgmpfhjpcfgdjbenkmmoknlobaihedjfgkonlglcbckpmgcgdeghjfefghhjfmnimhdpjejkelbofojeiloccdpmpembfpkdgignmkcopdganmmfjhhghbhghmflnldhhlaakijpggdbddiiamdnpljdobmckoaddlidiakepmbcfflineldjlefhdpdbmajmhcnnkcggombeonjheocojbhcjmfncdfjnlkibipkfjlbolcicgacdiebnchaafhjbiaalddcghpelbninjfcnbfmebkekbenliclimoknlpppgpkcbeaokfkgdafndepkbfjohdhdplmgpanamhimgkimmjpfcbcnbpjhglnpcfejkennjpieiceflbfbphgmdgkjliaaejgemfgmfiddcaecacfmdhmkdhblkijnandiddjobeelbahhhkaddffiphgnlmghjpdjbihcoekckfmkbokonoddfikaakaejaopekbhldpbmhhgdldjhbckjfjimccfchgnbcjpmiidiijkekijgcmgaoglimaoknmhgpghpjcodoblageapioildelbgibcdbfaiomcmlf';
  49.  
  50. $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":"1450610915@qq.com"}]}';
  51.  
  52. $aec = new Aes();
  53. //加密
  54. echo $aec->aes128_cbc_decrypt($encrypted);
  55. echo "<br>";
  56. //解密
  57. echo $aec->aes128_cbc_encrypt($decrypt);

 

接入到PHP7之后直接就是报错了,因为扩展无法引入,强行支持的话那稳定性就无法确定了;

重新转换了之后的代码贴下:

 

  1. /*
  2. 模式:
  3. AES-128-CBC
  4. AES-128-CFB
  5. AES-128-CFB1
  6. AES-128-CFB8
  7. AES-128-OFB
  8. AES-192-CBC
  9. AES-192-CFB
  10. AES-192-CFB1
  11. AES-192-CFB8
  12. AES-192-OFB
  13. AES-256-CBC
  14. AES-256-CFB
  15. AES-256-CFB1
  16. AES-256-CFB8
  17. AES-256-OFB
  18. BF-CBC
  19. BF-CFB
  20. BF-OFB
  21. CAST5-CBC
  22. CAST5-CFB
  23. CAST5-OFB
  24. IDEA-CBC
  25. IDEA-CFB
  26. IDEA-OFB
  27. */
  28. /**
  29. * 对明文进行解密
  30. * @param string $data 需要解密的字符串
  31. * @return string 加密后的密文
  32. */
  33.  
  34.  
  35. public function openssldecrypt($data)
  36. {
  37. $decodemode = "des-ede3";
  38. // 代码先base64编码下,这样错误的几率会小点哦
  39. $decrypted = openssl_decrypt($data,$decodemode,base64_decode("aassddffgghh"),0);
  40. return $decrypted;
  41. }
  42.  
  43. /**
  44. * 对明文进行加密
  45. * @param string $text 需要加密的明文
  46. * @return string 加密后的密文
  47. */
  48.  
  49. public function opensslencrypt($data)
  50. {
  51. $decodemode = "des-ede3";
  52. $key = "aassddffgghh"; // 密钥 可以缺省的
  53. $decrypted = openssl_encrypt($data,$decodemode, $key ,0);
  54. return $decrypted;
  55. }

 

携程的开发因为先一步升级了系统,先乐玩系统已经不使用旧的encrypt加密算法了,但是demo也取消了。只能靠大家自己摸索了,其他的OTA因为业务拓展的问题没有升级。苦了各位Java开发了。

 

 

系统我修改之后的代码集可以帮助后来的PHP小伙伴吧。

 

 

 

 

 

 

 

 

说说梦想,谈谈感悟 ,聊聊技术,有啥要说的来github留言吧 https://github.com/cjx2328

—— 陈 建鑫

陈建鑫
footer logo
未经许可请勿自行使用、转载、修改、复制、发行、出售、发表或以其它方式利用本网站之内容。站长联系:cjx2328#126.com(修改#为@)
Copyright ©ziao design Studio All Rights Reserved.沪ICP备14052271号-3