Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is string base 64 encoded?

Tags:

php

base64

How can I find whether a string is a data encoded with base64_encode() function or not?

Is it possible?

like image 337
Richard Knop Avatar asked Aug 17 '10 07:08

Richard Knop


People also ask

Is base64 encoded string?

Base64 is a group of similar binary-to-text encoding schemes that represent binary data in an ASCII string format by translating it into a radix-64 representation. The term Base64 originates from a specific MIME content transfer encoding.

How do you check if a string is encoded or not?

There is no simple method that will tell you whether a given text is encrypted or not as there are many possible encryption algorithms and non-encryption text manipulations.

How do you check whether a string is base64 encoded or not in Java?

In base64 encoding, the character set is [A-Z, a-z, 0-9, and + /] . If the rest length is less than 4, the string is padded with '=' characters. ^([A-Za-z0-9+/]{4})* means the string starts with 0 or more base64 groups.


2 Answers

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';
}
like image 85
BoltClock Avatar answered Oct 27 '22 06:10

BoltClock


Try this:

if(base64_encode(base64_decode($img, true)) === $img)
   echo 'is a Base64-encoded string' ;
like image 28
Mahdi Bashirpour Avatar answered Oct 27 '22 08:10

Mahdi Bashirpour