Php-simple symmetric encryption algorithm and conversion functions between strings and hexadecimal notation

Source: Internet
Author: User
: This article mainly introduces the php-simple symmetric encryption algorithm and the conversion function between the string and the hexadecimal system. if you are interested in the PHP Tutorial, refer to it. /**
* Encryption of simple symmetric encryption algorithms
* @ Param String $ string the String to be encrypted
* @ Param String $ skey encrypt EKY
* @ Return String
*/
Function encode ($ string = '', $ skey = 'textphp '){
$ Skey = str_split (base64_encode ($ skey ));
$ StrArr = str_split (base64_encode ($ string ));
$ StrCount = count ($ strArr );
Foreach ($ skey as $ key => $ value ){
$ Key <$ strCount & $ strArr [$ key]. = $ value;
}
Return str_replace ('=', '0o0o', join ('', $ strArr ));
}

/**
* Decryption of simple symmetric encryption algorithms
* @ Param String $ string the String to be decrypted
* @ Param String $ skey decryption KEY
* @ Return String
*/
Function decode ($ string = '', $ skey = 'textphp '){
$ Skey = str_split (base64_encode ($ skey ));
$ StrArr = str_split (str_replace ('0o0o', '=', $ string), 2 );
$ StrCount = count ($ strArr );
Foreach ($ skey as $ key => $ value ){
$ Key <$ strCount & $ strArr [$ key] [1] ===$ value & $ strArr [$ key] = $ strArr [$ key] [0];
}
Return base64_decode (join ('', $ strArr ));
}

// Convert string to hexadecimal
Function str2hex ($ str, $ encoded = 'gbk '){
$ Hex = '';
If ($ encoded = 'gbk '){
$ Str = mb_convert_encoding ($ str, 'gbk', 'utf-8 ');
}
For ($ I = 0, $ length = mb_strlen ($ str); $ I <$ length; $ I ++ ){
$ Hex. = dechex (ord ($ str {$ I }));
}
Return $ hex;
}

// Convert the string in hexadecimal notation
Function hex2str ($ hex, $ encoded = 'gbk '){
$ Str = '';
$ Arr = str_split ($ hex, 2 );
Foreach ($ arr as $ bit ){
$ Str. = chr (hexdec ($ bit ));
}
If ($ encoded = 'gbk '){
$ Str = mb_convert_encoding ($ str, 'utf-8', 'gbk ');
}
Return $ str;
}

The above introduces the php-simple symmetric encryption algorithm and the conversion function between strings and the hexadecimal system, including some content, and hope to be helpful to those who are interested in the PHP Tutorial.

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.