Php is recognized as the most secure forward and reverse encryption and decryption function in history _ PHP Tutorial

Source: Internet
Author: User
Tags pkcs7
Php is recognized as the safest forward and reverse encryption and decryption function in history. I have written an article on encryption and decryption before, which can be used together. now I have written a forward and backward encryption and decryption function of tripledes (3DES) in php for security purposes. 3DES is recognized as the most secure encryption and decryption. I wrote an article on encryption and decryption, which can be used together. now, for security, I wrote a triple des (3DES) in php) forward and backward encryption and decryption functions. 3DES is recognized as the most secure encryption and decryption function, but it is a pity that php does not provide such a ready-made Function. as a result, it has written a 3DES method. 3DES has many versions, this version uses the ECB mode. it uses PKCS7 as the complement code and base64 as the ciphertext. the security level is high. Generally, you only need to modify the key of this function.

Test:
Echo (des3crypt ("Longge blog", 'encoding '));
Echo"
";
Echo (des3crypt ("bxC46TETFEZFpTS1DClzpg =", 'decode '));
Echo"
";

/**
* Encryption and decryption functions
* @ Param $ str
* @ Param $ type
* @ Param $ key
*/
Function des3crypt ($ str, $ type = 'encoding', $ 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, '000000 ′);
If (strtoupper ($ type) = 'encoding '){
$ 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) = 'encoding '){
$ Data = removeBR (base64_encode ($ data ));
} Elseif (strtoupper ($ type) = 'decode '){
$ Data = removePadding ($ data );
}
Return $ data;
}

// Delete the padding 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 in the password, which is 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 Longge blog copyright, welcome to reprint, reprint please be sure to indicate the source, illegal version of the study.
*/

// Delete the carriage return and line feed
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 Brother blog original: http://www.52blogger.com/archives/821

Finally, for the sake of security, I used php to write a positive and negative encryption and decryption function for triple des (3DES). 3DES is recognized as the most secure encryption and decryption...

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.