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 the string was base64 enocoded?
问题:
回答1:
You can use the following regular expression to check if a string is base64 encoded or not:
^([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}==
.
回答2:
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());
回答3:
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.
回答4:
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\";
}
回答5:
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.
}
回答6:
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?
回答7:
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.
回答8:
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\');
}
回答9:
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;
}
回答10:
/^([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 long of string.
回答11:
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() ;
}
回答12:
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.
回答13:
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\");
}
}
回答14:
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)
回答15:
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\");
}
回答16:
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.
回答17:
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
回答18:
There is no way to distinct string and base64 encoded, except the string in your system has some specific limitation or identification.