Check if a string is encoded in base64 using Pytho

2020-05-25 06:55发布

Is there a good way to check if a string is encoded in base64 using Python?

标签: python base64
9条回答
仙女界的扛把子
2楼-- · 2020-05-25 07:19

I was looking for a solution to the same problem, then a very simple one just struck me in the head. All you need to do is decode, then re-encode. If the re-encoded string is equal to the encoded string, then it is base64 encoded.
Here is the code:

import base64

def isBase64(s):
    try:
        return base64.b64encode(base64.b64decode(s)) == s
    except Exception:
        return False

That's it!

Edit: Here's a version of the function that works with both the string and bytes objects in 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
查看更多
兄弟一词,经得起流年.
3楼-- · 2020-05-25 07:20

Using Python RegEx

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")
查看更多
做自己的国王
4楼-- · 2020-05-25 07:25

This isn't possible. The best you could do would be to verify that a string might be valid Base 64, although many strings consisting of only ASCII text can be decoded as if they were Base 64.

查看更多
The star\"
5楼-- · 2020-05-25 07:27

@geoffspear is correct in that this is not 100% possible but you can get pretty close by checking the string header to see if it matches that of a base64 encoded string (re: How to check whether a string is base64 encoded or not).

# 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)

Also not that in my case I wanted to return false if the string is empty to avoid decoding as there's no use in decoding nothing.

查看更多
Summer. ? 凉城
6楼-- · 2020-05-25 07:31

The solution I used is based on one of the prior answers, but uses more up to date calls.

In my code, the my_image_string is either the image data itself in raw form or it's a base64 string. If the decode fails, then I assume it's raw data.

Note the validate=True keyword argument to b64decode. This is required in order for the assert to be generated by the decoder. Without it there will be no complaints about an illegal string.

import base64, binascii

try:
    image_data = base64.b64decode(my_image_string, validate=True)
except binascii.Error:
    image_data = my_image_string
查看更多
可以哭但决不认输i
7楼-- · 2020-05-25 07:35
import base64
import binascii

try:
    base64.decodestring("foo")
except binascii.Error:
    print "no correct base64"
查看更多
登录 后发表回答