Share a php encryption and decryption class. it is estimated that it will be used when the user registers and sends an email for verification. the code is as follows:
The code is as follows:
Class SysCrypt {
Private $ crypt_key = 'http: // www.bitsCN.com '; // key
Public function _ construct ($ crypt_key ){
$ This-> crypt_key = $ crypt_key;
}
Public function encrypt ($ txt ){
Srand (double) microtime () * 1000000 );
$ Encrypt_key = md5 (rand (0, 32000 ));
$ Ctr = 0;
$ Tmp = '';
For ($ I = 0; $ I $ Ctr = strlen ($ encrypt_key )? 0: $ ctr;
$ Tmp. = $ encrypt_key [$ ctr]. ($ txt [$ I] ^ $ encrypt_key [$ ctr ++]);
}
Return base64_encode (self :__ key ($ tmp, $ this-> crypt_key ));
}
Public function decrypt ($ txt ){
$ Txt = self ::__ key (base64_decode ($ txt), $ this-> crypt_key );
$ Tmp = '';
For ($ I = 0; $ I $ Md5 = $ txt [$ I];
$ Tmp. = $ txt [++ $ I] ^ $ md5;
}
Return $ tmp;
}
Private function _ key ($ txt, $ encrypt_key ){
$ Encrypt_key = md5 ($ encrypt_key );
$ Ctr = 0;
$ Tmp = '';
For ($ I = 0; $ I $ Ctr = strlen ($ encrypt_key )? 0: $ ctr;
$ Tmp. = $ txt [$ I] ^ $ encrypt_key [$ ctr ++];
}
Return $ tmp;
}
Public function _ destruct (){
$ This-> crypt_key = NULL;
}
}
Usage of this class:
The code is as follows:
$ SC = new SysCrypt ('http: // www.bitsCN.com ');
$ Text = 'yhm. 1234@163.com ';
$ Test1 = $ SC-> encrypt ($ text );
Echo 'original: ', $ text;
Echo'
';
Echo 'ciphertext: ', $ test1;
Echo"
";
Echo 'decrypt: ', $ SC-> decrypt ($ test1 );
The output result is similar:
Original article: yhm.1234@163.com
Ciphertext: BS8DbFU6AioCNFFlVGZQMgRGBDUOYlEzBXoAZgo0Bjk =
Decryption: yhm.1234@163.com