PHP Hex Cryptographic decryption Function Use example

Source: Internet
Author: User
Tags ord strlen


Encrypted decryption


Hex (Hex (original + key). GetBytes ("Utf-8")). GetBytes ("Utf-8"))
function String2hex ($string) {
$hex = ';
for ($i =0; $i < strlen ($string); $i + +) {
$hex. = Dechex (Ord ($string [$i]));
}
return $hex;
}

function hex2string ($hex) {
$string = ';
For ($i =0 $i < strlen ($hex)-1; $i +=2) {
$string. = Chr (Hexdec ($hex [$i]. $hex [$i +1]));
}
return $string;
}

Example

$hex = String2hex ("Test sentence ...");

$data = Array (
' AppId ' => ' Shan-guo-chi-bi ',
' Data ' => ' {appId ': ' Shan-guo-chi-bi ', ' cardno ': ' 6006012000000001 '} ',
' Sig ' => ' 4331443139334331313645464642444544364334343742374236364335454243 ',
);

$key = ' 111111111111111111111111 ';

$data [' sig '] = String2hex (Strtoupper (MD5 ($data [' data ']. $key))];

Example

PHP Convert output string to Hex Oct
The following code allows you to change the string of PHP output into 16 and 8.

<?php
$str = "Http://www.111cn.net";
$lixiphp = ";
for ($i = 0; $i < strlen ($STR); $i + +) {
if ($i% 2 = 0) {
$lixiphp. = ' \x '. Base_convert (Ord ($str [$i]), 10, 16);
}
else {
$lixiphp. = ' \ n ' Base_convert (ord ($str [$i]), 10, 8);
}
}
Echo $lixiphp;
?>

Using the code above, convert the string "Http://www.111cn.net" to "\x68\164\x74\160\x3a\57\x2f\142\x6c\157\x67\56\x6c\151\x78\151\x70\150\ X70\56\X63\157\X6D "

PHP implements the base-digit string to convert hexadecimal, and an even-digit string into octal.

The following figure is an ASCII code table that can simply check the relationship between 10, 8, and 16 and 127 commonly used characters.

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.