PHP實現加鹽的圖片加密

來源:互聯網
上載者:User
PHP加密解密算是老話題,今天給大家分享一篇關於php實現圖片加密解密,支援加鹽的文章,有需要的朋友們可以參考借鑒。希望對大家有所協助。

一個簡單的圖片加解密函數,使用client跑,不要使用瀏覽器跑

話不多說,直接上代碼


<?php$notice = <<<A  為了穩定性,必須在用戶端跑  格式 :php path=D:/xxx/uuu type=en is_copy=1 salt=xxx  參數使用空格分開  path    -- 路徑 必須寫  type    -- en加密, de為解密 必須寫  is_copy    -- 1為複製,0為轉移,         不寫預設為轉移  salt    -- 加密鑰匙 加密用什麼,解密就用什麼  不寫預設為saltA;//如果不是用戶端if(PHP_SAPI != 'cli') {echo $notice;die;}//擷取參數$arr = parse_parameter($argv);//如果路徑沒設定if(!isset($arr['path']) || !isset($arr['type']))   {echo $notice;die;}//如果is_dir沒設定if(!isset($arr['is_copy']))             {$arr['is_copy'] = '';}//如果salt沒設定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();  //擷取參數  for($len=count($argv)-1; $len--; )  {    list($key, $val) = explode('=', $argv[$len]);    $arr[$key] = $val;  }  return $arr;}//圖片加密函數//路徑檔案夾//是否為複製(預設不複製)//鹽(預設為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";    //在指定檔案夾下建立臨時檔案夾    $temp_dir = $path . '\\' . 'temp';    @mkdir($temp_dir, 0777, 1);    while ($file = readdir($handle))    {      $time2 = microtime(1);      //構造當前檔案絕對位址      $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))      {        //開啟當前檔案        $fh = fopen($dir_path, 'r');        //開啟檔案為流        $stream = fread($fh, filesize($dir_path));        //輸出        file_put_contents($temp_dir . '\\' . uniqid('',1), $file . '!' . $salt . '@' . $stream);        //關閉控制代碼        fclose($fh);        //是否為複製        //1為複製,0為刪除(預設)        if(!$is_copy)        {          echo "加密並刪除 : " . $dir_path . "\r\n";          @unlink($dir_path);        }        else        {          echo "加密 : " . $dir_path . "\r\n";        }        $time3 = microtime(1);        echo "此圖用時 ", ($time3 - $time2), " S\r\n", "已經用時 ", ($time3 - $time1), " S\r\n\r\n";      }    }    echo "加密完成\r\n";  }  else  {    echo "path invalid ";    return false;  }}//圖片解密函數//路徑檔案夾//是否為複製(預設不複製)//鹽(預設為salt)加密寫什麼,這裡就寫什麼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';    //在指定檔案夾下建立臨時檔案夾    $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);      //構造當前檔案絕對位址      $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);          //是否為複製          //1為複製,0為刪除(預設)          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 "此圖用時 ", ($time3 - $time2), " S\r\n", "已經用時 ", ($time3 - $time1), " S\r\n\r\n";      }    }    if(!$count)    {      echo "沒有有效加密檔案\r\n";      return false;    }    echo "解密完成\r\n";  }  else  {    echo "path invalid ";    return false;  }}?>

相關推薦:

php 加密擴充庫Mcrypt安裝與樣本

PHP 加密解密內部演算法

PHP 加密與解密的鬥爭

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.