PHP實現(xiàn)支持加鹽的圖片加密解密
更新時間:2016年09月09日 11:15:24 作者:shut_up
PHP加密解密算是老話題,今天給大家分先一篇關(guān)于php實現(xiàn)圖片加密解密,支持加鹽的文章,有需要的朋友們可以參考借鑒。
一個簡單的圖片加解密函數(shù),使用client跑,不要使用瀏覽器跑
話不多說,直接上代碼



<?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ù)使用空格分開
path -- 路徑 必須寫
type -- en加密, de為解密 必須寫
is_copy -- 1為復(fù)制,0為轉(zhuǎn)移, 不寫默認為轉(zhuǎn)移
salt -- 加密鑰匙 加密用什么,解密就用什么 不寫默認為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沒設(shè)置
if(!isset($arr['is_copy'])) {$arr['is_copy'] = '';}
//如果salt沒設(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ù)制(默認不復(fù)制)
//鹽(默認為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)建臨時文件夾
$temp_dir = $path . '\\' . 'temp';
@mkdir($temp_dir, 0777, 1);
while ($file = readdir($handle))
{
$time2 = microtime(1);
//構(gòu)造當(dāng)前文件絕對地址
$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))
{
//打開當(dāng)前文件
$fh = fopen($dir_path, 'r');
//打開文件為流
$stream = fread($fh, filesize($dir_path));
//輸出
file_put_contents($temp_dir . '\\' . uniqid('',1), $file . '!' . $salt . '@' . $stream);
//關(guān)閉句柄
fclose($fh);
//是否為復(fù)制
//1為復(fù)制,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", "已經(jīng)用時 ", ($time3 - $time1), " S\r\n\r\n";
}
}
echo "加密完成\r\n";
}
else
{
echo "path invalid ";
return false;
}
}
//圖片解密函數(shù)
//路徑文件夾
//是否為復(fù)制(默認不復(fù)制)
//鹽(默認為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';
//在指定文件夾下創(chuàng)建臨時文件夾
$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)前文件絕對地址
$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為刪除(默認)
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", "已經(jīng)用時 ", ($time3 - $time1), " S\r\n\r\n";
}
}
if(!$count)
{
echo "沒有有效的加密文件\r\n";
return false;
}
echo "解密完成\r\n";
}
else
{
echo "path invalid ";
return false;
}
}
?>
以上就是這篇文章的全部內(nèi)容,希望對大家的學(xué)習(xí)或者工作能有一定的幫助。
相關(guān)文章
php rmdir使用遞歸函數(shù)刪除非空目錄實例詳解
我們大家都知道,php rmdir()函數(shù)用于刪除空目錄,但如果要刪除非空目錄,我們必須將非空目錄中的文件或子目錄刪除,本文章向大家介紹php如何使用遞歸函數(shù)刪除非空目錄,需要的朋友可以參考一下2016-10-10
淺析php與數(shù)據(jù)庫代碼開發(fā)規(guī)范
以下是對php與數(shù)據(jù)庫代碼開發(fā)規(guī)范進行了簡單的分析介紹。需要的朋友可以過來參考下2013-08-08
PHP實現(xiàn)自動登入google play下載app report的方法
這篇文章主要介紹了PHP實現(xiàn)自動登入google play下載app report的方法,較為詳細的講述了登陸下載APP及對應(yīng)的實現(xiàn)代碼,具有不錯的實用價值,需要的朋友可以參考下2014-09-09
PHP寫的求多項式導(dǎo)數(shù)的函數(shù)代碼
PHP寫的求多項式導(dǎo)數(shù)的函數(shù)代碼,需要的朋友可以參考下2012-07-07

