Ask the php experts to check the principles of this encryption algorithm function & nbsp; sys_auth ($ str, & nbsp; $ action & nbsp ;=& nbsp; 'encoding', & nbsp; $ key & nbsp ;=& nbsp; '') {$ key & nbsp ;=& nbsp; md5 ($ key); $ str & nbsp; seek php experts, let's take a look at the principles of this encryption algorithm.
function sys_auth($str, $action = 'encode', $key = ''){
$key = md5($key);
$str = $action == 'encode' ? $str : base64_decode($str);
$strlen = strlen($str);
$keylen = strlen($key);
$code = '';
for($i = 0; $i < $strlen; $i++){
$k = $i % $keylen;
$code .= $str[$i] ^ $key[$k];
}
$code = $action == "decode" ? $code : base64_encode($code);
return $code;
}
I echo the code in every sentence:
Function sys_auth ($ str, $ action = 'encoding', $ key = ''){
$ Key = md5 ($ key );
Echo'
$ Key: '. $ key;
$ Str = $ action = 'encoding '? $ Str: base64_decode ($ str );
Echo'
$ Str: '. $ str;
$ Strlen = strlen ($ str );
Echo'
$ Str length '. $ strlen;
$ Keylen = strlen ($ key );
Echo'
$ Keylen length '. $ keylen;
$ Code = '';
For ($ I = 0; $ I <$ strlen; $ I ++ ){
$ K = $ I % $ keylen;
Echo"
The \ $ k value of the ". $ I." subcycle is ". $ k;
$ Code. = $ str [$ I] ^ $ key [$ k];
Echo"
The ". $ I. 'Round Robin $ k value is'. $ k;
Echo"
The ". $ I. 'loop $ str [$ I] value is'. $ str [$ I];
Echo"
The ". $ I. 'Round Robin $ key [$ k] value is'. $ key [$ k];
Echo"
The value of ". $ I." The \ $ code of the next loop is ". $ code ."
";
}
$ Code = $ action = "decode "? $ Code: base64_encode ($ code );
Echo"
". $ Code;
Return $ code;
}
The result is:
[Code = PHP]
$ Key: c81e728d9d4c2f636f067f89cc14862c
Encrypted $ str: 123456
$ Str 6 characters in length
$ Keylen length 32
The value of $ k for 0th cycles is 0.
0th cycles $ k value is 0
0th cycles $ str [$ I] The value is 1
The value of $ key [$ k] for 0th cycles is c.
The value of $ code for 0th cycles is R.
The value of $ k for 1st cycles is 1.
1st cycles $ k is 1
1st cycles $ str [$ I] value is 2
The value of $ key [$ k] for 1st cycles is 8.
The value of $ code for 1st cycles is R.
The value of $ k for 2nd cycles is 2.
2nd Cycles $ the k value is 2
The value of $ str [$ I] for 2nd cycles is 3.
The value of $ key [$ k] for 2nd cycles is 1.
The value of $ code for 2nd cycles is R.
The value of $ k for 3rd cycles is 3.
3rd cycles $ the k value is 3
The value of 3rd cycles $ str [$ I] is 4.
3rd cycles $ key [$ k] value is e
The value of $ code for 3rd cycles is r q.
The value of $ k for 4th cycles is 4.
4th cycles $ the k value is 4
4th cycles $ str [$ I] value is 5
4th cycles $ key [$ k] value is 7
The value of $ code for 4th cycles is r q.
The value of $ k for 5th cycles is 5.
5th cycles $ the k value is 5
5th cycles $ str [$ I] value is 6
The value of $ key [$ k] for 5th cycles is 2.
The value of $ code for 5th cycles is r q.
UgoCUQIE
After encryption: UgoCUQIE
[/Code]
The problem is: the value of $ str [0] for 0th cycles is 1, and the value of $ key [0] is c.
I directly follow the above $ str [0] ^ $ key [0], (that is, 1 ^ 0), but an error is reported. what is the problem, in addition, $ str [0] ^ $ key [0] does not understand
------ Solution --------------------
Manual, operator