Check if a string is encoded in base64 using Python

前端 未结 10 2046
离开以前
离开以前 2021-02-05 01:56

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

相关标签:
10条回答
  • 2021-02-05 02:15
    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
    

    In my case, my input, s, had newlines which I had to strip before the comparison.

    0 讨论(0)
  • 2021-02-05 02:16

    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
    
    0 讨论(0)
  • 2021-02-05 02:18
    x = 'possibly base64 encoded string'
    result = x
    try:
       decoded = x.decode('base64', 'strict')
       if x == decoded.encode('base64').strip():
           result = decoded
    except:
       pass
    

    this code put in the result variable decoded string if x is really encoded, and just x if not. Just try to decode doesn't always work.

    0 讨论(0)
  • 2021-02-05 02:21

    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.

    0 讨论(0)
  • 2021-02-05 02:22

    if the length of the encoded string is the times of 4, it can be decoded

    base64.encodestring("whatever you say").strip().__len__() % 4 == 0
    

    so, you just need to check if the string can match something like above, then it won't throw any exception(I Guess =.=)

    if len(the_base64string.strip()) % 4 == 0:
        # then you can just decode it anyway
        base64.decodestring(the_base64string)
    
    0 讨论(0)
  • 2021-02-05 02:24

    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")
    
    0 讨论(0)
提交回复
热议问题