PHP Encoding and Decode - php
I have a slice of php encoded code but I don't know how it encoded i mean by which why they encoded this code when i use unphp.net successfully it decode the code and get the real code
simple of encoded code define("\x53\x54\117\103\113\x5f\103\x48\105\x43\113", false) my question is how i can encode another code to be like this and it by which why is encoded please anyone have a knowledge about tell me or know any tool to suggest
I would need more information for me to be able to discern what obfuscator was actually used to encode this, but for this specific string, it appears to be a mix of hex- and octal- encoded letters
"\x53\x54\117\103\113\x5f\103\x48\105\x43\113"
\xFF - Hex
\000 - Octal
The sequence above is encoded as follows (where x is hex and 8 is octal): xx888x8x8x8
This sequence may be random or intentional. Assuming it's random, the following function may be used to encode any string in like manner using random_int() for selecting encoding method at random.
<?php
function hex_octal_rand_encode($s) {
$result = '';
foreach (str_split($s) as $c) {
$d = ord($c);
// select encoding method at random (0 = hex, 1 = oct)
$result .= (random_int(0, 1) === 0)
? '\\x' . dechex($d)
: '\\' . decoct($d);
}
return $result;
}
// Test
$input = "STOCK_CHECK";
$encoded = hex_octal_rand_encode($input);
echo sprintf("Encoded: %s\n", $encoded);
$cmd = sprintf('return "%s";', $encoded);
echo sprintf("Decoded: %s\n", eval($cmd));
?>
Sample outputs (notice the encoded value changes):
Encoded: \x53\x54\117\103\x4b\137\x43\x48\x45\x43\x4b
Decoded: STOCK_CHECK
Encoded: \x53\124\x4f\x43\113\x5f\x43\110\x45\x43\x4b
Decoded: STOCK_CHECK
Encoded: \123\124\x4f\x43\113\137\x43\x48\105\x43\113
Decoded: STOCK_CHECK
Related
PHP Convert Unicode to text
I am receiving from a form the following urlencoded string %F0%9D%90%B4%F0%9D%91%99%F0%9D%91%92%F0%9D%91%97%F0%9D%91%8E%F0%9D%91%9B%F0%9D%91%91%F0%9D%91%9F%F0%9D%91%8E If I decode it I get the following formatted text: ๐ด๐๐๐๐๐๐๐๐ Is there any way with PHP to get the plain "Alejandra" text from the encoded or decoded string? I have tried without success several ways to do it with mb_convert_encoding($string, "UTF-16",mb_detect_encoding($string)) iconv('utf-16', 'utf-8', rawurldecode($string) and any other solution I could in stackoverflow. Edit: I tried the proposed solution $strAscii = iconv('UTF-8','ASCII//TRANSLIT',$str); but it deletes the special characters such as รกรฉรญรณรบรฑรง which we need to stay. Expected result input: ๐ด๐๐๐๐๐๐๐๐ output: Alejandra input: รlejandra output: รlejandra Thank you in advance.
urldecode or rawurldecode is sufficient. $string = "%F0%9D%90%B4%F0%9D%91%99%F0%9D%91%92%F0%9D%91%97%F0%9D%91%8E%F0%9D%91%9B%F0%9D%91%91%F0%9D%91%9F%F0%9D%91%8E"; $str = urldecode($string); var_dump($str); //string(36) "๐ด๐๐๐๐๐๐๐๐" Demo: https://3v4l.org/OMQ35 A special debugger gives me: string(36) UTF-8mb4. This means that there are also UTF-8 characters in the string that require 4 bytes. The character A is the Unicode character โ๐ดโ (U+1D434). Note: If the special UTF-8 characters cause problems, you can try to display the strings as ASCII characters with iconv. $strAscii = iconv('UTF-8','ASCII//TRANSLIT',$str); //string(9) "Alejandra"
What you are getting is called a "psuedo-alphabet", you can see a list of them here: https://qaz.wtf/u/convert.cgi. The one that you appear to be getting can be seen here: https://en.wikipedia.org/wiki/Mathematical_Alphanumeric_Symbols Basically what you need to do is take the string, split it and use a lookup table to convert it back to regular characters. This implementation is terribly efficient but that's because I grabbed the alphabets from the above Wikipedia page and was too lazy to reorganise it. function math_symbols_to_plain_text($input, $alphabet) { $alphabets = [ ['a','๐','๐','๐','๐บ','๐ฎ','๐ข','๐','๐ถ','๐ช','๐','๐','๐','๐'], ['b','๐','๐','๐','๐ป','๐ฏ','๐ฃ','๐','๐ท','๐ซ','๐','๐','๐','๐'], ['c','๐','๐','๐','๐ผ','๐ฐ','๐ค','๐','๐ธ','๐ฌ','๐ ','๐','๐','๐'], ['d','๐','๐','๐ ','๐ฝ','๐ฑ','๐ฅ','๐','๐น','๐ญ','๐ก','๐','๐','๐'], ['e','๐','๐','๐','๐พ','๐ฒ','๐ฆ','๐','โฏ','๐ฎ','๐ข','๐','๐','๐'], ['f','๐','๐','๐','๐ฟ','๐ณ','๐ง','๐','๐ป','๐ฏ','๐ฃ','๐','๐','๐'], ['g','๐ ','๐','๐','๐','๐ด','๐จ','๐','โ','๐ฐ','๐ค','๐','๐','๐'], ['h','๐ก','โ','๐','๐','๐ต','๐ฉ','๐','๐ฝ','๐ฑ','๐ฅ','๐','๐','๐'], ['i','๐ข','๐','๐','๐','๐ถ','๐ช','๐','๐พ','๐ฒ','๐ฆ','๐','๐','๐'], ['j','๐ฃ','๐','๐','๐','๐ท','๐ซ','๐','๐ฟ','๐ณ','๐ง','๐','๐','๐'], ['k','๐ค','๐','๐','๐','๐ธ','๐ฌ','๐ ','๐','๐ด','๐จ','๐','๐','๐'], ['l','๐ฅ','๐','๐','๐ ','๐น','๐ญ','๐ก','๐','๐ต','๐ฉ','๐','๐','๐'], ['m','๐ฆ','๐','๐','๐','๐บ','๐ฎ','๐ข','๐','๐ถ','๐ช','๐','๐','๐'], ['n','๐ง','๐','๐','๐','๐ป','๐ฏ','๐ฃ','๐','๐ท','๐ซ','๐','๐','๐'], ['o','๐จ','๐','๐','๐','๐ผ','๐ฐ','๐ค','โด','๐ธ','๐ฌ','๐','๐','๐ '], ['p','๐ฉ','๐','๐','๐','๐ฝ','๐ฑ','๐ฅ','๐ ','๐น','๐ญ','๐','๐','๐ก'], ['q','๐ช','๐','๐','๐','๐พ','๐ฒ','๐ฆ','๐','๐บ','๐ฎ','๐','๐','๐ข'], ['r','๐ซ','๐','๐','๐','๐ฟ','๐ณ','๐ง','๐','๐ป','๐ฏ','๐','๐','๐ฃ'], ['s','๐ฌ','๐ ','๐','๐','๐','๐ด','๐จ','๐','๐ผ','๐ฐ','๐','๐','๐ค'], ['t','๐ญ','๐ก','๐','๐','๐','๐ต','๐ฉ','๐','๐ฝ','๐ฑ','๐','๐','๐ฅ'], ['u','๐ฎ','๐ข','๐','๐','๐','๐ถ','๐ช','๐','๐พ','๐ฒ','๐','๐','๐ฆ'], ['v','๐ฏ','๐ฃ','๐','๐','๐','๐ท','๐ซ','๐','๐ฟ','๐ณ','๐','๐','๐ง'], ['w','๐ฐ','๐ค','๐','๐','๐','๐ธ','๐ฌ','๐','๐','๐ด','๐','๐ ','๐จ'], ['x','๐ฑ','๐ฅ','๐','๐','๐ ','๐น','๐ญ','๐','๐','๐ต','๐','๐ก','๐ฉ'], ['y','๐ฒ','๐ฆ','๐','๐','๐','๐บ','๐ฎ','๐','๐','๐ถ','๐','๐ข','๐ช'], ['z','๐ณ','๐ง','๐','๐','๐','๐ป','๐ฏ','๐','๐','๐ท','๐','๐ฃ','๐ซ'], ['A','๐','๐ด','๐จ','๐ ','๐','๐','๐ผ','๐','๐','๐','๐ฌ','๐ฐ','๐ธ'], ['B','๐','๐ต','๐ฉ','๐ก','๐','๐','๐ฝ','โฌ','๐','๐ ','๐ญ','๐ฑ','๐น'], ['C','๐','๐ถ','๐ช','๐ข','๐','๐','๐พ','๐','๐','โญ','๐ฎ','๐ฒ','โ'], ['D','๐','๐ท','๐ซ','๐ฃ','๐','๐','๐ฟ','๐','๐','๐','๐ฏ','๐ณ','๐ป'], ['E','๐','๐ธ','๐ฌ','๐ค','๐','๐','๐','โฐ','๐','๐','๐ฐ','๐ด','๐ผ'], ['F','๐ ','๐น','๐ญ','๐ฅ','๐','๐','๐','โฑ','๐','๐','๐ฑ','๐ต','๐ฝ'], ['G','๐','๐บ','๐ฎ','๐ฆ','๐','๐','๐','๐ข','๐','๐','๐ฒ','๐ถ','๐พ'], ['H','๐','๐ป','๐ฏ','๐ง','๐','๐','๐','โ','๐','โ','๐ณ','๐ท','โ'], ['I','๐','๐ผ','๐ฐ','๐จ','๐','๐','๐','โ','๐','โ','๐ด','๐ธ','๐'], ['J','๐','๐ฝ','๐ฑ','๐ฉ','๐','๐','๐ ','๐ฅ','๐','๐','๐ต','๐น','๐'], ['K','๐','๐พ','๐ฒ','๐ช','๐','๐','๐','๐ฆ','๐','๐','๐ถ','๐บ','๐'], ['L','๐','๐ฟ','๐ณ','๐ซ','๐','๐','๐','โ','๐','๐','๐ท','๐ป','๐'], ['M','๐','๐','๐ด','๐ฌ','๐ ','๐','๐','โณ','๐','๐','๐ธ','๐ผ','๐'], ['N','๐','๐','๐ต','๐ญ','๐ก','๐','๐','๐ฉ','๐','๐','๐น','๐ฝ','โ'], ['O','๐','๐','๐ถ','๐ฎ','๐ข','๐','๐','๐ช','๐','๐','๐บ','๐พ','๐'], ['P','๐','๐','๐ท','๐ฏ','๐ฃ','๐','๐','๐ซ','๐','๐','๐ป','๐ฟ','โ'], ['Q','๐','๐','๐ธ','๐ฐ','๐ค','๐','๐','๐ฌ','๐ ','๐','๐ผ','๐','โ'], ['R','๐','๐ ','๐น','๐ฑ','๐ฅ','๐','๐','โ','๐ก','โ','๐ฝ','๐','โ'], ['S','๐','๐','๐บ','๐ฒ','๐ฆ','๐','๐','๐ฎ','๐ข','๐','๐พ','๐','๐'], ['T','๐','๐','๐ป','๐ณ','๐ง','๐','๐','๐ฏ','๐ฃ','๐','๐ฟ','๐','๐'], ['U','๐','๐','๐ผ','๐ด','๐จ','๐','๐','๐ฐ','๐ค','๐','๐','๐','๐'], ['V','๐','๐','๐ฝ','๐ต','๐ฉ','๐','๐','๐ฑ','๐ฅ','๐','๐','๐ ','๐'], ['W','๐','๐','๐พ','๐ถ','๐ช','๐','๐','๐ฒ','๐ฆ','๐','๐','๐','๐'], ['X','๐','๐','๐ฟ','๐ท','๐ซ','๐','๐','๐ณ','๐ง','๐','๐','๐','๐'], ['Y','๐','๐','๐','๐ธ','๐ฌ','๐ ','๐','๐ด','๐จ','๐','๐','๐','๐'], ['Z','๐','๐','๐','๐น','๐ญ','๐ก','๐','๐ต','๐ฉ','โจ','๐ ','๐','โค'] ]; $replace = ['a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z','A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z']; $lookup = [ 'serif-normal', 'serif-bold', 'serif-italic', 'serif-bolditalic', 'sans-normal', 'sans-bold', 'sans-italic', 'sans-bolditalic', 'script-normal', 'script-bold', 'franktur-normal', 'fraktur-bold', 'monospace', 'doublestruck' ]; $map_index = array_search($alphabet, $lookup); $split = mb_str_split($input); $output = ''; foreach ($split as $char) { foreach ($alphabets as $i => $letter) { if ($letter[$map_index] === $char) $output .= $replace[$i]; } } return $output; } $input = '๐ด๐๐๐๐๐๐๐๐'; $output = math_symbols_to_plain_text($input, 'serif-italic'); echo $input . PHP_EOL . $output . PHP_EOL; Yields: ๐ด๐๐๐๐๐๐๐๐ Alejandra
If I am not wrong, you are trying to decode URL then why you are not trying to use urldecode() follow this .PHP DOC
Arabic not urlencoding correctly
I have the string: $str = 'ู ุงุฌุฏ'; This need to be encoded as: '%E3%C7%CC%CF' But I cannot figure out how to reach this encoded string. I believe it is Windows-1256. The above encoded string is how it is being encoded by a program I have. Does anyone know how to reach this string?
If you know you want to use Windows-1256 then all you have to do is to change the encoding of the input string (which is UTF-8) to Windows-1256. Then you apply urlencode() to the returned string and that's all. There are several ways to change the encoding of a string in PHP. One of them (that I tested and provides the result you expect) is using iconv(): $str = 'ู ุงุฌุฏ'; $conv = iconv('utf-8', 'windows-1256', $str); echo(urlencode($conv));
You need to somehow split the string into its hexadecimal representation and then put a % singn in front of the hex number pairs. <?php $hexString = bin2hex("ู ุงุฌุฏ"); for($i = 0; $i < strlen($hexString); $i += 2){ echo "%".substr($hexString, $i, 2); } ?> This will do the trick but im sure there is a more elegant way.
How to convert E5%AE%89%E5%85%A8 to this format ๅฎๅ จ in PHP 5.1
I got a keyword var from an url like this: search.php?keyword=%E5%AE%89%E5%85%A8 with utf8 encoding. Then I want to convert the keyword to this format: 安全 So I can use it in MySQL/PHP/ How can I achieve this?
This should work. $html = mb_convert_encoding($_GET['keyword'], 'HTML-ENTITIES', 'UTF-8'); echo htmlspecialchars($html); //ๅฎๅ จ
First one is url encoded, and what you want are html entities. $string = urldecode('%E5%AE%89%E5%85%A8'); // == ๅฎๅ จ $string2 = htmlentities($string); // == ๅฎๅ จ $string3 = htmlspecialchars($string2); // == 安全 The one from your question is double encoded (& got converted to &) which I assume is wrong. 23433 is decimal and equals hexadecimal x5B89. Same for the second code. For the browser, it doesn't matter if it's decimal or hexadecimal. If you really intend double encoding, use htmlspecialchars($string); on the above code.
PHP base64 encode doesn't get decoded by Android
I have a PHP script that creates a QR code out of a username and timestamp. I need to prove the QR code came from my server so I am encrypting the username and timestamp with a private RSA key. In order to get it into the QR code I am then base64 encoding it. I am trialling it with Android first. I can get the string out of the QR code but when I base64 decode it in Android, it returns null. After debugging it seems it's because there are two whitespaces in the string. When the decoder comes to check the illegal characters are divisible by 4, it obviously fails. Getting desperate I removed the whitespaces but then it changed the length so the calculations still didn't work out. Can I change the whitespace for a 'safe' character? Or is the particular encode/decode pair not compatible?? PHP code: $data = base64_encode($username."`".$timestamp); $fp = fopen("private.pem", "r"); $private_key = fread($fp, 8192); fclose($fp); openssl_private_encrypt($data, &$encrypted_data, $private_key); $encrypted_data_64 = base64_encode($encrypted_data); // create QR code Android code: String s = data.getStringExtra("SCAN_RESULT"); byte[] b = Base64.decode(s.toCharArray()); // b is null at this point Base64 code that it bugs out on:is // Check special case int sLen = str != null ? str.length() : 0; if (sLen == 0) return new byte[0]; // Count illegal characters (including '\r', '\n') to know what size the returned array will be, // so we don't have to reallocate & copy it later. int sepCnt = 0; // Number of separator characters. (Actually illegal characters, but that's a bonus...) for (int i = 0; i < sLen; i++) // If input is "pure" (I.e. no line separators or illegal chars) base64 this loop can be commented out. if (IA[str.charAt(i)] < 0) sepCnt++; // Check so that legal chars (including '=') are evenly divideable by 4 as specified in RFC 2045. if ((sLen - sepCnt) % 4 != 0) return null;
PHP base64 encode uses the '+' symbol. When this gets put into a QR code, the '+' comes through as space. Replaced ' ' with '+' and it got decoded fine.
json_encode() non utf-8 strings?
So I have an array of strings, and all of the strings are using the system default ANSI encoding and were pulled from a SQL database. So there are 256 different possible character byte values (single byte encoding). Is there a way I can get json_encode() to work and display these characters instead of having to use utf8_encode() on all of my strings and ending up with stuff like \u0082? Or is that the standard for JSON?
Is there a way I can get json_encode() to work and display these characters instead of having to use utf8_encode() on all of my strings and ending up with stuff like "\u0082"? If you have an ANSI encoded string, using utf8_encode() is the wrong function to deal with this. You need to properly convert it from ANSI to UTF-8 first. That will certainly reduce the number of Unicode escape sequences like \u0082 from the json output, but technically these sequences are valid for json, you must not fear them. Converting ANSI to UTF-8 with PHP json_encode works with UTF-8 encoded strings only. If you need to create valid json successfully from an ANSI encoded string, you need to re-encode/convert it to UTF-8 first. Then json_encode will just work as documented. To convert an encoding from ANSI (more correctly I assume you have a Windows-1252 encoded string, which is popular but wrongly referred to as ANSI) to UTF-8 you can make use of the mb_convert_encoding() function: $str = mb_convert_encoding($str, "UTF-8", "Windows-1252"); Another function in PHP that can convert the encoding / charset of a string is called iconv based on libiconv. You can use it as well: $str = iconv("CP1252", "UTF-8", $str); Note on utf8_encode() utf8_encode() does only work for Latin-1, not for ANSI. So you will destroy part of your characters inside that string when you run it through that function. Related: What is ANSI format? For a more fine-grained control of what json_encode() returns, see the list of predifined constants (PHP version dependent, incl. PHP 5.4, some constants remain undocumented and are available in the source code only so far). Changing the encoding of an array/iteratively (PDO comment) As you wrote in a comment that you have problems to apply the function onto an array, here is some code example. It's always needed to first change the encoding before using json_encode. That's just a standard array operation, for the simpler case of pdo::fetch() a foreach iteration: while($row = $q->fetch(PDO::FETCH_ASSOC)) { foreach($row as &$value) { $value = mb_convert_encoding($value, "UTF-8", "Windows-1252"); } unset($value); # safety: remove reference $items[] = array_map('utf8_encode', $row ); }
The JSON standard ENFORCES Unicode encoding. From RFC4627: 3. Encoding JSON text SHALL be encoded in Unicode. The default encoding is UTF-8. Since the first two characters of a JSON text will always be ASCII characters [RFC0020], it is possible to determine whether an octet stream is UTF-8, UTF-16 (BE or LE), or UTF-32 (BE or LE) by looking at the pattern of nulls in the first four octets. 00 00 00 xx UTF-32BE 00 xx 00 xx UTF-16BE xx 00 00 00 UTF-32LE xx 00 xx 00 UTF-16LE xx xx xx xx UTF-8 Therefore, on the strictest sense, ANSI encoded JSON wouldn't be valid JSON; this is why PHP enforces unicode encoding when using json_encode(). As for "default ANSI", I'm pretty sure that your strings are encoded in Windows-1252. It is incorrectly referred to as ANSI.
<?php $array = array('first word' => array('ะกะปะพะฒะพ','ะะธัะธะปะปะธัะฐ'),'second word' => 'ะะธัะธะปะปะธัะฐ','last word' => 'ะะธัะธะปะปะธัะฐ'); echo json_encode($array); /* return {"first word":["\u0421\u043b\u043e\u0432\u043e","\u041a\u0438\u0440\u0438\u043b\u043b\u0438\u0446\u0430"],"second word":"\u041a\u0438\u0440\u0438\u043b\u043b\u0438\u0446\u0430","last word":"\u041a\u0438\u0440\u0438\u043b\u043b\u0438\u0446\u0430"} */ echo json_encode($array,256); /* return {"first word":["ะกะปะพะฒะพ","ะะธัะธะปะปะธัะฐ"],"second word":"ะะธัะธะปะปะธัะฐ","last word":"ะะธัะธะปะปะธัะฐ"} */ ?> JSON_UNESCAPED_UNICODE (integer) Encode multibyte Unicode characters literally (default is to escape as \uXXXX). Available since PHP 5.4.0. http://php.net/manual/en/json.constants.php#constant.json-unescaped-unicode
I found the following answer for an analogous problem with a nested array not utf-8 encoded that i had to json encode: $inputArray = array( 'a'=>'First item - ร ', 'c'=>'Third item - รฉ' ); $inputArray['b']= array ( 'a'=>'First subitem - รน', 'b'=>'Second subitem - รฌ' ); if (!function_exists('recursive_utf8')) { function recursive_utf8 ($data) { if (!is_array($data)) { return utf8_encode($data); } $result = array(); foreach ($data as $index=>$item) { if (is_array($item)) { $result[$index] = array(); foreach($item as $key=>$value) { $result[$index][$key] = recursive_utf8($value); } } else if (is_object($item)) { $result[$index] = array(); foreach(get_object_vars($item) as $key=>$value) { $result[$index][$key] = recursive_utf8($value); } } else { $result[$index] = recursive_utf8($item); } } return $result; } } $outputArray = json_encode(array_map('recursive_utf8', $inputArray ));
json_encode($str,JSON_HEX_TAG|JSON_HEX_AMP|JSON_HEX_APOS|JSON_HEX_QUOT); that will convert windows based ANSI to utf-8 and the error will be no more.
Use this instead: <?php //$return_arr = the array of data to json encode //$out = the output of the function //don't forget to escape the data before use it! $out = '["' . implode('","', $return_arr) . '"]'; ?> Copy from json_encode php manual's comments. Always read the comments. They are useful.