Php implements image encryption and decryption, and supports adding salt,
A simple image encryption/Decryption Function
Run with client, do not run with a browser
Qq845875470, Technical Exchange
1 <? Php 2/** 3 * Created by hello. 4 * User: qq 845875470 5 * Date: 2016/4/2 6 * Time: 7 */8 9 $ notice = <A 10 to ensure stability, you must run the 11 format on the client: php path = D:/xxx/uuu type = en is_copy = 1 salt = xxx 12 parameter separated by space 13 path -- path must write 14 type -- en encryption, if de is decrypted, 15 is_copy must be written. If 0 is transferred, 16 salt is transferred by default. If no value is set, the default value is salt. If no value is set, the default value is salt 17; 18 19 // if not the client 20 if (PHP_SAPI! = 'Cli ') {echo $ notice; die;} 21 22 // obtain the parameter 23 $ arr = parse_parameter ($ argv ); 24 25 // if the path is not set to 26 if (! Isset ($ arr ['path']) |! Isset ($ arr ['type']) {echo $ notice; die;} 27 // if is_dir is not set to 28 if (! Isset ($ arr ['is _ copy']) {$ arr ['is _ copy'] = '';} 29 // if salt is not set to 30 if (! Isset ($ arr ['salt']) {$ arr ['salt'] = '';} 31 32 // if the type is en, the encryption is 33 if ($ arr ['type'] = "en") img_enconde ($ arr ['path'], $ arr ['is _ copy'], $ arr ['salt']); 34 // decrypt 35 if the type is de ($ arr ['type'] = "de") img_deconde ($ arr ['path'], $ arr ['is _ copy'], $ arr ['salt']); 36 37 38 function parse_parameter ($ argv) 39 {40 $ arr = array (); 41 // obtain the parameter 42 for ($ len = count ($ argv)-1; $ len --;) 43 {44 list ($ key, $ val) = explode ('= ', $ Argv [$ len]); 45 $ arr [$ key] = $ val; 46} 47 return $ arr; 48} 49 50 51 // image encryption function 52 // path folder 53 // whether to copy (no copy by default) 54 // salt (salt by default) 55 function img_enconde ($ path, $ is_copy = 0, $ salt = 'salt') 56 {57 $ time1 = microtime (1); 58 $ handle = opendir ($ path ); 59 if (! $ Salt) $ salt = 'salt'; 60 if ($ handle) 61 {62 echo "Path :". $ path. "\ r \ n"; 63 // create a temporary folder under the specified folder 64 $ temp_dir = $ path. '\\'. 'temp '; 65 @ mkdir ($ temp_dir, 0777, 1); 66 67 while ($ file = readdir ($ handle) 68 {69 $ time2 = microtime (1 ); 70 // construct the absolute address of the current File 71 $ dir_path = $ path. '\\'. $ file; 72 // get the file suffix 73 $ suffix = strrchr ($ file ,'. '); 74 // image suffix 75 $ fix = array('.jpg', '.gif ', '.bmp', '.png ', '.jpeg ','. JPG ','. GIF ','. BMP ','. PNG ', 'jpeg'); 76 77 if (is_file ($ dir_path) & in_array ($ suffix, $ fix )) 78 {79 // open the current file 80 $ fh = fopen ($ dir_path, 'R'); 81 82 // open the file as a stream 83 $ stream = fread ($ fh, filesize ($ dir_path); 84 // output 85 file_put_contents ($ temp_dir. '\\'. uniqid ('', 1), $ file. '! '. $ Salt. '@'. $ stream); 86 // close handle 87 fclose ($ fh); 88 89 // whether to copy 90 // 1 indicates copy, 0 indicates Delete (default) 91 if (! $ Is_copy) 92 {93 echo "encrypted and deleted :". $ dir_path. "\ r \ n"; 94 @ unlink ($ dir_path); 95} 96 else 97 {98 echo "encrypted :". $ dir_path. "\ r \ n"; 99} 100 $ time3 = microtime (1); 101 echo "when this image is used", ($ time3-$ time2 ), "S \ r \ n", "used", ($ time3-$ time1), "S \ r \ n "; 102} 103} 104 105 echo "encrypted \ r \ n"; 106} 107 else108 {109 echo "path invalid"; 110 return false; 111} 112} 113 114 // image decryption function 115 // path folder 116 // whether to copy (no copy by default)) 117 // salt (salt by default) What is encrypted and what is written here? 118 function img_deconde ($ path, $ is_copy = 0, $ salt = '') 119 {120 $ time1 = microtime (1); 121 $ handle = opendir ($ path); 122 if ($ handle) 123 {124 echo "path :". $ path. "\ r \ n"; 125 if (! $ Salt) $ salt = 'salt'; 126 127 // create a temporary folder in the specified folder. 128 $ temp_dir = $ path. '\\'. 'temp '; 129 @ mkdir ($ temp_dir, 0777, 1); 130 131 // core regular expression 132 $ reg = "# ^ (. +? [Jpgifbmne] {3, 4 })! (". $ Salt. ") @ # im"; 133 $ res = array (); 134 135 $ count = 0; 136 while ($ file = readdir ($ handle )) 137 {138 $ time2 = microtime (1); 139 // construct the absolute address of the current file 140 $ file_path = $ path. '\\'. $ file; 141 142 if (is_file ($ file_path) 143 {144 // file handle 145 $ hf = fopen ($ file_path, 'R '); 146 // return stream 147 $ stream = fread ($ hf, filesize ($ file_path); 148 fclose ($ hf ); 149 150 // password 151 if (preg_match_all ($ reg, $ stream, $ res) 152 {153 $ c Ount ++; 154 // clear salt 155 $ stream = str_replace ($ res [0] [0], '', $ stream ); 156 // output file 157 file_put_contents ($ temp_dir. '\\'. $ res [1] [0], $ stream); 158 159 // whether to copy 160 // 1 indicates copying, 0 indicates deleting (default) 161 if (! $ Is_copy) 162 {163 echo "successfully decrypted and deleted :". $ temp_dir. '\\'. $ res [1] [0]. "\ r \ n"; 164 @ unlink ($ file_path); 165} 166 else167 {168 echo "decryption :". $ temp_dir. '\\'. $ res [1] [0]. "\ r \ n"; 169} 170} 171 $ time3 = microtime (1); 172 echo "in this image", ($ time3-$ time2 ), "S \ r \ n", "used", ($ time3-$ time1), "S \ r \ n "; 173} 174} 175 if (! $ Count) 176 {177 echo "no valid encrypted file \ r \ n"; 178 return false; 179} 180 echo "decrypted \ r \ n "; 181} 182 else183 {184 echo "path invalid"; 185 return false; 186} 187} 188 189?>