Jump to content

Base64 Decode


ryancooper

Recommended Posts

Hi, Im trying to use base64_decode on a string, however the string is sometimes plain text others its encoded, how can i test to see if the sting is encoded or plaintext and only perform the decode if the string is indeed encoded? Im pretty new to PHP, but ive seen mentions of regex or preg_match for certain characteristics of base64 encoded, my one observation is it usually ends in == which wouldnt appear in my plain text, could i do a preg match for that?

Link to comment
Share on other sites

I'm not super familiar with base64, but if each encoded string ends with ==, you could do something like the following to see if it is encoded:

 

<?php

$decoded = (preg_match('/[a-z0-9]+==/i', $stringtotest) == 1) ? base64_decode($stringtotest) : $stringtotest;

?>

Link to comment
Share on other sites

Awesome, apparently there is only two equal signs if there is an extra byte left over, one if not, however this should work... Could you tell me what the code would look like with my setup? The text file is pulled in as $data, so what would i change in your example? Thanks again.

Link to comment
Share on other sites

Replace the three instances of $stringtotest with the variable that contains your *possible* base64 hash. I'm guessing that's your text file that you have stored in $data?

 

If you wanted to continue to refer to it as $data after the test, replace $decoded with $data as well.  Either way, the $decoded/$data variable will contain the decoded version of the base64.

Link to comment
Share on other sites

This may work better. It also matches newlines, unless you're sure there will never be any, and makes sure the equal signs are at the end:

$data = preg_match('/^[a-z0-9\r\n]+={0,2}$/i', trim($stringtotest)) ? base64_decode($stringtotest) : $stringtotest;

Link to comment
Share on other sites

Awesome that worked perfectly, is the match for any = or only strings that end in = ? I realized i may use = at some point but never end with it in plain text, thanks so much for your help, couldnt of been easier, all the code ive looked at was pretty complex and above me.

Link to comment
Share on other sites

I had to change the match to just one =. The solution from nethnet decodes fine but seems to search the whole string not just the end of it, and ive realized if if i have HTML in the sting such as a href="" then the plain text is decoded and returns null. I can get the dcro2 solution to run but it leaves the string encoded? Is it possible the match is off?

Link to comment
Share on other sites

Hmm.. it seems we all missed two other valid characters in base64 (+ and /). This should work:

$data = preg_match('/^[+\/a-z0-9\r\n]+={0,2}$/i', trim($stringtotest)) ? base64_decode($stringtotest) : $stringtotest;

 

That worked perfect, thank you so much, i really appreciate it.

Link to comment
Share on other sites

But "cat" isn't base64-encoded...

(4 characters)* (2 characters (= | 1 character) =)?

(where a "character" is not whitespace)

Well... what can you do? It's not perfect, but I don't think there's a perfect method to detecting a base64 string. As long as the data is more than one word it works.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.