Php encryption and decryption function code with timeliness

Source: Internet
Author: User

Copy codeThe Code is as follows:
<? Php
Function encode_pass ($ tex, $ key, $ type = "encode", $ expiry = 0 ){
$ ChrArr = array ('A', 'B', 'C', 'D', 'E', 'F', 'G', 'h ', 'I', 'J', 'k', 'l', 'M', 'n', 'O', 'P', 'Q', 'R ', 'S ', 't', 'U', 'V', 'w', 'x', 'y', 'z ',
'A', 'B', 'C', 'D', 'E', 'E', 'F', 'G', 'h', 'I', 'J ', 'k', 'l', 'M', 'n', 'O', 'P', 'Q', 'R', 's','t ', 'U', 'V', 'w', 'x', 'y', 'z ',
'0', '1', '2', '3', '4', '5', '6', '7', '8 ', '9 ');
If ($ type = "decode "){
If (strlen ($ tex) <14) return false;
$ Verity_str = substr ($ tex, 0, 8 );
$ Tex = substr ($ tex, 8 );
If ($ verity_str! = Substr (md5 ($ tex), 0, 8 )){
// Integrity Verification Failed
Return false;
}
}
$ Key_ B = $ type = "decode "? Substr ($ tex, 0, 6): $ chrArr [rand () % 62]. $ chrArr [rand () % 62]. $ chrArr [rand () % 62]. $ chrArr [rand () % 62]. $ chrArr [rand () % 62]. $ chrArr [rand () % 62];

$ Rand_key = $ key_ B. $ key;
// Set the time option
$ Modnum = 0; $ modCount = 0; $ modCountStr = "";
If ($ expiry> 0 ){
If ($ type = "decode "){
$ ModCountStr = substr ($ tex, 6, 1 );
$ ModCount = $ modCountStr = ""? 10: floor ($ modCountStr );
$ Modnum = substr ($ tex, 7, $ modCount );
$ Rand_key = $ rand_key. (floor (time ()-$ modnum)/$ expiry ));
} Else {
$ Modnum = time () % $ expiry;
$ ModCount = strlen ($ modnum );
$ ModCountStr = $ modCount = 10? "A": $ modCount;

$ Rand_key = $ rand_key. (floor (time ()/$ expiry ));
}
$ Tex = $ type = "decode "? Base64_decode (substr ($ tex, (7 + $ modCount): "xugui". $ tex;
} Else {
$ Tex = $ type = "decode "? Base64_decode (substr ($ tex, 6): "xugui". $ tex;
}
$ Rand_key = md5 ($ rand_key );


$ Texlen = strlen ($ tex );
$ Reslutstr = "";
For ($ I = 0; $ I <$ texlen; $ I ++ ){
$ Reslutstr. = $ tex {$ I} ^ $ rand_key {$ I % 32 };
}
If ($ type! = "Decode "){
$ Reslutstr = trim (base64_encode ($ reslutstr), "= ");
$ Reslutstr = $ modCount? $ ModCountStr. $ modnum. $ reslutstr: $ reslutstr;
$ Reslutstr = $ key_ B. $ reslutstr;
$ Reslutstr = substr (md5 ($ reslutstr), 0, 8). $ reslutstr;
} Else {
If (substr ($ reslutstr, 0, 5 )! = "Xugui "){
Return false;
}
$ Reslutstr = substr ($ reslutstr, 5 );
}
Return $ reslutstr;
}
$ Psa = encode_pass ("woshi ceshi yong de", "taintainxousad", "encode", 120 );
Echo $ psa;
Echo "\ r \ n decryption :";
Echo encode_pass ($ psa, "taintainxousad", 'decode', 120 );
?>

This function is time-sensitive and cannot be decrypted as long as it expires! Diversified data after time-based dynamic encryption and encryption increase the difficulty of cracking

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.