從PHP7版本開始很多依賴mcrypt擴(kuò)展的方法都不支持了,PHP7.2.0及以上版本已經(jīng)完全不支持mcrypt擴(kuò)展的任何方法了,所以PHP7及以上版本都應(yīng)該使用openssl擴(kuò)展來實(shí)現(xiàn)加解密。
以DES-CBC加密方式為例:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
|
<?php class DesUtil { /** * Des 加密 * * @param $str * @param $secretKey * @param string $iv * @return string */ public static function encrypt( $str , $secretKey , $iv = '' ) { return base64_encode (openssl_encrypt( $str , 'des-cbc' , $secretKey , OPENSSL_RAW_DATA, $iv )); } /** * Des 解密 * * @param $str * @param $secretKey * @param string $iv * @return string */ public static function decrypt( $str , $secretKey , $iv = '' ) { return openssl_decrypt( base64_decode ( $str ), 'des-cbc' , $secretKey , OPENSSL_RAW_DATA, $iv ); } } |
到此這篇關(guān)于PHP使用openssl擴(kuò)展實(shí)現(xiàn)加解密方法示例的文章就介紹到這了,更多相關(guān)PHP openssl擴(kuò)展實(shí)現(xiàn)加解密內(nèi)容請搜素服務(wù)器之家以前的文章或下面相關(guān)文章,希望大家以后多多支持服務(wù)器之家!
原文鏈接:https://segmentfault.com/a/1190000021785883