276

I want to decode a Base64 encoded string, then store it in my database. If the input is not Base64 encoded, I need to throw an error.

How can I check if a string is Base64 encoded?

SharpC
  • 6,974
  • 4
  • 45
  • 40
loganathan
  • 5,838
  • 8
  • 33
  • 50
  • 1
    Why? How can the situation arise? – user207421 Oct 09 '15 at 09:25
  • 3
    without specifying which programming language (and/or) Operating System you are targeting, this is a very open question – bcarroll Jan 05 '16 at 16:32
  • 12
    All that you can determine is that the string contains only characters that are valid for a base64 encoded string. It may not be possible to determine that the string is the base64 encoded version of some data. for example `test1234` is a valid base64 encoded string, and when you decode it you will get some bytes. There is no application independent way of concluding that `test1234` is not a base64 encoded string. – Kinjal Dixit Feb 10 '16 at 11:56
  • https://play.golang.org/p/RnEBFCJ9h0 – BentCoder Sep 26 '19 at 15:29

25 Answers25

335

You can use the following regular expression to check if a string constitutes a valid base64 encoding:

^([A-Za-z0-9+/]{4})*([A-Za-z0-9+/]{3}=|[A-Za-z0-9+/]{2}==)?$

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.

([A-Za-z0-9+/]{4}|[A-Za-z0-9+/]{3}=|[A-Za-z0-9+/]{2}==)$ means the string ends in one of three forms: [A-Za-z0-9+/]{4}, [A-Za-z0-9+/]{3}= or [A-Za-z0-9+/]{2}==.

Michael Mior
  • 28,107
  • 9
  • 89
  • 113
xuanyuanzhiyuan
  • 3,811
  • 1
  • 14
  • 6
  • 18
    Just wanted to verify so please help with my question : What is the guarantee that this regex will always refers to only base64 string?? If there is any string having no space and it is multiple of 4 characters, then will that string be considered as base64 string???? – DShah Oct 01 '12 at 10:11
  • 5
    Then it is a valid base64 string which can be decoded. You could add a minimum length constraint; for example, instead of zero or more repetitions of groups of four, require (say) four or more. It depends on your problem, too; if your users often enter a single word in a language with long words and pure ASCII (Hawaiian?) it's more error-prone than if non-base64 input typically contains spaces, punctuation, etc. – tripleee Nov 22 '12 at 21:43
  • @Didier Ghys it seems this base64 encoded string `IHRlc3QgbWVzc2FnZQoK` does not much the regex. Or maybe it is not base64 encoded ? though base64_decode('IHRlc3QgbWVzc2FnZQoK') outputs write string - `test message` – dav Dec 24 '12 at 08:32
  • 73
    This only tell that an input **could have been** a b64 encoded value, but it does not tell whether or not the input **is** actually a b64 encoded value. In other words, `abcd` will match, but it is not necessarily represent the encoded value of `i·` rather just a plain `abcd` input – Tzury Bar Yochay May 19 '13 at 10:10
  • 5
    Your regexp is incorrect, since it does not match the empty string, with is the base64 encoding of zero-length binary data according to RFC 4648. – reddish Feb 19 '17 at 16:53
  • This String : 2f885077341cb05943c2aeddc1288ad72934de45442732cff695572f35b04c9340fe6f7a6f95488c9446943da0a54576ffa6a9a87d1f2b48e6292b259d562b2851cb0d1310985b52fc44b6d33a19cc87a496947dcddb8feff366a8e72fe68c7bcdf313b379ad22ab7f94b338f3c8df8e9adffb7bb4650eb278a1c1ef89a86dd44f28924e713fba5eac54ad62eba1a2c14bdd3edc040455156b7930c558b8e66f2124471675968d61d65eae5550a4b1e64d01de849acccba6c16405148174af6ae30c84705dcbb9496ce36530fdd92f866ac0bbcf790f7a22821f1fcf04210ab7b9a971af9ee175aecec502c3ca752f66f09f3dd54f7d4a7c75f763d2150f10ee is failing regex test – a.r. Jun 05 '17 at 09:15
  • To validate use the function preg_match(). The syntax is if(preg_match("/^([A-Za-z0-9+\/]{4})*([A-Za-z0-9+\/]{4}|[A-Za-z0-9+\/]{3}=|[A-Za-z0-9+\/]{2}==)$/", 'abc')=== 1){ echo "base64 encoded
    "; } else { echo "not base 64 encoded" } Also check forward slash that needs to be escaped.
    – bikashphp Jun 20 '17 at 09:30
  • This is incorrect. This will match with the every string which has a length multiple of four. – Mohammed Javad Oct 22 '18 at 06:45
  • Doesn't this match all strings that could be encoded with base64, but not necessarily are? For example, this would return that word "pass" is base64 encoded (which might be, but most likely not). I don't think this answers the question, because by just validating this regex it is not possible to know if it is just a regular string, with a multiple of 4, or a base64 encoded string. – Adomas Nov 19 '18 at 23:44
  • 8
    @Adomas, `"pass"` is a perfectly valid base64 string, that decodes into the sequence of bytes `0xa5`, `0xab` and `0x2c`. Why to discard it _a priori_, if you don't have more context to decide? – Luis Colorado Nov 22 '18 at 08:57
  • 2
    @LuisColorado, but the question here is "how to check if the string was base64 encoded", the answer states that the provided regex validates if the string is base64 encoded - but that's not true, provided regex just validates if a string is in base64 encoded string format, but there's no way of knowing if this is an original string, just in a base64 format, or an encoded string. – Adomas Nov 22 '18 at 10:18
  • 1
    @Adomas, well, then the answer to it is impossible, because for that to be answered you need to know the history of how the string was composed. How do you distinguish that `The book is not good` is a valid encoding for the binary sequence `4e 17 9b a2 89 22 b2 7a 2d 82 8a 1d` or not? What if you want to transmit such a sequence, and the validation software doesn't permit it because it has some meaning in english? – Luis Colorado Nov 22 '18 at 10:29
  • 1
    @LuisColorado That's the point. I would say this should be the correct answer, that to know if string is base64 encoded or not is impossible. – Adomas Nov 22 '18 at 12:23
  • Well, I'm pragmatic, so I don't try to solve impossibles. Better try to guess if there's some way to work the problem around. In this case, as I understand, the problem is to identify if a string _can be_ the product of a Base64 encoding. As such, not all strings are this form. For example `pass` is, but `passs` isn't. I think this is the reason for the question, and indeed, also neither `passs=`, nor `passs==`, nor `passs===...........=`, are possible candidates, and this makes the problem nontrivial. – Luis Colorado Nov 22 '18 at 12:38
  • @LuisColorado, I understand your point of view, but looking from this questions perspective - it is asked to know if a string is base64 encoded, otherwise an error needs to be thrown. I wasn't that much familiar with how base64 encoded strings look like, so looking at this most voted answer simply thought that validating the regexp would prove if it is base64 encoded or not, but soon realized that this regexp validates only if the string could be a product of base64 encoding, it doesn't confirm if that string was created by encoding something in base64 (turns out that's impossible). – Adomas Nov 22 '18 at 18:31
  • 2
    This is a pretty old question, but the discussion seems to still be on life support! A couple of quick comments: The regex doesn't account for whitespace characters which are frequently found in "valid" base64 strings, especially those that are part of PEM encoded files. Also, there is some context given in the question: there is an assumption that the decoded value will consist of only printable ASCII characters. One could test the string to determine if it is potentially base64 encoded, if so decode, then test the resulting byte array to see if it consists of only printable ASCII characters. – Lampshade Feb 25 '19 at 15:30
  • 1
    This sounds to me like a machine learning problem... Where the Tensor Flow peeps at!? :D – Robert Talada May 06 '19 at 18:05
  • That first regex `^([A-Za-z0-9+/]{4})*([A-Za-z0-9+/]{3}=|[A-Za-z0-9+/]{2}==)?$` will return true for an empty string. It's probably also subject to catastrophic backtracking (https://regular-expressions.mobi/catastrophic.html?wlr=1). – jonschlinkert Oct 22 '20 at 04:54
  • Please remember that padding is optional – Yusef Maali Jul 22 '21 at 15:38
  • In Ruby 2.7, simply decoding the string and hoping the decoding function will throw an error does not work. Base64.decode64 will happily decode the string 'skdjglio$%_', and the return value is "\xB2Gc\x82X\xA8".... – MDickten Mar 23 '23 at 09:01
70

If you are using Java, you can actually use commons-codec library

import org.apache.commons.codec.binary.Base64;

String stringToBeChecked = "...";
boolean isBase64 = Base64.isArrayByteBase64(stringToBeChecked.getBytes());

[UPDATE 1] Deprecation Notice Use instead

Base64.isBase64(value);

   /**
     * Tests a given byte array to see if it contains only valid characters within the Base64 alphabet. Currently the
     * method treats whitespace as valid.
     *
     * @param arrayOctet
     *            byte array to test
     * @return {@code true} if all bytes are valid characters in the Base64 alphabet or if the byte array is empty;
     *         {@code false}, otherwise
     * @deprecated 1.5 Use {@link #isBase64(byte[])}, will be removed in 2.0.
     */
    @Deprecated
    public static boolean isArrayByteBase64(final byte[] arrayOctet) {
        return isBase64(arrayOctet);
    }
shareef
  • 9,255
  • 13
  • 58
  • 89
zihaoyu
  • 5,483
  • 11
  • 42
  • 46
  • 22
    from the documentation: `isArrayByteBase64(byte[] arrayOctet)` Deprecated. 1.5 Use `isBase64(byte[])`, will be removed in 2.0. – Avinash R Dec 05 '13 at 04:32
  • 8
    You can use also Base64.isBase64(String base64) instead of converting it to byte array yourself. – Saša Feb 27 '14 at 12:00
  • 5
    Sadly, based on documentation: http://commons.apache.org/proper/commons-codec/apidocs/org/apache/commons/codec/binary/Base64.html#isBase64%28java.lang.String%29 : "Tests a given String to see if it contains only valid characters within the Base64 alphabet. Currently the method treats whitespace as valid." This means that this methods has some false positives like "whitespace" or numbers ("0", "1"). – Christian Vielma Feb 02 '15 at 19:23
  • for string Base64.isBase64(content) – ema Feb 25 '16 at 17:17
  • 9
    This answer is wrong because given `stringToBeChecked="some plain text"` then it sets `boolean isBase64=true` even though it's not a Base64 encoded value. Read the source for commons-codec-1.4 `Base64.isArrayByteBase64()` it only checks that each character in the string is valid to be considered for Base64 encoding and allows white space. – Brad Apr 27 '17 at 11:30
  • i am expecting false for plain string 'politicalstudent' and the method returns true with above function – Ajay Aug 04 '18 at 11:29
  • 1
    @Ajay, `politicalstudent` is a valid base64 string, it decodes into the sequence: `a6 89 62 b6 27 1a 96 cb 6e 75 e9 ed` – Luis Colorado Nov 22 '18 at 09:01
  • 1
    The problem is that `isBase64("a")` returns `true`. This is a bad test. It only makes sure that all characters are in `[A-Za-z0-9+/=]` – Shloim Apr 21 '19 at 13:56
58

Well you can:

  • Check that the length is a multiple of 4 characters
  • Check that every character is in the set A-Z, a-z, 0-9, +, / except for padding at the end which is 0, 1 or 2 '=' characters

If you're expecting that it will be base64, then you can probably just use whatever library is available on your platform to try to decode it to a byte array, throwing an exception if it's not valid base 64. That depends on your platform, of course.

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
  • Parsing differs from validation at least by the fact that it require memory for decoded byte array. So this is not the most effective approach in some cases. – Victor Yarema Sep 22 '19 at 12:36
  • 3
    @VictorYarema: I suggested both a validation-only approach (bullet points) and also a parsing approach (after the bullet points). – Jon Skeet Sep 22 '19 at 17:58
  • 1
    This gets to be real fun when you're trying to detect base64 encoding in raw email. MIME headers are _occassionaly_ a strange mix of quoted-printable, non-ascii, non-utf8, _sort of_ base64. e.g. `=?windows-874?B?M0JCIGUtQmlsbCCgIEEvQyBOby4gNDEwMDQ1Nzg3IOC01825IDAzLzIwMjIgSU5WOjM2NTAzMjYwMzAwNjky?=` off the shelf email clients handle all this, without blinking, but it's a mess. – ocodo May 28 '23 at 10:01
36

As of Java 8, you can simply use java.util.Base64 to try and decode the string:

String someString = "...";
Base64.Decoder decoder = Base64.getDecoder();

try {
    decoder.decode(someString);
} catch(IllegalArgumentException iae) {
    // That string wasn't valid.
}
Philippe
  • 9,582
  • 4
  • 39
  • 59
  • 4
    yes, it's an option, but don't forget that catch is quite expensive operation in Java – panser Dec 28 '17 at 22:45
  • 14
    That is not the case anymore. Exception handling is performing pretty good. You better not forget that Java Regex is pretty slow. I mean: REALLY SLOW! It's actually faster to decode a Base64 and check that it is (not) working instead of matching the String with the above Regex. I did a rough test and Java Regex matching is around six times slower (!!) than catching an eventual exception on the decode. – Sven Döring Jun 12 '19 at 10:57
  • With more test runs it is actually eleven times slower. It is time for a better Regex implementation in Java. Even a Regex check with the Nashorn JavaScript engine in Java is so much faster. Unbelievable. Additionally JavaScript Regex (with Nashorn) is so much more powerful. – Sven Döring Jun 12 '19 at 11:14
  • 8
    With Java 11 (instead of Java 8) the Regex check is even 22 times slower. (Because the Base64 decoding got faster.) – Sven Döring Jun 12 '19 at 11:42
  • 3
    Using this approach with string "Commit" will return as a valid value that is just gibberish. So it doesn't seem to be fool proof. – Alain P Jul 28 '21 at 15:47
  • it is wrong... decoder.decode("dev"); don't make exception... – seunggabi Dec 07 '21 at 04:50
  • 2
    @seunggabi why would it throw on the string `"dev"`? – Philippe Dec 22 '21 at 13:26
  • 1
    @Philippe it doesn't throw exception if String 1st letter is capital for example string is "Shubhamwaghmare" For this string this lib not throws exception. So, it doesn't seem to be fool proof solution. – Mangesh Mandavgane May 03 '22 at 05:45
  • @Philippe The sample code only throws an exception, if the string contains non-base64 characters. Try "123" and you will get "[B@5e2de80c" – Frank Feb 17 '23 at 18:50
15

Try like this for PHP5

//where $json is some data that can be base64 encoded
$json=some_data;

//this will check whether data is base64 encoded or not
if (base64_decode($json, true) == true)
{          
   echo "base64 encoded";          
}
else 
{
   echo "not base64 encoded"; 
}

Use this for PHP7

 //$string parameter can be base64 encoded or not

function is_base64_encoded($string){
 //this will check if $string is base64 encoded and return true, if it is.
 if (base64_decode($string, true) !== false){          
   return true;        
 }else{
   return false;
 }
}
Suneel Kumar
  • 5,621
  • 3
  • 31
  • 44
  • 2
    Which language is this? The question was asked without referring to a language – Ozkan Nov 27 '14 at 10:53
  • this will not work. read the docs `Returns FALSE if input contains character from outside the base64 alphabet.` [base64_decode](http://php.net/manual/en/function.base64-decode.php) – Aley Apr 30 '16 at 21:29
  • 2
    How? if input contains outside character then it is not base64, right? – Suneel Kumar Feb 01 '17 at 13:16
9
var base64Rejex = /^(?:[A-Z0-9+\/]{4})*(?:[A-Z0-9+\/]{2}==|[A-Z0-9+\/]{3}=|[A-Z0-9+\/]{4})$/i;
var isBase64Valid = base64Rejex.test(base64Data); // base64Data is the base64 string

if (isBase64Valid) {
    // true if base64 formate
    console.log('It is base64');
} else {
    // false if not in base64 formate
    console.log('it is not in base64');
}
Alex K
  • 8,269
  • 9
  • 39
  • 57
Deepak Sisodiya
  • 850
  • 9
  • 11
7

Try this:

public void checkForEncode(String string) {
    String pattern = "^([A-Za-z0-9+/]{4})*([A-Za-z0-9+/]{4}|[A-Za-z0-9+/]{3}=|[A-Za-z0-9+/]{2}==)$";
    Pattern r = Pattern.compile(pattern);
    Matcher m = r.matcher(string);
    if (m.find()) {
        System.out.println("true");
    } else {
        System.out.println("false");
    }
}
arghtype
  • 4,376
  • 11
  • 45
  • 60
user5499458
  • 71
  • 1
  • 1
7

It is impossible to check if a string is base64 encoded or not. It is only possible to validate if that string is of a base64 encoded string format, which would mean that it could be a string produced by base64 encoding (to check that, string could be validated against a regexp or a library could be used, many other answers to this question provide good ways to check this, so I won't go into details).

For example, string flow is a valid base64 encoded string. But it is impossible to know if it is just a simple string, an English word flow, or is it base 64 encoded string ~Z0

Adomas
  • 466
  • 1
  • 8
  • 21
5

There are many variants of Base64, so consider just determining if your string resembles the varient you expect to handle. As such, you may need to adjust the regex below with respect to the index and padding characters (i.e. +, /, =).

class String
  def resembles_base64?
    self.length % 4 == 0 && self =~ /^[A-Za-z0-9+\/=]+\Z/
  end
end

Usage:

raise 'the string does not resemble Base64' unless my_string.resembles_base64?
user664833
  • 18,397
  • 19
  • 91
  • 140
5

Check to see IF the string's length is a multiple of 4. Aftwerwards use this regex to make sure all characters in the string are base64 characters.

\A[a-zA-Z\d\/+]+={,2}\z

If the library you use adds a newline as a way of observing the 76 max chars per line rule, replace them with empty strings.

Yaw Boakye
  • 10,352
  • 1
  • 17
  • 25
  • The link mentioned shows 404. Please check and update. – Ankur Jul 09 '14 at 10:16
  • Sorry @AnkurKumar but that's what happen when people have uncool URLs: they change all the time. I have no idea where it's moved to. I hope you find other useful resources through Google – Yaw Boakye Jul 13 '14 at 02:41
  • You can always get old pages from web.archive.org - here's the original url. http://web.archive.org/web/20120919035911/http://www.ict.griffith.edu.au/anthony/info/crypto/base64.hints or I posted the text here: https://gist.github.com/mika76/d09e2b65159e435e7a4cc5b0299c3e84 – Mladen Mihajlovic Jun 16 '18 at 11:52
2
/^([A-Za-z0-9+\/]{4})*([A-Za-z0-9+\/]{4}|[A-Za-z0-9+\/]{3}=|[A-Za-z0-9+\/]{2}==)$/

this regular expression helped me identify the base64 in my application in rails, I only had one problem, it is that it recognizes the string "errorDescripcion", I generate an error, to solve it just validate the length of a string.

mayo
  • 3,845
  • 1
  • 32
  • 42
Onironauta
  • 21
  • 1
2

For Flutter, I tested couple of the above comments and translated that into dart function as follows

  static bool isBase64(dynamic value) {

    if (value.runtimeType == String){
      
      final RegExp rx = RegExp(r'^([A-Za-z0-9+/]{4})*([A-Za-z0-9+/]{3}=|[A-Za-z0-9+/]{2}==)?$',
          multiLine: true,
          unicode: true,
      );

      final bool isBase64Valid = rx.hasMatch(value);

      if (isBase64Valid == true) {return true;}
      else {return false;}

    }

    else {return false;}

  }
Rageh Azzazy
  • 701
  • 1
  • 7
  • 16
2

In Java below code worked for me:

public static boolean isBase64Encoded(String s) {
        String pattern = "^([A-Za-z0-9+/]{4})*([A-Za-z0-9+/]{3}=|[A-Za-z0-9+/]{2}==)?$";
        Pattern r = Pattern.compile(pattern);
        Matcher m = r.matcher(s);
        return m.find();
    }
Shiv Buyya
  • 3,770
  • 2
  • 30
  • 25
1

C# This is performing great:

static readonly Regex _base64RegexPattern = new Regex(BASE64_REGEX_STRING, RegexOptions.Compiled);

private const String BASE64_REGEX_STRING = @"^[a-zA-Z0-9\+/]*={0,3}$";

private static bool IsBase64(this String base64String)
{
    var rs = (!string.IsNullOrEmpty(base64String) && !string.IsNullOrWhiteSpace(base64String) && base64String.Length != 0 && base64String.Length % 4 == 0 && !base64String.Contains(" ") && !base64String.Contains("\t") && !base64String.Contains("\r") && !base64String.Contains("\n")) && (base64String.Length % 4 == 0 && _base64RegexPattern.Match(base64String, 0).Success);
    return rs;
}
mayo
  • 3,845
  • 1
  • 32
  • 42
Veni Souto
  • 47
  • 3
1

This works in Python:

import base64

def IsBase64(str):
    try:
        base64.b64decode(str)
        return True
    except Exception as e:
        return False

if IsBase64("ABC"):
    print("ABC is Base64-encoded and its result after decoding is: " + str(base64.b64decode("ABC")).replace("b'", "").replace("'", ""))
else:
    print("ABC is NOT Base64-encoded.")

if IsBase64("QUJD"):
    print("QUJD is Base64-encoded and its result after decoding is: " + str(base64.b64decode("QUJD")).replace("b'", "").replace("'", ""))
else:
    print("QUJD is NOT Base64-encoded.")

Summary: IsBase64("string here") returns true if string here is Base64-encoded, and it returns false if string here was NOT Base64-encoded.

gave
  • 181
  • 13
0

This snippet may be useful when you know the length of the original content (e.g. a checksum). It checks that encoded form has the correct length.

public static boolean isValidBase64( final int initialLength, final String string ) {
  final int padding ;
  final String regexEnd ;
  switch( ( initialLength ) % 3 ) {
    case 1 :
      padding = 2 ;
      regexEnd = "==" ;
      break ;
    case 2 :
      padding = 1 ;
      regexEnd = "=" ;
      break ;
    default :
      padding = 0 ;
      regexEnd = "" ;
  }
  final int encodedLength = ( ( ( initialLength / 3 ) + ( padding > 0 ? 1 : 0 ) ) * 4 ) ;
  final String regex = "[a-zA-Z0-9/\\+]{" + ( encodedLength - padding ) + "}" + regexEnd ;
  return Pattern.compile( regex ).matcher( string ).matches() ;
}
Laurent Caillette
  • 1,281
  • 2
  • 14
  • 20
0

If the RegEx does not work and you know the format style of the original string, you can reverse the logic, by regexing for this format.

For example I work with base64 encoded xml files and just check if the file contains valid xml markup. If it does not I can assume, that it's base64 decoded. This is not very dynamic but works fine for my small application.

Jankapunkt
  • 8,128
  • 4
  • 30
  • 59
0

This works in Python:

def is_base64(string):
    if len(string) % 4 == 0 and re.test('^[A-Za-z0-9+\/=]+\Z', string):
        return(True)
    else:
        return(False)
bcarroll
  • 1,727
  • 16
  • 14
0

Try this using a previously mentioned regex:

String regex = "^([A-Za-z0-9+/]{4})*([A-Za-z0-9+/]{4}|[A-Za-z0-9+/]{3}=|[A-Za-z0-9+/]{2}==)$";
if("TXkgdGVzdCBzdHJpbmc/".matches(regex)){
    System.out.println("it's a Base64");
}

...We can also make a simple validation like, if it has spaces it cannot be Base64:

String myString = "Hello World";
 if(myString.contains(" ")){
   System.out.println("Not B64");
 }else{
    System.out.println("Could be B64 encoded, since it has no spaces");
 }
Marco
  • 2,445
  • 2
  • 25
  • 15
0

if when decoding we get a string with ASCII characters, then the string was not encoded

(RoR) ruby solution:

def encoded?(str)
  Base64.decode64(str.downcase).scan(/[^[:ascii:]]/).count.zero?
end

def decoded?(str)
  Base64.decode64(str.downcase).scan(/[^[:ascii:]]/).count > 0
end
0
Function Check_If_Base64(ByVal msgFile As String) As Boolean
Dim I As Long
Dim Buffer As String
Dim Car As String

Check_If_Base64 = True

Buffer = Leggi_File(msgFile)
Buffer = Replace(Buffer, vbCrLf, "")
For I = 1 To Len(Buffer)
    Car = Mid(Buffer, I, 1)
    If (Car < "A" Or Car > "Z") _
    And (Car < "a" Or Car > "z") _
    And (Car < "0" Or Car > "9") _
    And (Car <> "+" And Car <> "/" And Car <> "=") Then
        Check_If_Base64 = False
        Exit For
    End If
Next I
End Function
Function Leggi_File(PathAndFileName As String) As String
Dim FF As Integer
FF = FreeFile()
Open PathAndFileName For Binary As #FF
Leggi_File = Input(LOF(FF), #FF)
Close #FF
End Function
0
import java.util.Base64;

    public static String encodeBase64(String s) {
        return Base64.getEncoder().encodeToString(s.getBytes());
    }

    public static String decodeBase64(String s) {
        try {
            if (isBase64(s)) {
                return new String(Base64.getDecoder().decode(s));
            } else {
                return s;
            }
        } catch (Exception e) {
            return s;
        }
    }

    public static boolean isBase64(String s) {
        String pattern = "^([A-Za-z0-9+/]{4})*([A-Za-z0-9+/]{4}|[A-Za-z0-9+/]{3}=|[A-Za-z0-9+/]{2}==)$";
        Pattern r = Pattern.compile(pattern);
        Matcher m = r.matcher(s);

        return m.find();
    }
seunggabi
  • 1,699
  • 12
  • 12
0

For Java flavour I actually use the following regex:

"([A-Za-z0-9+]{4})*([A-Za-z0-9+]{3}=|[A-Za-z0-9+]{2}(==){0,2})?"

This also have the == as optional in some cases.

Best!

Antonio Saco
  • 1,620
  • 12
  • 21
0

There is no way to distinct string and base64 encoded, except the string in your system has some specific limitation or identification.

pinxue
  • 1,736
  • 12
  • 17
-2

I try to use this, yes this one it's working

^([A-Za-z0-9+/]{4})*([A-Za-z0-9+/]{3}=|[A-Za-z0-9+/]{2}==)?$

but I added on the condition to check at least the end of the character is =

string.lastIndexOf("=") >= 0
  • *Why* check for `=`: What specification of `Base64` are you using? What does `end of the character` mean, and how does non-negative `lastIndexOf()` check that? – greybeard Apr 23 '20 at 07:15
  • mostly the return of my `base64` character always has `=` at the end – Ashadi Sedana Pratama Oct 08 '20 at 01:34
  • 1
    Not all base 64 encoded strings end with =, for example: rYNltxhaxFAdr3ex8JFFtyCWHNRLCKyPyYei3xo05yHJEXmh3GZQxWm0NSP3tWBkMoIqrHQibfQmYpw-i6TspDJ0M3A1Z1FRWU1wM3V3aGZ1eTViOGJk – Michael Draper Oct 12 '20 at 01:00