Can a base64 encoded string contain whitespace?

前端 未结 6 728
逝去的感伤
逝去的感伤 2020-12-10 23:48

Might a base64 encoded string contain whitespace? Specifically, could it contain whitespace at the end of the string?

PS. I\'m thinking about the whole \"My

6条回答
  •  鱼传尺愫
    2020-12-11 00:25

    It shouldn't, but it might do.

    A valid base64 string should not contain whitespace since the encoding alphabet should only consist of A-Z a-z 0-9 + /

    However, if the encoded data happens to contain a '+' character, and the data is passed in a URL, it can be unintentionally converted into a space. So you may come across a supposed base64 string that appears to have spaces in it under these circumstances.

    If this is the case, simply replace spaces with pluses before decoding.

    PS. I'm thinking about the whole "MySQL will trim trailing whitespace when storing strings in VARCHAR fields" here

    As an aside, the trailing whitespaces of a varchar won't be casually stripped as of MySQL 5.0.3

提交回复
热议问题