Triple des encryption-PHP source code

Source: Internet
Author: User
Triple des encryption jumps

Class STD3Des {private $ key = ""; private $ iv = "";/*** structure, pass two keys that have been base64_encode and IV ** @ param string $ KEY * @ param string $ iv */function _ construct ($ key, $ iv) {if (empty ($ key) | empty ($ iv) {echo 'key and iv is not valid'; exit ();} $ this-> key = $ key; $ this-> iv = $ iv ;} /*** encrypt * @ param $ value * @ return */public function encrypt ($ value) {$ td = mcrypt_module_open (MCRYPT_3DES, '', MCRYP T_MODE_CBC, ''); $ iv = base64_decode ($ this-> iv); $ value = $ this-> PaddingPKCS7 ($ value ); $ key = base64_decode ($ this-> key); mcrypt_generic_init ($ td, $ key, $ iv); $ ret = base64_encode (mcrypt_generic ($ td, $ value )); mcrypt_generic_deinit ($ td); mcrypt_module_close ($ td); return $ ret;}/*** decrypt * @ param $ value * @ return */public function decrypt ($ value) {$ td = mcrypt_module_open (MCRYPT_3DES, '', MCRYPT_MOD E_CBC, ''); $ iv = base64_decode ($ this-> iv); $ key = base64_decode ($ this-> key); mcrypt_generic_init ($ td, $ key, $ iv); $ ret = trim (mdecrypt_generic ($ td, base64_decode ($ value); $ ret = $ this-> UnPaddingPKCS7 ($ ret ); encrypt ($ td); mcrypt_module_close ($ td); return $ ret;} private function PaddingPKCS7 ($ data) {$ block_size = mcrypt_get_block_size ('tripledes ', 'cbc '); $ padding_char = $ block_s Ize-(strlen ($ data) % $ block_size); $ data. = str_repeat (chr ($ padding_char), $ padding_char); return $ data;} private function UnPaddingPKCS7 ($ text) {$ pad = ord ($ text {strlen ($ text) -1}); if ($ pad> strlen ($ text) {return false;} if (strspn ($ text, chr ($ pad), strlen ($ text) -$ pad )! = $ Pad) {return false;} return substr ($ text, 0,-1 * $ pad );}}

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.