base64_encode - base64_decode

Hello,

I downloaded a wordpress theme file from one of the sites and found the below code in the footer.php

<? eval(gzinflate(base64_decode('
bZCxisMwEERrG/wPgz8g6o2iwDV3XZpAajla24tt
SScpEQf5+MjxlRm2WGbYB7MnVRVBGn6AzbEdnEsU
WtXUKCO9gqHIoyWD/q+D1JgCDcd2Ssl3QuScD6ue
6ffOt/lwc2urznZhS7hSHzkRvu68GApSaAVtzWdE
pn5yMbEdR6I57qBCwM/uYjv/3iI8oR+aF90vhCEQ
QacOF+c/YZPz2aeJVvpH4uqC8YFixOVtb1wpSsum
lqL8oPRu6qrad7xVktML
'))); ?>

When I add this inside a PHP code and after a run I get the below:

----------------
                         <div id="footer">
 
 <p> designed by: <a href="http://www.makequick.com">Online Website Builder</a> and: <a href="http://www.webhostinggeeks.com">Web Hosting </a>Geeks | available free at: Top<a href="http://www.topwpthemes.com"> Wordpress Themes</a></p>
</div>

            </div>      
--------------

I was wondering how the author come with this below:

bZCxisMwEERrG/wPgz8g6o2iwDV3XZpAajla24tt
SScpEQf5+MjxlRm2WGbYB7MnVRVBGn6AzbEdnEsU
WtXUKCO9gqHIoyWD/q+D1JgCDcd2Ssl3QuScD6ue
6ffOt/lwc2urznZhS7hSHzkRvu68GApSaAVtzWdE
pn5yMbEdR6I57qBCwM/uYjv/3iI8oR+aF90vhCEQ
QacOF+c/YZPz2aeJVvpH4uqC8YFixOVtb1wpSsum
lqL8oPRu6qrad7xVktML

How can I make these type of encoding? I tried base64_encode but gives some different results than the above encoded text.

Please let me know,
Thank you,
cemlouisAsked:
Who is Participating?
 
Richard QuadlingConnect With a Mentor Senior Software DeveloperCommented:
Just looking at this little snippet ...

<h2><?php _e('Meta');</h2>

You've not closed ?> ...

<h2><?php _e('Meta'); ?></h2>



Right. 2 scripts below. The first is a corrected $str which creates the encoded string.

The second one uses the encoded string along with some dummy functions which I've used for testing.

This all works.

The $str has to be REAL PHP code.

The output of the first script is ...

The string to use in your main script is:
rZExb8MgEIVnR8p/OHmxO1nKWoqHzF3i7hZOL+GkMyCDVeXf90xKM7aVyoA4eO8dn67X+52yB636YAOM2DavmEzz9Nxr1cm9vK4sO+SlmLQyUMrKLnh5qe/eif2V3MW3zRLjYVwXzin1Q50oMRb51mq4uXc6m4SQLEWIJKc1krvCaRju7u/WVfXwKTNNC3zFndAw32CgOTBCiSTvai0pqtu0OoeBFEIlDH/jOft5Rpfi+GuwN4vAwhUTFDMkD/JRCD5KQe4HxGOx/R9rzv4I4ywDbjeNzLaT4eaH/e4T

The output of the second script is ...

<h2>_e:Meta</h2>
<ul>
      <li><a
        href="Bloginfo:rss2_url"
        title="_e:Syndicate this site using RSS">
                _e:<abbr title="Really Simple Syndication">RSS</abbr> </a></li>
      <li><a
        href="Bloginfo:comments_rss2_url"
        title="_e:The latest comments to all posts in RSS">
                _e:Comments <abbr title="Really Simple Syndication">RSS</abbr> <
/a></li>
      wpmeta</ul>


Here you can see that the dummy functions just output the function name and the parameter.


<?php
 
$str = <<<END_TEXT
?>
<h2><?php _e('Meta');?></h2>
<ul>
      <li><a 
      	href="<?php bloginfo('rss2_url');?>" 
      	title="<?php _e('Syndicate this site using RSS');?>">
      		<?php _e('<abbr title="Really Simple Syndication">RSS</abbr>');?> </a></li>
      <li><a 
      	href="<?php bloginfo('comments_rss2_url');?>" 
      	title="<?php _e('The latest comments to all posts in RSS');?>">
      		<?php _e('Comments <abbr title="Really Simple Syndication">RSS</abbr>');?> </a></li>
      <?php wp_meta();?>
</ul><?php
 
END_TEXT;
 
echo 'The string to use in your main script is:', PHP_EOL, base64_encode(gzdeflate($str, 9));
?>
 
<?php
// An example main script.
 
function bloginfo($s)
{
echo "Bloginfo:$s";
}
 
function _e($s)
{
echo "_e:$s";
}
 
function wp_meta()
{
echo "wpmeta";
}
 
eval(gzinflate(base64_decode('rZExb8MgEIVnR8p/OHmxO1nKWoqHzF3i7hZOL+GkMyCDVeXf90xKM7aVyoA4eO8dn67X+52yB636YAOM2DavmEzz9Nxr1cm9vK4sO+SlmLQyUMrKLnh5qe/eif2V3MW3zRLjYVwXzin1Q50oMRb51mq4uXc6m4SQLEWIJKc1krvCaRju7u/WVfXwKTNNC3zFndAw32CgOTBCiSTvai0pqtu0OoeBFEIlDH/jOft5Rpfi+GuwN4vAwhUTFDMkD/JRCD5KQe4HxGOx/R9rzv4I4ywDbjeNzLaT4eaH/e4T')));
?>

Open in new window

0
 
nplibCommented:
you need to reverse it


eval(gzdeflate(base64_encode("string to encode"), 16);   16 is the length of the string

<?php
$string = "Encode Me";
 
$encode = eval(gzdeflate(base64_encode($string), strlen($string)));
echo $encode;
 
echo "<br />";
 
$decode = eval(gzinflate(base64_decode($encode)));
echo $decode;
?>

Open in new window

0
 
Richard QuadlingSenior Software DeveloperCommented:
You'll notice that base64_decode is used with gzinflate.

So, try this ...

If you add an echo $s_b64; in there near the end you'll see that this is the base64 string.
<?php
// Start with some text, but it has to be escaped from PHP
$s_Start = ' ?' . '>Welcome to the Pleasure Dome!<' . '? ';
 
// Compress it.
$s_GZ = gzdeflate($s_Start, 9);
 
// Encode it.
$s_b64 = base64_encode($s_GZ);
 
// Run it.
eval(gzinflate(base64_decode($s_b64)));
 
// or
eval(gzinflate(base64_decode('U7C3C0/NSc7PTVUoyVcoyUhVCMhJTSwuLUpVcAEKKtrYKwAA')));

Open in new window

0
Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

 
Richard QuadlingSenior Software DeveloperCommented:
nplib,

You have to also reverse the order of the operations.

inflate(decode)

encode(deflate)


Not

deflate(encode)
0
 
nplibCommented:
@RQuadling:
other then taking more coding to do it, how is your suggestion any different then mine?
0
 
Richard QuadlingSenior Software DeveloperCommented:
nplib, your code produces ...

Warning: Unexpected character in input:  'B' (ASCII=11) state=1 in C:\-(4) : eva
l()'d code on line 1

Parse error: syntax error, unexpected T_STRING in C:\-(4) : eval()'d code on lin
e 2
<br />


0
 
nplibCommented:
what are you talking about? did you read mine carefully.

$encode has the encoded data,

so you must deflate the encoded data to read it in human
0
 
nplibCommented:
ah, so you have to separate each step.

should have tested it.
0
 
Richard QuadlingSenior Software DeveloperCommented:
Erm, um, mine works ?

Outputs ...

Use s7cLT1VIzMlRyMkvAzIU0vPzUxTSchJzUxXy8ssVEvNSFBLTEzPzFG3sAQ== in your code, like this ...

eval(gzinflate(base64_decode('s7cLT1VIzMlRyMkvAzIU0vPzUxTSchJzUxXy8ssVEvNSFBLTEzPzFG3sAQ==')));
<?php
// Nice and small for nplib
$s_Text = 'We all love a good flame now and again!';
$s_b64 = base64_encode(gzdeflate('?' .">{$s_Text}<" . '?', 9));
 
echo "Use $s_b64 in your code, like this ...
 
eval(gzinflate(base64_decode('$s_b64')));";

Open in new window

0
 
cemlouisAuthor Commented:
Thank you but when I try to run the below I get an error:

<?php
// Start with some text, but it has to be escaped from PHP
$s_Start = '
<h2>Meta</h2>
<ul>
      <li><a href="<?php bloginfo("comments_rss2_url"); ?>" title="<?php _e("The latest comments to all posts in RSS"); ?>"><?php _e("Comments <abbr title="Really Simple Syndication">RSS</abbr>"); ?></a></li>
</ul>
</div>
';
 
// Compress it.
$s_GZ = gzdeflate($s_Start, 9);
 
// Encode it.
$s_b64 = base64_encode($s_GZ);
 
// Run it.
eval(gzinflate(base64_decode($s_b64)));
echo "<br />";
// or
eval(gzinflate(base64_decode('U7C3C0/NSc7PTVUoyVcoyUhVCMhJTSwuLUpVcAEKKtrYKwAA')));
echo "<br />";

echo "$s_b64";
echo "<br />";
?>

Any ideas?
0
 
cemlouisAuthor Commented:
Any ideas?
0
 
Richard QuadlingSenior Software DeveloperCommented:
Like I said, you have to have escaped from PHP in the string you want to use.

Remember that when you use the eval command the text you have encoded is going to be interpreted as PHP code.

So, say your text was "Hello".

Your PHP code would be something like this ...

<?php
eval(gzinflate(base64_decode('xxxxx'));


This would then become ...

<?php
"Hello"


which makes no sense.

Adding the escaping ...

"?>Hello<?"

then you PHP code becomes ...

<?php
?>Hello<?



which is fine.

So, add ?> to the beginning of your string and <? to the end and try the whole lot again.

0
 
Richard QuadlingSenior Software DeveloperCommented:
To see what I'm on about, replace eval(xxxx) with echo xxxx;

Put it in a script all by itself.

You will see the output contains ?> .... <?
0
 
cemlouisAuthor Commented:
Hello RQuadling,

Thank you for the answer but I don't want the output with <? ?> I also want to run the func in it. Echo works but it shows the decoded string but how to run the func in it? I mean like decoding and rendering at the same time.

I added a forum post in devshed and summarized by problem there can you look at the last post in this url: http://forums.devshed.com/php-development-5/printing-the-php-tag-on-the-screen-507742.html

Thank you,
0
 
Richard QuadlingSenior Software DeveloperCommented:
This is a 2 stage process.

1 - Create the string to be used.
2 - Use the created string.

So, let's do this again.

The string I am going to encode is ...

"I want a new laptop and today's date is " . date('r')

So, the output will be a string and the current date time. This is to show you a static string and some dynamic content all wrapped up in the encoded data.

<?php
echo base64_encode(gzdeflate("?>I want a new laptop and today's date is <?php echo date('r');", 9));
?>

This is the code to build your encoded string.

Running this produces a string of ...

s7fzVChPzCtRSFTISy1XyEksKMkvUEjM
S1EoyU9JrFQvVkhJLElVyCxWsLEvyChQ
SE3OyAcLaagXqWtaAwA=

(I chopped it up for the sake of getting it in here)

So, that's the string you need in the main script.

e.g.

<?php
// Somewhere in your main script ...
eval(gzinflate(base64_decode('s7fzVChPzCtRSFTISy1XyEksKMkvUEjMS1EoyU9JrFQvVkhJLElVyCxWsLEvyChQSE3OyAcLaagXqWtaAwA=')));

Running that will output ...

I want a new laptop and today's date is Thu, 07 Feb 2008 23:35:34 +0000

Rerunning it will give you a new date time.


That's it.

0
 
cemlouisAuthor Commented:
Hello again, what am I doing wrong with the below one? I increased this to 500 points also...

<?php
/*
echo base64_encode(gzdeflate("?>I want a new laptop and today's date is <?php echo date('r');", 9));
*/

$str = "?>

<h2><?php _e('Meta');</h2>
<ul>
      <li><a href='<?php bloginfo('rss2_url');' title='<?php _e('Syndicate this site using RSS');'><?php _e('<abbr title='Really Simple Syndication'>RSS</abbr>'); </a></li>
      <li><a href='<?php bloginfo('comments_rss2_url');' title='<?php _e('The latest comments to all posts in RSS');'><?php _e('Comments <abbr title='Really Simple Syndication'>RSS</abbr>');</a></li>
      <?php wp_meta();
</ul>
";
echo base64_encode(gzdeflate($str, 9));
echo "<br />";
// outputs below:
// nZExawMxDIXn+ldo82U6yBrVGTp3yXU/fKkSC3S2Ofso+ffRpRgylBa6SeK9p0/o6IzBsHd4zCHDSJ19p+rt7oC9Tg2u4swLCjv0EBa6vNpv5STpyvGSOruUsh/XRdRjoXIVapotbbjFTz77SlADFyis1Vo4XuE0DJvlaTP6aVpaxIm8yA0GnrMQtBhO0Tp1Yr9pnQaAlg57JfyD85zmmWIt4+/AH4FAlLdUaA6oCRQGciracPwB/a1J/3XD8wmPyK88zvqGbncw2G8vuAM=
// so added below:

eval(gzinflate(base64_decode('nZExawMxDIXn+ldo82U6yBrVGTp3yXU/fKkSC3S2Ofso+ffRpRgylBa6SeK9p0/o6IzBsHd4zCHDSJ19p+rt7oC9Tg2u4swLCjv0EBa6vNpv5STpyvGSOruUsh/XRdRjoXIVapotbbjFTz77SlADFyis1Vo4XuE0DJvlaTP6aVpaxIm8yA0GnrMQtBhO0Tp1Yr9pnQaAlg57JfyD85zmmWIt4+/AH4FAlLdUaA6oCRQGciracPwB/a1J/3XD8wmPyK88zvqGbncw2G8vuAM=')));

// is giving the below error:
// Parse error: syntax error, unexpected '<' in /home/public_html/try30.php(21) : eval()'d code on line 3
?>
0
 
cemlouisAuthor Commented:
What can I say this is it! Thank you much...
0
All Courses

From novice to tech pro — start learning today.