PHP supports encryption and decryption of salt images,

Source: Internet
Author: User

PHP supports encryption and decryption of salt images,

A simple image encryption/Decryption function that runs with client instead of using a browser

Let's just talk about the code.

<? Php/*** Created by hello. * User: qq 845875470 * Date: 2016/4/2 * Time: */$ notice = <A to ensure stability, you must run the following format on the client: php path = D: /xxx/uuu type = en is_copy = 1 salt = xxx parameter separated by space path -- path must write type -- en encryption, de is decrypted must write is_copy -- 1 is copy, 0 indicates transfer. if this parameter is set to "transfer salt" by default, it indicates what is used for encryption key encryption. if this parameter is set to "if", the default value is "saltA". // if it is not a client, if (PHP_SAPI! = 'Cli ') {echo $ notice; die;} // obtain the parameter $ arr = parse_parameter ($ argv); // if the path is not set if (! Isset ($ arr ['path']) |! Isset ($ arr ['type']) {echo $ notice; die;} // if is_dir is not set if (! Isset ($ arr ['is _ copy']) {$ arr ['is _ copy'] = '';} // if salt is not set if (! Isset ($ arr ['salt']) {$ arr ['salt'] = '';} // encrypt if ($ arr ['type'] = "en") img_enconde ($ arr ['path'], $ arr ['is _ copy'], $ arr ['salt']); // if the type is de, decrypt if ($ arr ['type'] = "de") img_deconde ($ arr ['path'], $ arr ['is _ copy'], $ arr ['salt']); function parse_parameter ($ argv) {$ arr = array (); // obtain the parameter for ($ len = count ($ argv)-1; $ len --;) {list ($ key, $ val) = explode ('= ', $ argv [$ len]); $ arr [$ key] = $ val;} return $ arr;} // Image encryption function // path folder // whether to copy (no copy by default) // salt (salt by default) function img_enconde ($ path, $ is_copy = 0, $ salt = 'salt') {$ time1 = microtime (1); $ handle = opendir ($ path); if (! $ Salt) $ salt = 'salt'; if ($ handle) {echo "Path :". $ path. "\ r \ n"; // create a temporary folder under the specified folder $ temp_dir = $ path. '\\'. 'temp '; @ mkdir ($ temp_dir, 0777, 1); while ($ file = readdir ($ handle) {$ time2 = microtime (1 ); // construct the absolute address of the current file $ dir_path = $ path. '\\'. $ file; // get the file suffix $ suffix = strrchr ($ file ,'. '); // image suffix $ fix = array('.jpg', '.gif ', '.bmp', '.png ', '.jpeg ','. JPG ','. GIF ','. BMP ','. PNG ', 'jpeg'); if (I S_file ($ dir_path) & in_array ($ suffix, $ fix) {// open the current file $ fh = fopen ($ dir_path, 'R '); // open the file as stream $ stream = fread ($ fh, filesize ($ dir_path); // output file_put_contents ($ temp_dir. '\\'. uniqid ('', 1), $ file. '! '. $ Salt. '@'. $ stream); // close the handle fclose ($ fh); // whether to copy/1 indicates copying, 0 indicates deleting (default) if (! $ Is_copy) {echo "encrypt and delete :". $ dir_path. "\ r \ n"; @ unlink ($ dir_path);} else {echo "encryption :". $ dir_path. "\ r \ n" ;}$ time3 = microtime (1); echo "when this image is used", ($ time3-$ time2), "S \ r \ n ", "In use", ($ time3-$ time1), "S \ r \ n" ;}} echo "encrypted \ r \ n ";} else {echo "path invalid"; return false ;}/// image decryption function // path folder // whether to copy (no copy by default) // salt (salt by default) function img_deconde ($ path, $ is_copy = 0, $ salt = '') {$ Time1 = microtime (1); $ handle = opendir ($ path); if ($ handle) {echo "path :". $ path. "\ r \ n"; if (! $ Salt) $ salt = 'salt'; // create a temporary folder under the specified folder $ temp_dir = $ path. '\\'. 'temp '; @ mkdir ($ temp_dir, 0777, 1); // core regular $ reg = "# ^ (. +? [Jpgifbmne] {3, 4 })! (". $ Salt. ") @ # im"; $ res = array (); $ count = 0; while ($ file = readdir ($ handle) {$ time2 = microtime (1 ); // construct the absolute address of the current file $ file_path = $ path. '\\'. $ file; if (is_file ($ file_path) {// file handle $ hf = fopen ($ file_path, 'R '); // return stream $ stream = fread ($ hf, filesize ($ file_path); fclose ($ hf); // match the added password if (preg_match_all ($ reg, $ stream, $ res) {$ count ++; // clear the salt $ stream = str_replace ($ res [0] [0], '', $ stream ); // output File file_put_contents ($ temp_dir. '\\'. $ res [1] [0], $ stream); // whether to copy/1 is copy, 0 is delete (default) if (! $ Is_copy) {echo "successfully decrypted and deleted :". $ temp_dir. '\\'. $ res [1] [0]. "\ r \ n"; @ unlink ($ file_path);} else {echo "decryption :". $ temp_dir. '\\'. $ res [1] [0]. "\ r \ n" ;}}$ time3 = microtime (1); echo "when this image is used", ($ time3-$ time2), "S \ r \ n ", "In use", ($ time3-$ time1), "S \ r \ n" ;}} if (! $ Count) {echo "no valid encrypted file \ r \ n"; return false;} echo "decrypted \ r \ n";} else {echo "path invalid "; return false ;}}?>

The above is all the content of this article, hoping to help you in your study or work.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.