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.
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.
If a = B ^ c; then B = a ^ c (^ is an exclusive or meaning), php converts the character to the binary ascii value when processing the exclusive or character, convert the ascii value to a character after obtaining the result. The principle is that the implementation code is pasted later:
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.
,