2

this is my code(python3.7):

base64.b64decode('ThisIsASecret')

the console show error msg 'binascii.Error: Invalid base64-encoded string: number of data characters (13) cannot be 1 more than a multiple of 4'

Then i change the string

base64.b64decode('ThisIs==') OK
base64.b64decode('ThisIsA=') OK
base64.b64decode('ThisI===') ERROR

I find this error was show when the len(string)%4==1 only. example the length of a string is 5,9,13 etc. How can I reolve the string?

LiuYang
  • 21
  • 2
  • 1
    Possible duplicate of [python: Invalid base64-encoded string: number of data characters (5) cannot be 1 more than a multiple of 4](https://stackoverflow.com/questions/55105045/python-invalid-base64-encoded-string-number-of-data-characters-5-cannot-be-1) – Tim Biegeleisen Nov 07 '19 at 02:37
  • I agree with @TimBiegeleisen, it looks like you're calling *decode when you should be calling *encode. The error message actually says this - "Invalid base64-encoded string", you're not passing an encoded string, you're passing a plain text string. – David Waterworth Nov 07 '19 at 02:43
  • @David Waterworth I encountered this problem when parsing the token string generated by Java using JWT. Java is decoded once using Base64 when setting the secret key at parsing time. My python had this error when it got the same token parsed. Java in the secret key transcoding source is like this :TextCodec.BASE64. Decode (BASE64 encodedkeybytes) – LiuYang Nov 07 '19 at 03:22

0 Answers0