PHP history is recognized as the safest reversible cryptographic decryption function _php Tutorial

Source: Internet
Author: User
Tags pkcs7
Previously wrote a plus decryption of the article, also can make use of, now for security, with PHP wrote a Triple DES (3DES) positive and negative decryption function. 3DES is recognized as the safest cryptographic decryption function, but unfortunately PHP does not provide such a ready-made function, so I wrote a 3DES version of the method, 3DES has many versions, the version is the ECB mode, with PKCS7 complement, base64 do ciphertext, security level high, This function is generally used as long as you modify one of the keys.

Test:
Echo (Des3crypt ("Dragon Brother Blog", ' ENCODE '));
echo "
“;
Echo (Des3crypt ("bxc46tetfezfpts1dclzpg==", ' DECODE '));
echo "
“;

/**
* Add 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;
}

Delete a fill character
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;
}

Fill password, fill to multiples 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 Long Brother Blog All rights reserved, welcome reprint, reprint please must indicate source, violate edition must investigate.
*/

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: Dragon Brother Blog Original: http://www.52blogger.com/archives/821

http://www.bkjia.com/PHPjc/363852.html www.bkjia.com true http://www.bkjia.com/PHPjc/363852.html techarticle previously wrote a plus decryption of the article, also can make use of, now for security, with PHP wrote a Triple DES (3DES) of the positive and negative decryption function. 3DES is recognized as the safest encryption decryption ...

  • Related Article

    Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    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.