Base64 encode with Stream_StringToBinary inserts a newline, breaking the string?

后端 未结 1 1745
不知归路
不知归路 2021-01-21 03:19

Maybe I\'m crazy, but it looks like the famous code to run Base64 in VB inserts a newline character (ascii 10) at the 73rd position, which subsequently makes the encoded string

相关标签:
1条回答
  • 2021-01-21 03:56

    This is because of how the Base64 encoding deals with long strings.

    From RFC 2045 - 6.8 Base64 Content-Transfer-Encoding
    The encoded output stream must be represented in lines of no more than 76 characters each. All line breaks or other characters not found in Table 1 must be ignored by decoding software. In base64 data, characters other than those in Table 1, line breaks, and other white space probably indicate a transmission error, about which a warning message or even a message rejection might be appropriate under some circumstances.

    Because it is adding the vbLf (Chr(10)) after the encode should mean you are safe to just remove it using

    strEnc = Replace(strEnc, vbLf, "")
    

    Some languages have a "no wrapping" argument that can be passed to stop the Linefeed being added after the 76th character but I don't know of one in the Microsoft XMLDOM implementation, noted here Base64 -- do we really want/need line breaks every 76 characters? it looks as though it was suggested but there is no evidence it was ever implemented.

    0 讨论(0)
提交回复
热议问题