0

I'm getting unexpected results using vba to encode a string in base64. For strings shorter than 55 characters - things work fine. But for strings longer than 55 characters (and for each 55 characters next) a newline character gets inserted in the encoded string.

Function EncodeBase64(text As String) As String
  Dim arrData() As Byte
  arrData = StrConv(text, vbFromUnicode)

  Dim objXML As MSXML2.DOMDocument60
  Dim objNode As MSXML2.IXMLDOMElement

  Set objXML = New MSXML2.DOMDocument60
  Set objNode = objXML.createElement("b64")

  objNode.DataType = "bin.base64"
  objNode.nodeTypedValue = arrData
  EncodeBase64 = objNode.text

  Set objNode = Nothing
  Set objXML = Nothing
End Function

Sub testEncode64()
    Dim ClientSecret As String, encodedStr As String, tens As String

    tens = "         1         2         3         4         5         6         7         8         9         10        11        12"
    Debug.Print "Begin"
    
    ClientSecret = "a23456789012345678901234567890123456789012345678901234"
    encodedStr = EncodeBase64(ClientSecret)
    Debug.Print tens
    Debug.Print ClientSecret
    Debug.Print encodedStr
    Debug.Print
    
    ClientSecret = "b234567890123456789012345678901234567890123456789012345"
    encodedStr = EncodeBase64(ClientSecret)
    Debug.Print tens
    Debug.Print ClientSecret
    Debug.Print encodedStr
    Debug.Print
    
    
    ClientSecret = "c23456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678"
    encodedStr = EncodeBase64(ClientSecret)
    Debug.Print tens
    Debug.Print ClientSecret
    Debug.Print encodedStr
    Debug.Print

    ClientSecret = "c234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789"
    encodedStr = EncodeBase64(ClientSecret)
    Debug.Print tens
    Debug.Print ClientSecret
    Debug.Print encodedStr
    Debug.Print
End Sub

Here's the output


Begin
         1         2         3         4         5         6         7         8         9         10        11        12
a23456789012345678901234567890123456789012345678901234
YTIzNDU2Nzg5MDEyMzQ1Njc4OTAxMjM0NTY3ODkwMTIzNDU2Nzg5MDEyMzQ1Njc4OTAxMjM0

         1         2         3         4         5         6         7         8         9         10        11        12
b234567890123456789012345678901234567890123456789012345
YjIzNDU2Nzg5MDEyMzQ1Njc4OTAxMjM0NTY3ODkwMTIzNDU2Nzg5MDEyMzQ1Njc4OTAxMjM0
NQ==

         1         2         3         4         5         6         7         8         9         10        11        12
c23456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678
YzIzNDU2Nzg5MDEyMzQ1Njc4OTAxMjM0NTY3ODkwMTIzNDU2Nzg5MDEyMzQ1Njc4OTAxMjM0
NTY3ODkwMTIzNDU2Nzg5MDEyMzQ1Njc4OTAxMjM0NTY3ODkwMTIzNDU2Nzg5MDEyMzQ1Njc4

         1         2         3         4         5         6         7         8         9         10        11        12
c234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789
YzIzNDU2Nzg5MDEyMzQ1Njc4OTAxMjM0NTY3ODkwMTIzNDU2Nzg5MDEyMzQ1Njc4OTAxMjM0
NTY3ODkwMTIzNDU2Nzg5MDEyMzQ1Njc4OTAxMjM0NTY3ODkwMTIzNDU2Nzg5MDEyMzQ1Njc4
OQ==

As can be seen, the 54 char string converts fine. For the 55 character string a newline char gets added. The 108 char string gets 1 newline and the 110 char string gest 2. Any ideas why this extra character is getting added each 55 characters?

Tim McNaughton
  • 170
  • 2
  • 7
  • 1
    That is normal behavior. Often it is at 76 chars but it depends on the manufacturer (programmer). The reason is that base64 encodings are often used to encode binary data as ASCII to be able to send by email. Some older email clients didn't like long lines. – StureS Aug 29 '21 at 11:09
  • So, nl behavior to break long lines up. Yes, the output strings are 72char ling. Interesting - its breaking my api auth. Things work when i strip out that char. Thx! – Tim McNaughton Aug 29 '21 at 19:54
  • 1
    Previously: https://stackoverflow.com/questions/41837881/base64-encode-with-stream-stringtobinary-inserts-a-newline-breaking-the-string – Tim Williams Aug 29 '21 at 23:42

0 Answers0