php7.2中mcrypt转openssl的方法详解

本文阅读 1 分钟
首页 PHP笔记 正文

在php7.2中mcrypt已经被废弃了。
用openssl代替它。
例如

  1. public function desEncrypt($str,$key) {
  2. $iv = $key;
  3. $size = mcrypt_get_block_size ( MCRYPT_DES, MCRYPT_MODE_CBC );
  4. $str = $this->_pkcs5_pad ( $str, $size );
  5. return strtoupper( bin2hex( mcrypt_encrypt(MCRYPT_DES, $key, $str, MCRYPT_MODE_CBC, $iv ) ) );
  6. }
  7. public function desDecrypt($str,$key) {
  8. $iv = $key;
  9. $strBin = $this->_hex2bin( strtolower( $str ) );
  10. $str = mcrypt_decrypt( MCRYPT_DES, $key, $strBin, MCRYPT_MODE_CBC, $iv );
  11. $str = $this->_pkcs5_unpad( $str );
  12. return $str;
  13. }
  14. private function _pkcs5_pad($text,$block=8){
  15. $pad = $block - (strlen($text) % $block);
  16. return $text . str_repeat(chr($pad), $pad);
  17. }
  18. private function _pkcs5_unpad($text) {
  19. $pad = ord($text{strlen($text)-1});
  20. if ($pad > strlen($text)) return $text;
  21. if (strspn($text, chr($pad), strlen($text) - $pad) != $pad) return $text;
  22. return substr($text, 0, -1 * $pad);
  23. }

替换后:

  1. //要改的加密
  2. public function desEncrypt($str,$key) {
  3. // $b = openssl_get_cipher_methods();
  4. // echo '<pre>';
  5. // print_r($b);
  6. $iv = $key;
  7. // $size = mcrypt_get_block_size ( MCRYPT_DES, MCRYPT_MODE_CBC );
  8. // var_dump($size);exit;
  9. // $str = $this->_pkcs5_pad ( $str, $size );
  10. // return strtoupper( bin2hex( mcrypt_encrypt(MCRYPT_DES, $key, $str, MCRYPT_MODE_CBC, $iv ) ) );
  11. $data = openssl_encrypt($str,"DES-CBC",$key,OPENSSL_RAW_DATA,$iv);
  12. $data = strtolower(bin2hex($data));
  13. return $data;
  14. }
  15. //要改的解密
  16. public function desDecrypt($str,$key) {
  17. $iv = $key;
  18. // $strBin = $this->_hex2bin( strtolower( $str ) );
  19. // $str = mcrypt_decrypt( MCRYPT_DES, $key, $strBin, MCRYPT_MODE_CBC, $iv );
  20. // $str = $this->_pkcs5_unpad( $str );
  21. // return $str;
  22. return openssl_decrypt (hex2bin($str), 'DES-CBC', $key, OPENSSL_RAW_DATA,$iv);
  23. }

这些网上都有,在作者使用的过程中因为使用的是MCRYPT_DES,无法得知openssl对应的是那种加密方式,所以通过手册找到了openssl_get_cipher_methods();
该方法可以找出openssl支持的所有方法,进行替换即可。

  1. echo '<pre>';
  2. $a = openssl_get_cipher_methods();
  3. print_r($a);
  4. Array
  5. (
  6. [0] => AES-128-CBC
  7. [1] => AES-128-CFB
  8. [2] => AES-128-CFB1
  9. [3] => AES-128-CFB8
  10. [4] => AES-128-CTR
  11. [5] => AES-128-ECB
  12. [6] => AES-128-OFB
  13. [7] => AES-128-XTS
  14. [8] => AES-192-CBC
  15. [9] => AES-192-CFB
  16. [10] => AES-192-CFB1
  17. [11] => AES-192-CFB8
  18. [12] => AES-192-CTR
  19. [13] => AES-192-ECB
  20. [14] => AES-192-OFB
  21. [15] => AES-256-CBC
  22. [16] => AES-256-CFB
  23. [17] => AES-256-CFB1
  24. [18] => AES-256-CFB8
  25. [19] => AES-256-CTR
  26. [20] => AES-256-ECB
  27. [21] => AES-256-OFB
  28. [22] => AES-256-XTS
  29. [23] => BF-CBC
  30. [24] => BF-CFB
  31. [25] => BF-ECB
  32. [26] => BF-OFB
  33. [27] => CAMELLIA-128-CBC
  34. [28] => CAMELLIA-128-CFB
  35. [29] => CAMELLIA-128-CFB1
  36. [30] => CAMELLIA-128-CFB8
  37. [31] => CAMELLIA-128-ECB
  38. [32] => CAMELLIA-128-OFB
  39. [33] => CAMELLIA-192-CBC
  40. [34] => CAMELLIA-192-CFB
  41. [35] => CAMELLIA-192-CFB1
  42. [36] => CAMELLIA-192-CFB8
  43. [37] => CAMELLIA-192-ECB
  44. [38] => CAMELLIA-192-OFB
  45. [39] => CAMELLIA-256-CBC
  46. [40] => CAMELLIA-256-CFB
  47. [41] => CAMELLIA-256-CFB1
  48. [42] => CAMELLIA-256-CFB8
  49. [43] => CAMELLIA-256-ECB
  50. [44] => CAMELLIA-256-OFB
  51. [45] => CAST5-CBC
  52. [46] => CAST5-CFB
  53. [47] => CAST5-ECB
  54. [48] => CAST5-OFB
  55. [49] => DES-CBC
  56. [50] => DES-CFB
  57. [51] => DES-CFB1
  58. [52] => DES-CFB8
  59. [53] => DES-ECB
  60. [54] => DES-EDE
  61. [55] => DES-EDE-CBC
  62. [56] => DES-EDE-CFB
  63. [57] => DES-EDE-OFB
  64. [58] => DES-EDE3
  65. [59] => DES-EDE3-CBC
  66. [60] => DES-EDE3-CFB
  67. [61] => DES-EDE3-CFB1
  68. [62] => DES-EDE3-CFB8
  69. [63] => DES-EDE3-OFB
  70. [64] => DES-OFB
  71. [65] => DESX-CBC
  72. [66] => IDEA-CBC
  73. [67] => IDEA-CFB
  74. [68] => IDEA-ECB
  75. [69] => IDEA-OFB
  76. [70] => RC2-40-CBC
  77. [71] => RC2-64-CBC
  78. [72] => RC2-CBC
  79. [73] => RC2-CFB
  80. [74] => RC2-ECB
  81. [75] => RC2-OFB
  82. [76] => RC4
  83. [77] => RC4-40
  84. [78] => RC4-HMAC-MD5
  85. [79] => SEED-CBC
  86. [80] => SEED-CFB
  87. [81] => SEED-ECB
  88. [82] => SEED-OFB
  89. [83] => aes-128-cbc
  90. [84] => aes-128-cfb
  91. [85] => aes-128-cfb1
  92. [86] => aes-128-cfb8
  93. [87] => aes-128-ctr
  94. [88] => aes-128-ecb
  95. [89] => aes-128-gcm
  96. [90] => aes-128-ofb
  97. [91] => aes-128-xts
  98. [92] => aes-192-cbc
  99. [93] => aes-192-cfb
  100. [94] => aes-192-cfb1
  101. [95] => aes-192-cfb8
  102. [96] => aes-192-ctr
  103. [97] => aes-192-ecb
  104. [98] => aes-192-gcm
  105. [99] => aes-192-ofb
  106. [100] => aes-256-cbc
  107. [101] => aes-256-cfb
  108. [102] => aes-256-cfb1
  109. [103] => aes-256-cfb8
  110. [104] => aes-256-ctr
  111. [105] => aes-256-ecb
  112. [106] => aes-256-gcm
  113. [107] => aes-256-ofb
  114. [108] => aes-256-xts
  115. [109] => bf-cbc
  116. [110] => bf-cfb
  117. [111] => bf-ecb
  118. [112] => bf-ofb
  119. [113] => camellia-128-cbc
  120. [114] => camellia-128-cfb
  121. [115] => camellia-128-cfb1
  122. [116] => camellia-128-cfb8
  123. [117] => camellia-128-ecb
  124. [118] => camellia-128-ofb
  125. [119] => camellia-192-cbc
  126. [120] => camellia-192-cfb
  127. [121] => camellia-192-cfb1
  128. [122] => camellia-192-cfb8
  129. [123] => camellia-192-ecb
  130. [124] => camellia-192-ofb
  131. [125] => camellia-256-cbc
  132. [126] => camellia-256-cfb
  133. [127] => camellia-256-cfb1
  134. [128] => camellia-256-cfb8
  135. [129] => camellia-256-ecb
  136. [130] => camellia-256-ofb
  137. [131] => cast5-cbc
  138. [132] => cast5-cfb
  139. [133] => cast5-ecb
  140. [134] => cast5-ofb
  141. [135] => des-cbc
  142. [136] => des-cfb
  143. [137] => des-cfb1
  144. [138] => des-cfb8
  145. [139] => des-ecb
  146. [140] => des-ede
  147. [141] => des-ede-cbc
  148. [142] => des-ede-cfb
  149. [143] => des-ede-ofb
  150. [144] => des-ede3
  151. [145] => des-ede3-cbc
  152. [146] => des-ede3-cfb
  153. [147] => des-ede3-cfb1
  154. [148] => des-ede3-cfb8
  155. [149] => des-ede3-ofb
  156. [150] => des-ofb
  157. [151] => desx-cbc
  158. [152] => id-aes128-GCM
  159. [153] => id-aes192-GCM
  160. [154] => id-aes256-GCM
  161. [155] => idea-cbc
  162. [156] => idea-cfb
  163. [157] => idea-ecb
  164. [158] => idea-ofb
  165. [159] => rc2-40-cbc
  166. [160] => rc2-64-cbc
  167. [161] => rc2-cbc
  168. [162] => rc2-cfb
  169. [163] => rc2-ecb
  170. [164] => rc2-ofb
  171. [165] => rc4
  172. [166] => rc4-40
  173. [167] => rc4-hmac-md5
  174. [168] => seed-cbc
  175. [169] => seed-cfb
  176. [170] => seed-ecb
  177. [171] => seed-ofb
  178. )
解压密码: detechn或detechn.com

免责声明

本站所有资源出自互联网收集整理,本站不参与制作,如果侵犯了您的合法权益,请联系本站我们会及时删除。

本站发布资源来源于互联网,可能存在水印或者引流等信息,请用户自行鉴别,做一个有主见和判断力的用户。

本站资源仅供研究、学习交流之用,若使用商业用途,请购买正版授权,否则产生的一切后果将由下载用户自行承担。

PHP编译参数configure配置详解
« 上一篇 01-13
PHP7 OpenSSL DES-EDE-CBC加解密
下一篇 » 01-13

发表评论