RE: Base64 decoding - BUG????

From: Shawn Farkas (shawnfa_at_online.microsoft.com)
Date: 03/03/04

  • Next message: Shawn Farkas: "RE: Cannot change Code Group's Membership Condition to Strong Name"
    Date: Tue, 02 Mar 2004 23:50:39 GMT
    
    

    OK, from looking over your code, the problem is with your usage of the XML Text writer. XmllTextWriter is for use in outputing well formed XML only,
    not for converting text values back and forth. In order to get the WriteBase64 method to work correctly here, you need to use the following code:

       objXMLWriter.WriteStartDocument()
       objXMLWriter.WriteStartElement("Root")
       objXMLWriter.WriteBase64(abytBuffer, 0, abytBuffer.Length)
       objXMLWriter.WriteEndElement()
       objXMLWriter.WriteEndDocument()

    This puts the XML writer in a valid state for a text string, and it will then output the base 64 correctly. However, if you're not interested in putting the
    values in XML, you can use the System.Convert.ToBase64String() method, which will create a string for you directly.

    -Shawn
    http://blogs.msdn.com/shawnfa

    -- 
    This posting is provided "AS IS" with no warranties, and confers no rights.
    Note:  For the benefit of the community-at-large, all responses to this message are best directed to the newsgroup/thread from which they 
    originated.  
    --------------------
    >Thread-Topic: Base64 decoding - BUG????
    >thread-index: AcQAaUkWPSep7bslTU+tFxfRnHniyw==
    >X-Tomcat-NG: microsoft.public.dotnet.security
    >From: "=?Utf-8?B?TWFyayBIYXJyaXM=?=" <mark@netizen.co.uk_REMOVETHISBITTOMAIL>
    >References:  <0AAC685D-C27E-4099-8096-3CB2C178C445@microsoft.com> <Cfe4wb9$DHA.2300@cpmsftngxa06.phx.gbl>
    >Subject: RE: Base64 decoding - BUG????
    >Date: Tue, 2 Mar 2004 07:16:07 -0800
    >Lines: 97
    >Message-ID: <20ADAEBA-D52E-48D6-A172-C5AC30C027D3@microsoft.com>
    >MIME-Version: 1.0
    >Content-Type: text/plain;
    >	charset="Utf-8"
    >Content-Transfer-Encoding: 7bit
    >X-Newsreader: Microsoft CDO for Windows 2000
    >Content-Class: urn:content-classes:message
    >Importance: normal
    >Priority: normal
    >X-MimeOLE: Produced By Microsoft MimeOLE V6.00.3790.0
    >Newsgroups: microsoft.public.dotnet.security
    >Path: cpmsftngxa06.phx.gbl
    >Xref: cpmsftngxa06.phx.gbl microsoft.public.dotnet.security:5209
    >NNTP-Posting-Host: tk2msftcmty1.phx.gbl 10.40.1.180
    >X-Tomcat-NG: microsoft.public.dotnet.security
    >
    >Hi shawn ... thx for the reply, the code is as follows ... not VERY scaled down, but i dont know where the problem might be!! :D
    I have put both my encoder and decoder code here ... basic principle, as mentioned, is that a string get's passed into the function, gets encrypted, 
    then gets encoded (Base64? / BinHex) so it can be sent as a response by a Web Service function. The decoder then unencodes and decrypts to 
    return the initial string. I'm blaming the base64 algorithm, as as u will see from the code ... if i comment out the base64 read / write and put in the 
    BinHex calls instead, it works fine ... the other way round, and the Base64 decoding produces a truncated byte array!
    Hope u find an answer ...but for now, am still blaming Microsoft! :P
    Cheers
    Mark
    ******DECODER *******
    Me.GetEncryptionKeys(abytKey, abytVec)
    'un-BASE64 the string ... hopefully!??
    vstrString = "<root>" & vstrString & "</root>"   ***vstrString is the encrypted and encoding string****
    objXMLReader = New System.Xml.XmlTextReader(New System.IO.StringReader(vstrString))
    While objXMLReader.Read()
    	If objXMLReader.Name = "root" Then
    		Exit While
    	End If
    End While
    objText = New System.IO.MemoryStream
    'read the bytes from the reader, and put em in a memory stream
    ****THE BYTE ARRAY FROM THE BASE64 ALGORITHM IS TRUNCATED AT THIS POINT ****
    **** ie. It does not produce the same byte array as what is produced by the encryption algorithm initially****
    intBlock = objXMLReader.ReadBase64(abytBase64, 0, abytBase64.Length)
                         ****** OR *******
    intBlock = objXMLReader.ReadBinHex(abytBase64, 0, abytBase64.Length)
    ********************************************
    Do While intBlock <> 0
    	objText.Write(abytBase64, 0, intBlock)
    	'intBlock = objXMLReader.ReadBase64(abytBase64, 0, abytBase64.Length)
    	intBlock = objXMLReader.ReadBinHex(abytBase64, 0, abytBase64.Length)
    Loop
    ReDim abytString(objText.Length)
    objText.Seek(0, IO.SeekOrigin.Begin)
    objText.Read(abytString, 0, abytString.Length)
    'create the CryptoStream object and encrypt
    objOutput = New IO.MemoryStream(abytString)
    'create the Provider object we're using
    objCryptProvider = New System.Security.Cryptography.RC2CryptoServiceProvider
    objEnc = New System.Security.Cryptography.CryptoStream(objOutput, _
      objCryptProvider.CreateDecryptor(abytKey, abytVec), _
      Security.Cryptography.CryptoStreamMode.Read)
    abytBuffer = New Byte(abytString.Length) {}
    objEnc.Read(abytBuffer, 0, abytBuffer.Length) **** This line Fails 99% of time when using BASE64 algorithm
                                                                       **** as byte array is wrong size ... works fine in BinHex***
    ***** END OF DECODER*******
    *****ENCODER******
    Me.GetEncryptionKeys(abytKey, abytVec)
    'write the string to the byte array
    abytString = System.Text.Encoding.Default.GetBytes(vstrString)
    'create the CryptoStream object and encrypt
    objOutput = New IO.MemoryStream
    'create the Provider object we're using
    objCryptProvider = New System.Security.Cryptography.RC2CryptoServiceProvider
    'create the CryptoStream object and encrypt
    objEnc = New System.Security.Cryptography.CryptoStream(objOutput, _
       objCryptProvider.CreateEncryptor(abytKey, abytVec), _
     Security.Cryptography.CryptoStreamMode.Write)
    objEnc.Write(abytString, 0, abytString.Length)
    objEnc.FlushFinalBlock()
    abytBuffer = objOutput.ToArray
    'convert encrypted string to BASE64
    objStrWriter = New System.io.StringWriter
    objXMLWriter = New System.Xml.XmlTextWriter(objStrWriter)
    *****both these lines of code work for the ENCODING part ****
    objXMLWriter.WriteBase64(abytBuffer, 0, abytBuffer.Length)
                                *****OR *******
    objXMLWriter.WriteBinHex(abytBuffer, 0, abytBuffer.Length)
    ****************************
    rstrOutput = objStrWriter.GetStringBuilder.ToString
    ******END OF ENCODER*****
    >
    

  • Next message: Shawn Farkas: "RE: Cannot change Code Group's Membership Condition to Strong Name"

    Relevant Pages


  • Quantcast