Php uses an exclusive or implemented encryption and decryption instance _ php instance

Source: Internet
Author: User
String encryption and decryption should be frequently used in data processing, but MD5 encryption is irreversible. Therefore, you can write an encryption and decryption function by yourself. I read a few different or implementation ideas on the Internet, and then I wrote it myself. The idea of XOR is very simple. Use a simple public announcement to express it. if a = B ^ c; then B = a ^ c (^ is an exclusive or meaning ), php converts a character to a binary ascii value when processing an exclusive or character, returns an exclusive or exclusive ascii value, the principle is described as follows:

The code is as follows:


Echo' ';
$ Str = 'World, hao ';
Function jiami ($ str, $ key ){
$ Key = md5 ($ key );
$ K = md5 (rand (0,100); // equivalent to a dynamic key
$ K = substr ($ k, 0, 3 );
$ Tmp = "";
For ($ I = 0; $ I $ Tmp. = substr ($ str, $ I, 1) ^ substr ($ key, $ I, 1 );
}
Return base64_encode ($ k. $ tmp );
}
Function jiemi ($ str, $ key ){
$ Len = strlen ($ str );
$ Key = md5 ($ key );
$ Str = base64_decode ($ str );
$ Str = substr ($ str, 3, $ len-3 );
$ Tmp = "";
For ($ I = 0; $ I $ Tmp. = substr ($ str, $ I, 1) ^ substr ($ key, $ I, 1 );
}
Return $ tmp;
}
$ Key = 'CC ';
$ Keys = jiami ($ str, $ key );
Echo 'Before encryption: '. $ str .'
';
Echo 'encrypted: '. $ encrypted .'
';
Echo 'decrypted: '. jiemi ($ decrypt, $ key ).'
';


With the opportunity to improve, this function implements simple 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.