PHP implementation of salt-added image encryption

Source: Internet
Author: User
PHP encryption and decryption is an old topic, today to share an article about PHP implementation of encryption and decryption of the image, support for salt, the need for friends can reference. We hope to help you.

A simple picture plus decryption function, use the client run, do not use the browser to run

Words don't say much, directly on the code


<?php$notice = <<<a for stability, you must run the format on the client: PHP path=d:/xxx/uuu type=en is_copy=1 salt=xxx parameters use spaces to separate path--road The path must be written in type--en encryption, de for decryption must write is_copy--1 for copy, 0 for transfer, not write default to transfer salt--encrypt key encryption with what, decrypt with what do not write default to salta;//if Not the client if (php_sapi! = ' cli ') {echo $notice;d ie;} Get parameter $arr = Parse_parameter ($ARGV);//If path is not set if (!isset ($arr [' path ')] | |!isset ($arr [' type ']) {echo $notice;d ie;} If Is_dir does not set if (!isset ($arr [' is_copy ')) {$arr [' is_copy '] = ';} If the salt is not set if (!isset ($arr [' salt ')]) {$arr [' salt '] = ';} Type en is encrypted if ($arr [' type '] = = "en") img_enconde ($arr [' Path '], $arr [' is_copy '], $arr [' salt ']),//type the de to decrypt if ($arr [' Type '] = = "de") Img_deconde ($arr [' Path '], $arr [' is_copy '], $arr [' salt ']), function Parse_parameter ($argv) {$arr = array (  );  Get parameters for ($len =count ($ARGV)-1; $len--;)    {List ($key, $val) = explode (' = ', $argv [$len]);  $arr [$key] = $val; } return $arr;} Picture encryption function//path folder//Whether it is a copy (not copied by default)//Salt (default is salt) function Img_enconde ($path, $is _copy = 0, $salt = ' salt ') {$time 1 = microtime (1);  $handle = Opendir ($path);  if (! $salt) $salt = ' salt '; if ($handle) {echo "path:". $path.    "\r\n\r\n"; Create a temporary folder under the specified folder $temp _dir = $path. '\\' .    ' Temp ';    @mkdir ($temp _dir, 0777, 1);      while ($file = Readdir ($handle)) {$time 2 = microtime (1); Constructs the current file absolute address $dir _path = $path. '\\' .      $file;      Gets the file suffix $suffix = strrchr ($file, '. '); The picture suffix $fix = array ('. jpg ', '. gif ', '. bmp ', '. png ', '. jpeg ', '. JPG ', '. GIF ', '. BMP ', '.      PNG ', ' JPEG ');        if (Is_file ($dir _path) && In_array ($suffix, $fix)) {//Open the current file $FH = fopen ($dir _path, ' R ');        Open file for stream $stream = Fread ($fh, FileSize ($dir _path)); Output file_put_contents ($temp _dir. '\\' . Uniqid (', 1), $file. '!' . $salt. '@' .        $stream);        Close handle fclose ($FH); Whether to copy//1 for replication, 0 for delete (default) if (! $is _copy) {echo "Encrypt and delete:". $dir _path.          "\ r \ n"; @unlink ($dir _pATH); } else {echo "Encrypt:". $dir _path.        "\ r \ n";        } $time 3 = microtime (1);      echo "Time of this graph", ($time 3-$time 2), "s\r\n", "Elapsed Time", ($time 3-$time 1), "s\r\n\r\n";  }} echo "encryption complete \ r \ n";    } else {echo "path invalid";  return false; }}//image decryption function//path folder//Whether it is a copy (not copied by default)//salt (the default is salt) write what, here is what function Img_deconde ($path, $is _copy = 0, $salt = ") {$time 1 = m  Icrotime (1);  $handle = Opendir ($path); if ($handle) {echo "path:". $path.    "\r\n\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)) {$time 2 = microtime (1); Constructs the current file absolute address $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 plus password if (Preg_match_all ($reg, $stream, $res)) {$count + +;          emptying Salt $stream = Str_replace ($res [0][0], ', $stream); Output file File_put_contents ($temp _dir. '\\' .          $res [1][0], $stream); Whether to copy//1 for replication, 0 for delete (default) if (! $is _copy) {echo "Successfully decrypted Delete:". $temp _dir. '\\' . $res [1][0].            "\ r \ n";          @unlink ($file _path); } else {echo "decrypts:". $temp _dir. '\\' . $res [1][0].          "\ r \ n";        }} $time 3 = microtime (1);      echo "Time of this graph", ($time 3-$time 2), "s\r\n", "Elapsed Time", ($time 3-$time 1), "s\r\n\r\n";      }} if (! $count) {echo "does not have a valid encryption file \ r \ n";    return false;  } echo "decryption complete \ r \ n";    } else {echo "path invalid";  return false; }}?>

Related recommendations:

PHP Encryption Extension Library MCrypt installation and example

PHP encryption and decryption internal algorithm

The struggle for PHP encryption and decryption

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.