Click here to Skip to main content
15,895,192 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
How to determine if a string is Base64 decoded or not?
Posted

Do it like this:

C#
public bool IsBase64Encoded(String str)
{
    try
    {
        // If no exception is caught, then it is possibly a base64 encoded string
        byte[] data = Convert.FromBase64String(str);
        // The part that checks if the string was properly padded to the
        // correct length was borrowed from d@anish's solution
        return (str.Replace(" ","").Length % 4 == 0);
    }
    catch
    {
        // If exception is caught, then it is not a base64 encoded string
       return false;
    }
}



The function will return true if the string str was Base64 encoded and false if it was not. There is still is a doubt because a string that contains random letters and numbers might still look Base64 encoded even if it was just a random string. The only good answer in this case is false because that will tell you that the string is definitely not a Base64 encoded string.

Cheers and happy coding!

-MRB
 
Share this answer
 
v2
Comments
Sergey Alexandrovich Kryukov 6-Apr-11 19:22pm    
This seems to be better. My 5. The fact it is not certain is OK: this is so by definition: a format cannot be recognized by the formatted data in principle, as it's not meta-data.
--SA
dan!sh 6-Apr-11 22:42pm    
Very true. One can just create some base64 look alike and send it to this method. There, it will fail. Only way to be 100% sure is ask the creator of string.
Sergey Alexandrovich Kryukov 7-Apr-11 0:04am    
Right. And this is usually possible. In email there is the line with Content-type of the message part, in custom networking one can always add some meta-data before data to the protocol, and so forth. This is a matter of design, of the formulation of the problem. In right design, sufficient meta-data portion always goes before the data.
--SA
dan!sh 7-Apr-11 0:07am    
Yes. Had OP told some more about the kind of application he is working on, content-type would have been #1 contender. :)
the headless nick 13-Oct-11 2:41am    
If str = "HELL"

Then It will not throw any exception so we cannot find whether it is Base64String or a Simple String. So It will decode to a Simple String.

Plz check My Question here http://www.codeproject.com/Questions/267500/Alternate-Convert-FromBase64String-Method
-NS
I think you mean encoded, right?

Use Convert.FromBase64String method. If it throws FormatException, then the original string was not base64. Something like this:

public static bool IsBase64(string base64String){
if(base64String.Replace(" ","").Length % 4 != 0){
return false;
}

try{
Convert.FromBase64String(base64String);
return true;
}
catch(FormatException exception){
// Handle the exception
}
return false;
}


You can add null check etc to this method to make it less exception prone.
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 6-Apr-11 19:22pm    
Essentially the same as Manfred's. My 5. See my comment to Manfred's Answer, though.
--SA
Manfred Rudolf Bihy 7-Apr-11 4:45am    
You get my 5 for pointing out that (base64String.Length % 4 == 0) has to be true.
I borrowed that and incorporated it into my solution, but not without giving you proper credit though!

:=)
C#
//java.util.regex.Pattern
private boolean isBase64(String stringBase64){
        String regex =
               "([A-Za-z0-9+/]{4})*"+
               "([A-Za-z0-9+/]{4}|[A-Za-z0-9+/]{3}=|[A-Za-z0-9+/]{2}==)";

        Pattern patron = Pattern.compile(regex);

        if (!patron.matcher(stringBase64).matches()) {
            return false;
        } else {
            return true;
        }
}
 
Share this answer
 
Do you mean encoded?

If so, you can do it like this in C#:

C#
try
{
   byte[] converted = Convert.FromBase64String(base64value);
   return base64value.EndsWith("=");
}
catch
{
   return false;
}
 
Share this answer
 
Comments
Manfred Rudolf Bihy 6-Apr-11 9:52am    
Good one! 5+

The padding character (=) does not have to present though, as it is only used to fill up the number of characters so that str.Length % 4 == 0.

Read here: http://en.wikipedia.org/wiki/Base64
Sergey Alexandrovich Kryukov 6-Apr-11 19:20pm    
Manfred, you did not really vote.
(I did not because I am not sure; you say by yourself that a padding character is not always available by the end (I guess this is the case when all columns are full); I also thing that the problem makes no much sense, if this is e-mail, one could look at Content-type of the part, and so on).
--SA
Manfred Rudolf Bihy 7-Apr-11 4:34am    
Your are right! I'll fix my comment.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900