base64 encoded then decoded failing - php

Here is the base64 encoded smilies generated form my android phone and saved into MySQL when I try to display it in browser first I need to check if the string is base64 encoded then I decoded and print it, but below code fails to
$mystring = "8J+YgPCfmInwn5iZ8J+YkPCflIfwn5Oi8J+OtfCfjqfwn5C18J+QiPCfpoTwn5CX8J+MjfCfl7vw\nn4+f8J+PofCfj4Himb/wn5uC8J+atA==\n";
if (base64_encode(base64_decode($mystring)) == $mystring) {
echo base64_decode($mystring);
} else {
echo $mystring;
}
but if I directly decode same string and print it works
echo base64_decode($mystring);
// output πŸ˜€πŸ˜‰πŸ˜™πŸ˜πŸ”‡πŸ“’πŸŽ΅πŸŽ§πŸ΅πŸˆπŸ¦„πŸ—πŸŒπŸ—»πŸŸπŸ‘πŸβ™ΏπŸ›‚πŸš΄
however same code worked for iOS phone generated smiles.
$mystring = "8J+YgvCfkoHwn5iK8J+RqeKAjfCfkanigI3wn5Gn8J+ZiPCfpKPwn5mJ8J+QvPCfkLbwn5CT8J+MmeKaoe+4j/CfjY7wn42U8J+NlfCfpYPimr3vuI8=";
if(base64_encode(base64_decode($mystring)) == $mystring) {
echo base64_decode($mystring);
} else {
echo $mystring;
}
//output πŸ˜‚πŸ’πŸ˜ŠπŸ‘©β€πŸ‘©β€πŸ‘§πŸ™ˆπŸ€£πŸ™‰πŸΌπŸΆπŸ“πŸŒ™βš‘οΈπŸŽπŸ”πŸ•πŸ₯ƒβš½οΈ
I don't understand what is the problem with this block:
if(base64_encode(base64_decode($mystring)) == $mystring)

if(base64_encode(base64_decode($mystring)) == $mystring)
You cannot compare the content of two strings in that way.
Use strcmp() function.
Moreover you cannot check in this way if $mystring is base64 encoded.
You will always get true as encoding a string and then decoding the result will always deliver the original string.
Comparing wil only tell you if encoding/decoding works.

Related

PHP Encoding and Decode

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

How to multilevel base64 decoder and show proper php code

How to multilevel base64 decoder and show proper php code....
i.e.....
base64_decode(
base64_decode(
'SkhSb2FYTXRQazh3TUU4d01FOVBUMDh3VDA4Z1BTQm1ZV3h6WlRzZ0NRa2tUekF3VDA4d1R6QXdUMDh3VHlBOUlITjBjblJ2Ykc5M1pYSW9jSEpsWjE5eVpYQnNZV05sS0dKaGMyVTJORjlrWldOdlpHVW9KMGw1T0hWTGFWRnFKeWtzSUNjbkxDQWtYMU5GVWxaRlVsdGlZWE5sTmpSZlpHVmpiMlJsS0NkVk1GWlRWbXRXVTFneFFsTlVNVkpRVVRBNVRTY3BYU2twT3c9PQ=='
)
);
Any buddy have php script to decode multi level base64_decode() and show proper php code.
Note:
Encoded string will be decoded to exact string if the string encoded or decoded equal no. of times.
It means two level encoded string can be decoded 2 level.
<?php
$string="Hello Stack Overflow";
echo $encodedString=base64_encode(base64_encode($string));//Multilevel(2) encode
echo base64_decode(base64_decode($encodedString));//Multilevel(2) decode
I would recommend to create a function similar to this:
function multiBase64Decode($string, int $iteration = 1){
for($i=0;$i<$iteration;$i++){
$string = base64_decode($string);
}
return $string;
}

Base64 Decode in php with a json_decode

Hi i have the following issue where in some instances the json_decode does not work and i get an empty array as follows.
// for test purpose set the inbound enc parameter
$_POST["enc"] = "eyJ0cmFuc2NyaXB0IjoiLSAgICAgICAgICBQYXN0ZWQgdGhlIHRleHQgaW50byBOb3RlcGFkIBMgbm8gc3BlY2lhbCBjaGFyYWN0ZXJzIiwiaWQiOjcwLCJpc0FjdGlvbmVkIjp0cnVlLCJ1c2VyX2lkIjoxLCJ0YXNrX3R5cGVfaWQiOjEsImFjY291bnRfaWQiOjIxLCJhY2NvdW50X25hbWUiOiJURVNUIiwiZXZlbnRfZGF0ZSI6bnVsbH0=";
$decoded = base64_decode($_POST["enc"]);
$ar = (array)json_decode($decoded);
echo "<pre>";
print_r($decoded);
echo "</pre>";
echo "<pre>";
print_r($ar);
echo "</pre>";
$decoded displays as a json string, but $ar is null.
Any help please will be appreciated in helping me with this issue.
You didn't supply the second parameter in json_decode to return as an array, and not an object
// for test purpose set the inbound enc parameter
$_POST["enc"] = "eyJ0cmFuc2NyaXB0IjoiLSAgICAgICAgICBQYXN0ZWQgdGhlIHRleHQgaW50byBOb3RlcGFkIBMgbm8gc3BlY2lhbCBjaGFyYWN0ZXJzIiwiaWQiOjcwLCJpc0FjdGlvbmVkIjp0cnVlLCJ1c2VyX2lkIjoxLCJ0YXNrX3R5cGVfaWQiOjEsImFjY291bnRfaWQiOjIxLCJhY2NvdW50X25hbWUiOiJURVNUIiwiZXZlbnRfZGF0ZSI6bnVsbH0=";
$decoded = base64_decode($_POST["enc"]);
$ar = json_decode($decoded, true); //<-- Now returned as an array, and not an object
echo "<pre>";
print_r($decoded);
echo "</pre>";
echo "<pre>";
print_r($ar);
echo "</pre>";
Output
{"transcript":"- Pasted the text into Notepad no special characters","id":70,"isActioned":true,"user_id":1,"task_type_id":1,"account_id":21,"account_name":"TEST","event_date":null}
The error is in your JSON - oddly in the two spaces between "Notepad" and "no".
It looks like there's a nonstandard character between the spaces. Remove that character and the JSON is valid.
Invalid
{"transcript":"- Pasted the text into Notepad no special characters","id":70,"isActioned":true,"user_id":1,"task_type_id":1,"account_id":21,"account_name":"TEST","event_date":null}
Valid
{"transcript":"- Pasted the text into Notepad no special characters","id":70,"isActioned":true,"user_id":1,"task_type_id":1,"account_id":21,"account_name":"TEST","event_date":null}
In the future, ideally you would use json_encode to build your JSON string. The function will automatically escape any non-valid characters for you.
Taking your code and decoding the Base64 encoded string it turns out you have a CHR(13) ASCII character in your JSON data which is causing the JSON to fail validation according to JSON Lint. Taking that character out results in the JSON parsing correctly.
PHP Fiddle
Decoded JSON data:
{
"transcript": "- Pasted the text into Notepad no special characters",
"id": 70,
"isActioned": true,
"user_id": 1,
"task_type_id": 1,
"account_id": 21,
"account_name": "TEST",
"event_date": null
}
HEX Editor Screenshot:

How to check json encoded string?

I found that many of you use a function like this...
function isJson($string) {
json_decode($string);
return (json_last_error() == JSON_ERROR_NONE);
}
(from Fastest way to check if a string is JSON in PHP?)
Many of json encoded string contain these characters { : , "
If i check the string that contain these characters it may return true.
example:
$var = '{"1":"a2","a2":{"1":"a2.2","2":"a2.2"}}';
var_dump(isJson($var)) // true;
But when i check the string that is just a number, it return true too.
example:
$phone = '021234567';
var_dump(isJson($phone)); // true
I don't think the string '021234567' is json encoded and it should return false.
Let see more example for more clear question.
$var1 = '021234567'; // this is string. not integer. NOT json encoded.
$var2 = '"021234567"'; // this is json encoded.
$var3 = 021234567; // this is integer. NOT json encoded.
$var4 = 'bla bla'; // this is string. NOT json encoded.
$var5 = '{"1":"a2","a2":{"1":"a2.2","2":"a2.2"}}'; // this is json encoded.
var_dump(isJson($var1));
var_dump(isJson($var2));
var_dump(isJson($var3));
var_dump(isJson($var4));
var_dump(isJson($var5));
the results are:
true
true
true
false
true
But the expected results are:
false
true
false
false
true
The question is. How to check the string that is valid json encoded?
More example 2
If 021234567 is an integer. when i use json_encode it returns integer and have nothing to do with it again when use json_decode
$var = 021234567;
echo $var; // 4536695
echo json_encode($var); // 4536695
echo json_decode(4536695); // 4536695
echo json_decode($var); // 4536695
$var = 21234567;
echo $var; // 21234567
echo json_encode($var); // 21234567
echo json_decode(4536695); // 21234567
echo json_decode($var); // 21234567
so, the integer value should return false when i check json encoded string with isJson function. but it is not.
If 021234567 is string. when i use json_encode i should got "021234567" (with double quote). if this string has no double quote, it should not be json encoded.
$var = '021234567';
echo $var; // 021234567
echo json_encode($var); // "021234567"
echo json_decode('"021234567"'); // 021234567
echo json_decode($var); // 21234567
// example if string is text with double quote in it.
$var = 'i"m sam';
echo $var; // i"m sam
echo json_encode($var); // "i\"m sam"
echo json_decode('"i\"m sam"'); // i"m sam
echo json_decode($var); // null
As you see. the simple string with json encoded should contain at least double quote character at open and close of that string.
Please help me how to check that string is valid json encoded.
I think the valid json encoded string should at least have double quote " or curly bracket {} at the open and close of that string.
I don't know how to check that or don't know how to preg_match that.
From PHP Documentation:
PHP implements a superset of JSON - it will also encode and decode scalar types and NULL. The JSON standard only supports these values when they are nested inside an array or an object.
So, the reason leading zeros make this check pass is that json_decode just strips them during parsing without giving any errors. For example:
echo json_decode('0123');
will just display 123.

Is string base 64 encoded?

How can I find whether a string is a data encoded with base64_encode() function or not?
Is it possible?
Attempt to decode it strictly against the Base64 alphabet. The second parameter allows you to enforce this strict check; by leaving it out, the decoding function simply strips out illegal characters before decoding:
if (base64_decode($str, true) === false)
{
echo 'Not a Base64-encoded string';
}
Try this:
if(base64_encode(base64_decode($img, true)) === $img)
echo 'is a Base64-encoded string' ;

Categories