php-simple symmetric encryption algorithm and reciprocal transfer function between string and hexadecimal

Source: Internet
Author: User
Tags decrypt
/**
* Encryption of simple symmetric encryption algorithm
* @param string $string strings that need to be encrypted
* @param String $skey Encryption 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 (' = ', ' o0o0o ', join (', $STRARR));
}

/**
* Decryption of simple symmetric encryption algorithm
* @param string $string A string to decrypt
* @param String $skey Decrypt key
* @return String
*/
function decode ($string = ', $skey = ' textphp ') {
$skey = Str_split (Base64_encode ($skey));
$STRARR = Str_split (Str_replace (' o0o0o ', ' = ', $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));
}

String converted to 16 binary
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;
}

16 binary converted to string
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;
}

This paper introduces the php-simple symmetric encryption algorithm and the reciprocal function between the string and hexadecimal, including the contents of the content, I hope to be interested in the PHP tutorial friends helpful.

  • Related Article

    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.