本文實例講述了PHP實現的AES加密、解密封裝類與用法。分享給大家供大家參考,具體如下:
001 | <?php |
002 | /** |
003 | * Class AES |
004 | * 用于AES加解密數據 |
005 | * time:2018-04-27 |
006 | */ |
007 | class AES |
008 | { |
009 | protected $cipher = MCRYPT_RIJNDAEL_256; //AES加密算法 |
010 | protected $mode = MCRYPT_MODE_CBC; //采用cbc加密模式 |
011 | protected $key ; //密鑰 |
012 | protected $iv ; //cbc模式加密向量,如為空將采用密鑰代替 |
013 | /** |
014 | * AES constructor. |
015 | * |
016 | * @param $key 密鑰 |
017 | * @param null $iv 向量 可選 如為空將采用密鑰代替 |
018 | * |
019 | * @throws Exception |
020 | */ |
021 | public function __construct( $key , $iv = NULL) |
022 | { |
023 | if (! extension_loaded ( "mcrypt" )) { |
024 | // throw new \Exception("mcrypt extension do not exist. it was DEPRECATED in PHP 7.1.0, and REMOVED in PHP 7.2.0. use OpenSSL instead"); |
025 | } |
026 | $this ->key = $key ; |
027 | $this ->iv = $iv ; |
028 | } |
029 | /** |
030 | * 加密數據 |
031 | * @param $data |
032 | * |
033 | * @return string |
034 | */ |
035 | public function encrypt( $data ) |
036 | { |
037 | $td = mcrypt_module_open( $this ->cipher, '' , $this ->mode, '' ); |
038 | $key = hash( "sha256" , $this ->key, true); |
039 | $iv = isset( $this ->iv) ? hash( "sha256" , $this ->iv, true) : $key ; |
040 | $data = $this ->padding( $data ); |
041 | mcrypt_generic_init( $td , $key , $iv ); |
042 | $encryptedData = base64_encode (mcrypt_generic( $td , $data )); |
043 | mcrypt_generic_deinit( $td ); |
044 | mcrypt_module_close( $td ); |
045 | return $encryptedData ; |
046 | } |
047 | /** |
048 | * 解密數據 |
049 | * @param $data |
050 | * |
051 | * @return bool|string |
052 | */ |
053 | public function decrypt( $data ) |
054 | { |
055 | $td = mcrypt_module_open( $this ->cipher, '' , $this ->mode, '' ); |
056 | $key = hash( "sha256" , $this ->key, true); |
057 | $iv = isset( $this ->iv) ? hash( "sha256" , $this ->iv, true) : $key ; |
058 | mcrypt_generic_init( $td , $key , $iv ); |
059 | $decrypted_data = mdecrypt_generic( $td , base64_decode ( $data )); |
060 | mcrypt_generic_deinit( $td ); |
061 | mcrypt_module_close( $td ); |
062 | return $this ->unPadding( $decrypted_data ); |
063 | } |
064 | /** |
065 | * 填充數據到分組大小的整數倍 |
066 | * @param null $data |
067 | * |
068 | * @return string |
069 | */ |
070 | protected function padding( $data = null) |
071 | { |
072 | $blockSize = 32; //MCRYPT_RIJNDAEL_256算法的分組大小是32字節 |
073 | $pad = $blockSize - ( strlen ( $data ) % $blockSize ); |
074 | return $data . str_repeat ( chr ( $pad ), $pad ); |
075 | } |
076 | /** |
077 | * 去掉填充的數據 |
078 | * @param null $data |
079 | * |
080 | * @return bool|string |
081 | */ |
082 | protected function unPadding( $data = null) |
083 | { |
084 | $pad = ord( $data [ strlen ( $data ) - 1]); |
085 | if ( $pad > strlen ( $data )) { |
086 | return false; |
087 | } |
088 | if ( strspn ( $data , chr ( $pad ), strlen ( $data ) - $pad ) != $pad ) { |
089 | return false; |
090 | } |
091 | return substr ( $data , 0, -1 * $pad ); |
092 | } |
093 | /** |
094 | * @return mixed |
095 | */ |
096 | public function getSecretKey() |
097 | { |
098 | return $this ->key; |
099 | } |
100 | /** |
101 | * @param mixed $key |
102 | */ |
103 | public function setSecretKey( $key ) |
104 | { |
105 | $this ->key = $key ; |
106 | } |
107 | /** |
108 | * @return null |
109 | */ |
110 | public function getIv() |
111 | { |
112 | return $this ->iv; |
113 | } |
114 | /** |
115 | * @param null $iv |
116 | */ |
117 | public function setIv( $iv ) |
118 | { |
119 | $this ->iv = $iv ; |
120 | } |
121 | } |
122 | //使用方法: |
123 | $keyStr = 'sq8f77fwhksk' ; |
124 | $aes = new AES( $keyStr ); |
125 | $str = 'www.zmynmublwnt.cn' ; |
126 | $chgstr = $aes ->encrypt( $str ); |
127 | echo $chgstr ; |
128 | echo "<br/>" ; |
129 | $rstr = $aes ->decrypt( $chgstr ); |
130 | echo $rstr ; |
131 | ?> |
運行結果:
pDyiRRNaxlss2b6SgoiVPdkD2m1QWhX393lh2iFgGdY=
www.zmynmublwnt.cn
希望本文所述對大家PHP程序設計有所幫助。