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

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

Jul 21, 2016 pm 02:58 PM
php use function add encrypt and decode Safety article most Now of Decrypt

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...
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

Hot Article Tags

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian Dec 24, 2024 pm 04:42 PM

PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian

CakePHP Date and Time CakePHP Date and Time Sep 10, 2024 pm 05:27 PM

CakePHP Date and Time

CakePHP Project Configuration CakePHP Project Configuration Sep 10, 2024 pm 05:25 PM

CakePHP Project Configuration

CakePHP File upload CakePHP File upload Sep 10, 2024 pm 05:27 PM

CakePHP File upload

CakePHP Routing CakePHP Routing Sep 10, 2024 pm 05:25 PM

CakePHP Routing

Discuss CakePHP Discuss CakePHP Sep 10, 2024 pm 05:28 PM

Discuss CakePHP

CakePHP Quick Guide CakePHP Quick Guide Sep 10, 2024 pm 05:27 PM

CakePHP Quick Guide

How To Set Up Visual Studio Code (VS Code) for PHP Development How To Set Up Visual Studio Code (VS Code) for PHP Development Dec 20, 2024 am 11:31 AM

How To Set Up Visual Studio Code (VS Code) for PHP Development

See all articles