我希望解码一个Base64编码的字符串,并将其存储在我的数据库中。如果输入不是Base64编码,我需要抛出错误。
如何检查一个字符串是否为Base64编码?
我希望解码一个Base64编码的字符串,并将其存储在我的数据库中。如果输入不是Base64编码,我需要抛出错误。
如何检查一个字符串是否为Base64编码?
^([A-Za-z0-9+/]{4})*([A-Za-z0-9+/]{3}=|[A-Za-z0-9+/]{2}==)?$
在Base64编码中,字符集为[A-Z, a-z, 0-9, + /]
。如果剩余长度小于4,则字符串用'='
字符进行填充。
^([A-Za-z0-9+/]{4})*
表示字符串以0个或多个Base64组开头。
([A-Za-z0-9+/]{4}|[A-Za-z0-9+/]{3}=|[A-Za-z0-9+/]{2}==)$
表示字符串以三种形式之一结尾:[A-Za-z0-9+/]{4}
、[A-Za-z0-9+/]{3}=
或[A-Za-z0-9+/]{2}==
。
abcd
会匹配,但并不一定代表编码了i·
的结果,而只是一个普通的abcd
输入。 - Tzury Bar Yochayimport org.apache.commons.codec.binary.Base64;
String stringToBeChecked = "...";
boolean isBase64 = Base64.isArrayByteBase64(stringToBeChecked.getBytes());
[更新 1] 废弃通知 请改用
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);
}
isArrayByteBase64(byte[] arrayOctet)
已弃用。
1.5版本后请使用isBase64(byte[])
,2.0版本将删除。 - Avinash RstringToBeChecked="some plain text"
时,它设置了 boolean isBase64=true
,即使这不是一个Base64编码的值。请阅读commons-codec-1.4的源码Base64.isArrayByteBase64()
,它仅检查字符串中的每个字符是否有效以被视为Base64编码,并允许空格。 - BradisBase64("a")
返回 true
。这是一个错误的测试,它只是确保所有字符都在 [A-Za-z0-9+/=]
中。 - Shloim你可以这样做:
如果你期望它是base64编码,那么你可以尝试使用你所在平台上可用的库将其解码为一个字节数组,如果不是有效的base 64,则抛出异常。当然,这取决于你所在的平台。
=?windows-874?B?M0JCIGUtQmlsbCCgIEEvQyBOby4gNDEwMDQ1Nzg3IOC01825IDAzLzIwMjIgSU5WOjM2NTAzMjYwMzAwNjky?=
;现成的电子邮件客户端可以毫不费力地处理所有这些,但这真的很混乱。 - ocodo从Java 8开始,您可以简单地使用java.util.Base64来尝试解码字符串:
String someString = "...";
Base64.Decoder decoder = Base64.getDecoder();
try {
decoder.decode(someString);
} catch(IllegalArgumentException iae) {
// That string wasn't valid.
}
// 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";
}
// $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.
return base64_decode($string, true) !== false;
}
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');
}
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");
}
}
检查字符串长度是否是4的倍数。之后使用此正则表达式确保字符串中的所有字符都是base64字符。
\A[a-zA-Z\d\/+]+={,2}\z
如果您使用的库将换行符添加为遵守每行最大76个字符规则的方法,请将它们替换为空字符串。
Base64有许多变体,因此您需要确定您的字符串是否类似于您要处理的变体。因此,您可能需要根据索引和填充字符(即+
,/
,=
)调整下面的正则表达式。
class String
def resembles_base64?
self.length % 4 == 0 && self =~ /^[A-Za-z0-9+\/=]+\Z/
end
end
使用方法:
raise 'the string does not resemble Base64' unless my_string.resembles_base64?
test1234
是一个有效的Base64编码字符串,解码后会得到一些字节。没有应用程序独立的方法可以得出test1234
不是Base64编码字符串的结论。 - Kinjal Dixit