Check if a string is encoded in base64 using Python

前端 未结 10 2048
离开以前
离开以前 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:27

    I know I'm almost 8 years late but you can use a regex expression thus you can verify if a given input is 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()
    

提交回复
热议问题