Php supports encryption and decryption in Chinese and English.-PHP Tutorial

Source: Internet
Author: User
Php supports encryption and decryption code in both Chinese and English. The php encryption class shared in this article is a php implementation class file that supports Chinese and English encryption and password decryption. if you need it, refer to it, however, it is best to compile the document. The php encryption class shared in this article is a php implementation class file that supports Chinese and English encryption and password decryption. if you need it, please refer to it, but it is best to set the document encoding to UTF-8.

The following code is saved as the MD5Crypt. class. php file.

The code is as follows:
Class MD5Crypt {
/**
* Enter description here...
* @ Param unknown_type $ str
* @ Return string
*/
Public final static function mdsha ($ str ){
$ Code = substr (md5 ($ str), 10 );
$ Code. = substr (sha1 ($ str), 0, 28 );
$ Code. = substr (md5 ($ str), 0, 22 );
$ Code. = substr (sha1 ($ str), 16). md5 ($ str );
Return self: chkToken ()? $ Code: null;
}
/**
* Enter description here...
* @ Param unknown_type $ param
*/
Private final static function chkToken (){
Return true;
}
/**
* Enter description here...
* @ Param unknown_type $ txt
* @ Param unknown_type $ encrypt_key
* @ Return Ambigous
*/
Private final static function keyED ($ txt, $ encrypt_key ){
$ Encrypt_key = md5 ($ encrypt_key );
$ Ctr = 0;
$ Tmp = "";
For ($ I = 0; $ I <strlen ($ txt); $ I ++ ){
If ($ ctr = strlen ($ encrypt_key ))
$ Ctr = 0;
$ Tmp. = substr ($ txt, $ I, 1) ^ substr ($ encrypt_key, $ ctr, 1 );
$ Ctr ++;
}
Return $ tmp;
}
/**
* Enter description here...
* @ Param unknown_type $ txt
* @ Param unknown_type $ key
* @ Return string
*/
Public final static function Encrypt ($ txt, $ key ){
Srand (double) microtime () * 1000000 );
$ Encrypt_key = md5 (rand (0, 32000 ));
$ Ctr = 0;
$ Tmp = "";
For ($ I = 0; $ I <strlen ($ txt); $ I ++ ){
If ($ ctr = strlen ($ encrypt_key ))
$ Ctr = 0;
$ Tmp. = substr ($ encrypt_key, $ ctr, 1). (substr ($ txt, $ I, 1) ^ substr ($ encrypt_key, $ ctr, 1 ));
$ Ctr ++;
}
$ _ Code = md5 ($ encrypt_key). base64_encode (self: keyED ($ tmp, $ key). md5 ($ encrypt_key. $ key );
Return self: chkToken ()? $ _ Code: null;
}
/**
* Enter description here...
* @ Param unknown_type $ txt
* @ Param unknown_type $ key
* @ Return Ambigous
*/
Public final static function Decrypt ($ txt, $ key ){
$ Txt = self: keyED (base64_decode (substr ($ txt, 32,-32), $ key );
$ Tmp = "";
For ($ I = 0; $ I <strlen ($ txt); $ I ++ ){
$ Md5 = substr ($ txt, $ I, 1 );
$ I ++;
$ Tmp. = (substr ($ txt, $ I, 1) ^ $ md5 );
}
Return self: chkToken ()? $ Tmp: null;
}
/**
* Enter description here...
* @ Var unknown_type
*/
Private static $ _ key = 'lau ';
}
?>

Usage

The code is as follows:
Define ('workspace ','. '. DIRECTORY_SEPARATOR );
Header ("Content-Type: text/html; charset = utf-8 ");
Include_once 'core/Library/MD5Crypt. class. php ';
$ A = MD5Crypt: Encrypt ("A", 100 );
Echo "EnCode:". $ ,"
";
Echo "DeCode:". MD5Crypt: Decrypt ($ a, 100 );
?>

...

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.