php史上公认最安全的正反加密解密函数
之前写过一个加解密的文章,也能凑合着使用, 现在为了安全, 用php写了一个triple des(3DES)的正反加解密函数. 3DES是公认的最安全的加密解密函数了,只是可惜的是php没有提供这样现成的函数,于是乎自己写了一个3DES版本的方法,3DES有很多版本,该版本采用的是ECB模式,用PKCS7补码,base64做密文,安全等级高,一般使用该函数只要修改其中的key即可.
测试:
echo (des3crypt( “龙哥博客”,’ENCODE’));
echo “
“;
echo (des3crypt( “bxC46TETFEZFpTS1DClzpg==”,’DECODE’));
echo “
“;
/**
* 加解密函数
* @param $str
* @param $type
* @param $key
*/
function des3crypt($str,$type = ‘ENCODE’,$key = ‘AXNU7SLKJ7HKJm+x4bfBJSJQKde’){
if(empty($str) && $str != 0){
return false;
}
$td = mcrypt_module_open( MCRYPT_3DES, ”, MCRYPT_MODE_ECB, ”);
$key = base64_decode($key);
mcrypt_generic_init($td, $key,’12345678′);
if(strtoupper($type) == ‘ENCODE’){
$str = padding( $str );
$data = mcrypt_generic($td, $str);
}elseif(strtoupper($type) == ‘DECODE’){
$str = base64_decode($str);
$data = mdecrypt_generic($td, $str);
}
//加密
mcrypt_generic_deinit($td);
//结束
mcrypt_module_close($td);
if(strtoupper($type) == ‘ENCODE’){
$data = removeBR(base64_encode($data));
}elseif(strtoupper($type) == ‘DECODE’){
$data = removePadding($data);
}
return $data;
}
//删除填充符
function removePadding( $str ){
$len = strlen( $str );
$newstr = “”;
$str = str_split($str);
for ($i = 0; $i if (!in_array($str[$i],array(chr(0),chr(1),chr(2),chr(3),chr(4),chr(5),chr(6),chr(7),chr(8)))){
$newstr .= $str[$i];
}
}
return $newstr;
}
//填充密码,填充至8的倍数,pkcs7 | pkcs5
function padding( $str ,$pkcs = 5){
if($pkcs == 5){
$pad = 8 – (strlen($str) % 8);
$str .= str_repeat(chr($pad), $pad);
}elseif($pkcs == 7){
$len = 8 – strlen( $str ) % 8;
for ( $i = 0; $i $str .= chr( 0 );
}
}
return $str ;
}
/**
* http://52blogger.com 龙哥博客版权所有,欢迎转载,转载请务必注明来源,违版必究.
*/
//删除回车和换行
function removeBR( $str ){
$len = strlen( $str );
$newstr = “”;
$str = str_split($str);
for ($i = 0; $i if ($str[$i] != ‘\n’ and $str[$i] != ‘\r’){
$newstr .= $str[$i];
}
}
return $newstr;
}
文章来源:龙哥博客 原文:http://www.52blogger.com/archives/821

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment

Notepad++7.3.1
Easy-to-use and free code editor

Zend Studio 13.0.1
Powerful PHP integrated development environment

SecLists
SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

Atom editor mac version download
The most popular open source editor
