有没有一种好的方法可以使用Python检查一个字符串是否是用base64
编码的?
我曾经遇到和你一样的问题,后来一个非常简单的解决方法突然在我脑海中浮现。你所需要做的就是解码,然后重新编码。如果重新编码后的字符串与原来的编码字符串相等,那么它就是base64编码。
以下是代码:
import base64
def isBase64(s):
try:
return base64.b64encode(base64.b64decode(s)) == s
except Exception:
return False
就是这样!
编辑:这是该函数的版本,可适用于Python 3中的字符串和字节对象:
import base64
def isBase64(sb):
try:
if isinstance(sb, str):
# If there's any unicode here, an exception will be thrown and the function will return false
sb_bytes = bytes(sb, 'ascii')
elif isinstance(sb, bytes):
sb_bytes = sb
else:
raise ValueError("Argument must be string or bytes")
return base64.b64encode(base64.b64decode(sb_bytes)) == sb_bytes
except Exception:
return False
import base64
import binascii
try:
base64.decodestring("foo")
except binascii.Error:
print "no correct base64"
s
是有效的,base64.b64decode(s, validate=true)
会解码它,否则会引发异常。而base64.decodestring
非常宽容,会删除任何不是Base64字符的内容,这有潜在问题。 - Julian这是不可能的。你最好的做法是验证一个字符串是否可能是有效的Base 64,尽管很多只包含ASCII文本的字符串在解码时可以像Base 64一样工作。
import base64, binascii
try:
image_data = base64.b64decode(my_image_string, validate=True)
except binascii.Error:
image_data = my_image_string
使用Python正则表达式
import re
txt = "VGhpcyBpcyBlbmNvZGVkIHRleHQ="
x = re.search("^([A-Za-z0-9+/]{4})*([A-Za-z0-9+/]{3}=|[A-Za-z0-9+/]{2}==)?$", txt)
if (x):
print("Encoded")
else:
print("Non encoded")
RE_BASE64 = "^([A-Za-z0-9+/]{4})*([A-Za-z0-9+/]{3}=|[A-Za-z0-9+/]{2}==)?$"
def likeBase64(s:str) -> bool:
return False if s is None or not re.search(RE_BASE64, s) else True
@geoffspear是正确的,这不是100%可能的,但您可以通过检查字符串头部是否与base64编码字符串匹配(参见:如何检查字符串是否已经进行了base64编码)来接近实现。
# check if a string is base64 encoded.
def isBase64Encoded(s):
pattern = re.compile("^([A-Za-z0-9+/]{4})*([A-Za-z0-9+/]{4}|[A-Za-z0-9+/]{3}=|[A-Za-z0-9+/]{2}==)$")
if not s or len(s) < 1:
return False
else:
return pattern.match(s)
请注意,我的情况是如果字符串为空,则希望返回false,以避免解码,因为解码空字符串没有用处。
base64.encodestring("whatever you say").strip().__len__() % 4 == 0
所以,你只需要检查字符串是否能够匹配上面的某些内容,那么它就不会抛出任何异常(我猜 =.=)
if len(the_base64string.strip()) % 4 == 0:
# then you can just decode it anyway
base64.decodestring(the_base64string)
我知道我晚了近8年,但你可以使用正则表达式,这样你就可以验证给定的输入是否为BASE64。
import re
encoding_type = 'Encoding type: '
base64_encoding = 'Base64'
def is_base64():
element = input("Enter encoded element: ")
expression = "^([A-Za-z0-9+/]{4})*([A-Za-z0-9+/]{3}=|[A-Za-z0-9+/]{2}==)?$"
matches = re.match(expression, element)
if matches:
print(f"{encoding_type + base64_encoding}")
else:
print("Unknown encoding type.")
is_base64()
def is_base64(s):
s = ''.join([s.strip() for s in s.split("\n")])
try:
enc = base64.b64encode(base64.b64decode(s)).strip()
return enc == s
except TypeError:
return False
s
包含换行符,我必须在比较之前将其去除。
return base64.b64encode(base64.b64decode(s)) == s
,而不是使用if语句并返回一个常量布尔结果 :) - d0nutstr
和bytes
无法自动隐式转换类型以进行比较,所以我不得不使用return base64.b64encode(base64.b64decode(s)).decode() == s
来使其工作。因为我的s
是Unicode字符串,而从base64.b64encode(base64.b64decode(s))
返回的值是bytes
类型。请参见:https://dev59.com/G10a5IYBdhLWcg3wNWbA。 - Vikas Prasad