Example of php custom encryption function and decryption

Source: Internet
Author: User
Tags md5 strlen

Encryption is the process of converting some characters into another string. Simply put, it is to lock the door and only you have the enabled key. As a programmer, you should have your own encryption method. Currently, the most popular encryption algorithm is the MD5 () encryption method. However, it is not safe to use software that has been cracked on the website. The following describes the custom encryption functions.

The code is as follows: Copy code

"/* Encryption */

Function addcoder ($ str)

    {

$ Yuan = 'Aba! C1dB # ef2 @ Cg $ h % iD_3jkl ^ E: m} 4n. o {& F * p) 5q (G-r [sH] 6tuIv7w + jxy8z9k0 ';

$ Jia = 'Zay % 0Bx + 1C $ wDv ^ Eu2-t3 (F {sr & G4q_pH5 * on6I) m: l7.Jk] j8K} ih @ gf9 # ed! Cb [';

If (strlen ($ str) = 0) return false;

For ($ I = 0; $ I <strlen ($ str); $ I ++)

        {

For ($ j = 0; $ j <strlen ($ yuan); $ j ++)

{

If ($ str [$ I] ==$ yuan [$ j])

                    {

$ Results. = $ jia [$ j];

Break;

}

            }

        }

Return $ results;

    }

 


/* Decrypt */

Function removecoder ($ str)

{

$ Yuan = 'Aba! C1dB # ef2 @ Cg $ h % iD_3jkl ^ E: m} 4n. o {& F * p) 5q (G-r [sH] 6tuIv7w + jxy8z9k0 ';

$ Jia = 'Zay % 0Bx + 1C $ wDv ^ Eu2-t3 (F {sr & G4q_pH5 * on6I) m: l7.Jk] j8K} ih @ gf9 # ed! Cb [';

If (strlen ($ str) = 0) return false;

For ($ I = 0; $ I <strlen ($ str); $ I ++)

{

For ($ j = 0; $ j <strlen ($ jia); $ j ++)

  {

If ($ str [$ I] ==$ jia [$ j])

{

$ Results. = $ yuan [$ j];

Break;

}

  }

}

Return $ results;

    }

$ Str = "www.111cn.net ";
Echo "before encryption:". $ str. "<br> ";
$ Str1 = addcoder ($ str );
Echo "encrypted:". $ str1. "<br> ";
$ Str2 = removecoder ($ str1 );
Echo "decrypted:". $ str2. "<br> ";

Example 2: Use md5 + base64_encode for editing.

The code is as follows: Copy code

<? Php
// Www.111cn.net
Function passport_encrypt ($ txt, $ key ){
Srand (double) microtime () * 1000000 );
$ Encrypt_key = md5 (rand (0, 32000 ));
$ Ctr = 0;
$ Tmp = '';
For ($ I = 0; $ I <strlen ($ txt); $ I ++ ){
$ Ctr = strlen ($ encrypt_key )? 0: $ ctr;
$ Tmp. = $ encrypt_key [$ ctr]. ($ txt [$ I] ^ $ encrypt_key [$ ctr ++]);
}
Return base64_encode (passport_key ($ tmp, $ key ));
}
Function passport_decrypt ($ txt, $ key ){
$ Txt = passport_key (base64_decode ($ txt), $ key );
$ Tmp = '';
For ($ I = 0; $ I <strlen ($ txt); $ I ++ ){
$ Md5 = $ txt [$ I];
$ Tmp. = $ txt [++ $ I] ^ $ md5;
}
Return $ tmp;
}
Function passport_key ($ txt, $ encrypt_key ){
$ Encrypt_key = md5 ($ encrypt_key );
$ Ctr = 0;
$ Tmp = '';
For ($ I = 0; $ I <strlen ($ txt); $ I ++ ){
$ Ctr = strlen ($ encrypt_key )? 0: $ ctr;
$ Tmp. = $ txt [$ I] ^ $ encrypt_key [$ ctr ++];
}
Return $ tmp;
}
?>

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.