The most secure forward and reverse encryption and decryption functions in PHP history_PHP Tutorial

WBOY
Release: 2016-07-21 14:58:35
Original
1191 people have browsed it

I have written an article about encryption and decryption before, and it can be used casually. Now for safety, I wrote a forward and reverse encryption and decryption function of triple des (3DES) in PHP. 3DES is recognized as the most secure encryption. Decryption function, but unfortunately PHP does not provide such a ready-made function, so I wrote a 3DES version of the method. There are many versions of 3DES. This version uses ECB mode, using PKCS7 complement and base64 as ciphertext. The security level is high. Generally, when using this function, you only need to modify the key.

Test:
echo (des3crypt( "龙哥blog",'ENCODE'));
echo "
";
echo (des3crypt( "bxC46TETFEZFpTS1DClzpg==",'DECODE' ));
echo “
“;

/**
* Encryption and decryption function
* @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);
}
//Encryption
mcrypt_generic_deinit($td);
//End
mcrypt_module_close($td);
if(strtoupper($type) == 'ENCODE'){
$data = removeBR(base64_encode($data));
}elseif(strtoupper($type) == 'DECODE'){
$data = removePadding($data);
}
return $data;
}

//Remove padding
function removePadding( $str ){
$len = strlen( $str );
$newstr = “”;
$str = str_split($str) ;
for ($i = 0; $i < $len; $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;
}

//Padding password to a multiple of 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 < $len; $i++ ){
$str .= chr( 0 );
}
}
return $str ;
}

/**
* http://52blogger.com All rights reserved by Long Ge Blog. Reprinting is welcome. Please be sure to indicate the source for reprinting. Violations will be investigated.
*/

//Remove carriage returns and line feeds
function removeBR( $str ){
$len = strlen( $str );
$newstr = “”;
$str = str_split($ str);
for ($i = 0; $i < $len; $i++ ){
if ($str[$i] != 'n' and $str[$i] != ' r'){
$newstr .= $str[$i];
}
}
return $newstr;
}

Article source: Long Ge’s blog Original text: http://www.52blogger.com/archives/821

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/363852.htmlTechArticleI wrote an article on encryption and decryption before, and it can be used casually. Now for safety, I wrote one in PHP The forward and reverse encryption and decryption functions of triple des (3DES). 3DES is recognized as the most secure encryption and decryption...
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template