gpt4 book ai didi

encoding - 使用 Stream_StringToBinary 进行 Base64 编码会插入换行符,从而破坏字符串?

转载 作者:行者123 更新时间:2023-12-01 15:14:27 25 4
gpt4 key购买 nike

也许我疯了,但看起来在 VB 中运行 Base64 的著名代码在第 73 个位置插入了一个换行符(ascii 10),这随后使编码字符串对于基本身份验证无效 - 或其他任何与此相关的内容。

原始代码:

Function Stream_StringToBinary(Text)
Const adTypeText = 2
Const adTypeBinary = 1

'Create Stream object
Dim BinaryStream 'As New Stream
Set BinaryStream = CreateObject("ADODB.Stream")

'Specify stream type - we want To save text/string data.
BinaryStream.Type = adTypeText

'Specify charset For the source text (unicode) data.
BinaryStream.CharSet = "us-ascii"

'Open the stream And write text/string data To the object
BinaryStream.Open
BinaryStream.WriteText Text

'Change stream type To binary
BinaryStream.Position = 0
BinaryStream.Type = adTypeBinary

'Ignore first two bytes - sign of
BinaryStream.Position = 0

'Open the stream And get binary data from the object
Stream_StringToBinary = BinaryStream.Read

Set BinaryStream = Nothing
End Function


Function Base64Encode(sText)
Dim oXML, oNode

Set oXML = CreateObject("Msxml2.DOMDocument.3.0")
Set oNode = oXML.CreateElement("base64")
oNode.dataType = "bin.base64"
oNode.nodeTypedValue =Stream_StringToBinary(sText)
Base64Encode = oNode.text
Set oNode = Nothing
Set oXML = Nothing
End Function

'------------------- and here goes the encoding -----------------------
strEnc = Base64Encode( "AVERYLONGUSERNAMEHELLOTHE123:AVERYLONGPASSWORDWHYAREYOUSOLONGREALLYANNOY123")
'----------------------------------------------------------------------

结果:

QVZFUllMT05HVVNFUk5BTUVIRUxMT1RIRTEyMzpBVkVSWUxPTkdQQVNTV09SRFdIWUFSRVlP
VVNPTE9OR1JFQUxMWUFOTk9ZMTIz

看起来这发生在很长的 UID/PWD 对上。

有没有人遇到过这个?

最佳答案

这是因为 Base64 编码如何处理长字符串。

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.



因为它添加了 vbLf ( Chr(10) ) 编码后应该意味着您可以安全地使用
strEnc = Replace(strEnc, vbLf, "")

某些语言具有“无换行”参数,可以传递该参数以阻止在第 76 个字符后添加换行符,但我不知道 Microsoft XMLDOM 实现中的一个,请在此处注明 Base64 -- do we really want/need line breaks every 76 characters?看起来好像有人建议过,但没有证据表明它曾经被实现过。

关于encoding - 使用 Stream_StringToBinary 进行 Base64 编码会插入换行符,从而破坏字符串?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41837881/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com