[Learning little bit-PHP] A simple Encryption Class

Source: Internet
Author: User
Tags crypt zend framework

This article is from: PhP5 application example details: Use Zend framework and smarty to build a true MVC-mode application.

Author: Wang Zhigang Zhu lei

<? PHP/* a simple Encryption Class (reversible) * Static call, no need to instantiate the crypt class */class crypt {/*** basic encryption, encrypt with passport_key and key * @ Param TXT: plaintext to be encrypted * @ Param key: Key * @ return: Encrypted string */public static function passport_encrypt ($ txt, $ key) {$ encrypt_key = MD5 (RAND (0,32000); $ CTR = 0; $ TMP = ''; For ($ I = 0; $ I <strlen ($ txt); $ I ++) {$ CTR = ($ CTR = strlen ($ encrypt_key )? 0: $ CTR); $ TMP. = $ encrypt_key [$ CTR]. ($ TXT [$ I] ^ $ encrypt_key [$ CTR ++]);} return base64_encode (crypt: passport_key ($ TMP, $ key ));} public static function passport_key ($ txt, $ encrypt_key) {$ encrypt_key = MD5 ($ encrypt_key); $ CTR = 0; $ TMP = ''; For ($ I = 0; $ I <strlen ($ txt); $ I ++) {$ CTR = ($ CTR = strlen ($ encrypt_key )? 0: $ CTR); $ TMP. = $ TXT [$ I] ^ $ encrypt_key [$ CTR ++];} return $ TMP;}/*** decrypt and strip the key * @ Param TXT: password to be decrypted * @ Param key: Key * @ return: decrypted string */public static function passport_decrypt ($ txt, $ key) {$ TXT = crypt :: passport_key (base64_decode ($ txt), $ key); $ TMP = ''; For ($ I = 0; $ I <strlen ($ txt); $ I ++) {$ MD5 = $ TXT [$ I]; $ TMP. = $ TXT [++ $ I] ^ $ MD5;} return $ TMP ;}}$ TXT = "I Will Be backi"; $ key = "justfortest "; $ encrypt = crypt: passport_encrypt ($ txt, $ key); $ decrypt = crypt: passport_decrypt ($ encrypt, $ key); echo "from: $ TXT <br/> "; echo" key: $ key <br/> "; echo" encrypt: $ encrypt <br/> "; echo" decrypt: $ decrypt <br/> ";

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.