一個(gè)簡(jiǎn)單的圖片加解密函數(shù),使用client跑,不要使用瀏覽器跑
話不多說(shuō),直接上代碼
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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
|
<?php /** * created by hello. * user: qq 845875470 * date: 2016/4/2 * time: 11:21 */ $notice = <<<a 為了穩(wěn)定性,必須在客戶端跑 格式 :php path=d:/xxx/uuu type=en is_copy=1 salt=xxx 參數(shù)使用空格分開(kāi) path -- 路徑 必須寫(xiě) type -- en加密, de為解密 必須寫(xiě) is_copy -- 1為復(fù)制,0為轉(zhuǎn)移, 不寫(xiě)默認(rèn)為轉(zhuǎn)移 salt -- 加密鑰匙 加密用什么,解密就用什么 不寫(xiě)默認(rèn)為salt a; //如果不是客戶端 if (php_sapi != 'cli' ) { echo $notice ; die ;} //獲取參數(shù) $arr = parse_parameter( $argv ); //如果路徑?jīng)]設(shè)置 if (!isset( $arr [ 'path' ]) || !isset( $arr [ 'type' ])) { echo $notice ; die ;} //如果is_dir沒(méi)設(shè)置 if (!isset( $arr [ 'is_copy' ])) { $arr [ 'is_copy' ] = '' ;} //如果salt沒(méi)設(shè)置 if (!isset( $arr [ 'salt' ])) { $arr [ 'salt' ] = '' ;} //type為en就加密 if ( $arr [ 'type' ] == "en" ) img_enconde( $arr [ 'path' ], $arr [ 'is_copy' ], $arr [ 'salt' ]); //type為de就解密 if ( $arr [ 'type' ] == "de" ) img_deconde( $arr [ 'path' ], $arr [ 'is_copy' ], $arr [ 'salt' ]); function parse_parameter( $argv ) { $arr = array (); //獲取參數(shù) for ( $len = count ( $argv )-1; $len --; ) { list( $key , $val ) = explode ( '=' , $argv [ $len ]); $arr [ $key ] = $val ; } return $arr ; } //圖片加密函數(shù) //路徑文件夾 //是否為復(fù)制(默認(rèn)不復(fù)制) //鹽(默認(rèn)為salt) function img_enconde( $path , $is_copy = 0, $salt = 'salt' ) { $time1 = microtime(1); $handle = opendir( $path ); if (! $salt ) $salt = 'salt' ; if ( $handle ) { echo "路徑:" . $path . "\r\n\r\n" ; //在指定文件夾下創(chuàng)建臨時(shí)文件夾 $temp_dir = $path . '\\' . 'temp' ; @ mkdir ( $temp_dir , 0777, 1); while ( $file = readdir( $handle )) { $time2 = microtime(1); //構(gòu)造當(dāng)前文件絕對(duì)地址 $dir_path = $path . '\\' . $file ; //獲取文件后綴 $suffix = strrchr ( $file , '.' ); //圖片后綴 $fix = array ( '.jpg' , '.gif' , '.bmp' , '.png' , '.jpeg' , '.jpg' , '.gif' , '.bmp' , '.png' , 'jpeg' ); if ( is_file ( $dir_path ) && in_array( $suffix , $fix )) { //打開(kāi)當(dāng)前文件 $fh = fopen ( $dir_path , 'r' ); //打開(kāi)文件為流 $stream = fread ( $fh , filesize ( $dir_path )); //輸出 file_put_contents ( $temp_dir . '\\' . uniqid( '' ,1), $file . '!' . $salt . '@' . $stream ); //關(guān)閉句柄 fclose( $fh ); //是否為復(fù)制 //1為復(fù)制,0為刪除(默認(rèn)) if (! $is_copy ) { echo "加密并刪除 : " . $dir_path . "\r\n" ; @unlink( $dir_path ); } else { echo "加密 : " . $dir_path . "\r\n" ; } $time3 = microtime(1); echo "此圖用時(shí) " , ( $time3 - $time2 ), " s\r\n" , "已經(jīng)用時(shí) " , ( $time3 - $time1 ), " s\r\n\r\n" ; } } echo "加密完成\r\n" ; } else { echo "path invalid " ; return false; } } //圖片解密函數(shù) //路徑文件夾 //是否為復(fù)制(默認(rèn)不復(fù)制) //鹽(默認(rèn)為salt)加密寫(xiě)什么,這里就寫(xiě)什么 function img_deconde( $path , $is_copy = 0, $salt = '' ) { $time1 = microtime(1); $handle = opendir( $path ); if ( $handle ) { echo "路徑:" . $path . "\r\n\r\n" ; if (! $salt ) $salt = 'salt' ; //在指定文件夾下創(chuàng)建臨時(shí)文件夾 $temp_dir = $path . '\\' . 'temp' ; @ mkdir ( $temp_dir , 0777, 1); //核心正則 $reg = "#^(.+?[jpgifbmne]{3,4})!(" . $salt . ")@#im" ; $res = array (); $count = 0; while ( $file = readdir( $handle )) { $time2 = microtime(1); //構(gòu)造當(dāng)前文件絕對(duì)地址 $file_path = $path . '\\' . $file ; if ( is_file ( $file_path )) { //文件句柄 $hf = fopen ( $file_path , 'r' ); //返回流 $stream = fread ( $hf , filesize ( $file_path )); fclose( $hf ); //匹配加的密碼 if (preg_match_all( $reg , $stream , $res )) { $count ++; //清空鹽 $stream = str_replace ( $res [0][0], '' , $stream ); //輸出文件 file_put_contents ( $temp_dir . '\\' . $res [1][0], $stream ); //是否為復(fù)制 //1為復(fù)制,0為刪除(默認(rèn)) if (! $is_copy ) { echo "成功解密刪除 : " . $temp_dir . '\\' . $res [1][0] . "\r\n" ; @unlink( $file_path ); } else { echo "解密 : " . $temp_dir . '\\' . $res [1][0] . "\r\n" ; } } $time3 = microtime(1); echo "此圖用時(shí) " , ( $time3 - $time2 ), " s\r\n" , "已經(jīng)用時(shí) " , ( $time3 - $time1 ), " s\r\n\r\n" ; } } if (! $count ) { echo "沒(méi)有有效的加密文件\r\n" ; return false; } echo "解密完成\r\n" ; } else { echo "path invalid " ; return false; } } ?> |
以上就是這篇文章的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)或者工作能有一定的幫助。