PHP des encryption + ECB mode + PKCS5 completion

Source: Internet
Author: User
Tags pkcs5
PHP des encryption + ECB mode + PKCS5 completion/***** encryption function * algorithm: des * encryption mode: ecb * completion method: PKCS5 ** @ paramunknown_type $ input */functionencryptDe PHP's des encryption + ECB mode + PKCS5 complete
/* Encryption function * algorithm: des * encryption mode: ecb * completion method: PKCS5 ** @ param unknown_type $ input */function encryptDesEcbPKCS5 ($ input, $ key) {$ size = mcrypt_get_block_size ('Des ', 'ECB'); $ input = pkcs5_pad ($ input, $ size); $ td = mcrypt_module_open ('Des ','', 'ECB ', ''); // $ iv = mcrypt_create_iv (mcrypt_enc_get_iv_size ($ td), MCRYPT_RAND); $ iv = 0; mcrypt_generic_init ($ td, $ key, $ iv); $ data = mdecrypt_generic ($ td, $ Indium Ut); mcrypt_generic_deinit ($ td); mcrypt_module_close ($ td); // $ data = base64_encode ($ data); return $ data;}/*** decryption function * algorithm: des * encryption mode: ecb * completion method: PKCS5 * @ param unknown_type $ input */function decryptDesEcbPKCS5 ($ input, $ key) {$ size = mcrypt_get_block_size ('Des ', 'ECB '); $ td = mcrypt_module_open ('Des', '', 'ECB ',''); $ iv = 0; mcrypt_generic_init ($ td, $ key, $ iv); $ data = mcrypt_generic ($ td, $ input); Mcrypt_generic_deinit ($ td); mcrypt_module_close ($ td); $ data = pkcs5_unpad ($ data, $ size); // $ data = base64_encode ($ data ); return $ data;} function pkcs5_pad ($ text, $ blocksize) {$ pad = $ blocksize-(strlen ($ text) % $ blocksize); return $ text. str_repeat (chr ($ pad), $ pad);} function pkcs5_unpad ($ 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);} $ str = 'Test Crypt'; $ key = 'jack11 '; $ res = encryptDesEcbPKCS5 ($ str, $ key); echo "$ res \ n"; $ deRes = decryptDesEcbPKCS5 ($ res, $ key ); echo "\ n $ deRes \ n ";

?

Php des encryption is special and cannot be specified.

Http://www.herongyang.com/Cryptography/DES-PHP-Block-Padding-in-mcrypt.html

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.