Write XML in Base64 encoding

前端 未结 4 2021
后悔当初
后悔当初 2021-01-20 00:37

Following my questions about storing data, it has been suggested that I could use XML but then obfuscate the file by encoding it using Base64. I like this idea, and I have a

相关标签:
4条回答
  • 2021-01-20 00:56

    Write it:

    var sb = new StringBuilder(); 
    var settings = new XmlWriterSettings();
    //..settings
    using (var writer = XmlWriter.Create(sb, settings))
    {
        //...
    }
    //http://stackoverflow.com/questions/1564718/using-stringwriter-for-xml-serialization
    var encoding = new UnicodeEncoding(); 
    string s = Convert.ToBase64String(encoding.GetBytes(sb.ToString()));
    
    File.WriteAllText("c:\temp.txt", s);
    

    Read it:

    string readText = File.ReadAllText("c:\temp.txt");
    byte[] toDecodeByte = Convert.FromBase64String(readText);
    
    using (var stream = new MemoryStream(toDecodeByte))
    {
        using (XmlReader reader = XmlReader.Create(stream))
        {
            //.. read your xml
        }
    }
    
    0 讨论(0)
  • 2021-01-20 00:58

    You need to convert your xmlwriter to string like this

     using (var sw = new StringWriter()) {
     using (var xw = XmlWriter.Create(sw)) {
    // Build Xml with xw.
    
    
       }
      return sw.ToString();
    }
    

    then convert your string to Base64. and write it to file

    0 讨论(0)
  • 2021-01-20 01:01

    This will have the information that you are looking for:

    http://msdn.microsoft.com/en-us/library/system.xml.xmlwriter.writebase64.aspx

    HTH,

    d3

    0 讨论(0)
  • 2021-01-20 01:06

    Here is an example using a MemoryStream

    Save the XML data using MemoryStream to base64 encode the data and write it to a file.

    using (var ms = new MemoryStream())
    {   
        // Memory Stream
        using (XmlWriter writer = XmlWriter.Create(ms,settings)) 
        {
            writer.WriteStartDocument();
            writer.WriteStartElement("Database");
            writer.WriteStartElement("Entry");
            writer.WriteAttributeString("key", "value");
            writer.WriteEndElement();
            writer.WriteEndElement();
    
            writer.Flush();
            writer.Close();
    
            // XML Data (Debug)
            Console.WriteLine(new UTF8Encoding().GetString(ms.ToArray()));
    
            // Pre encoding hex (Debug)
            Console.WriteLine(BitConverter.ToString(ms.ToArray()));
    
            string s = Convert.ToBase64String(ms.ToArray());
            Console.WriteLine(s); // (Debug)
    
            // Post encoding
            File.WriteAllText(@"C:\Temp\A.enc", s);
        }
    }
    

    Read the file back

    // Read encoded file back to xml
    string enc_text = File.ReadAllText(@"C:\Temp\A.enc");
    Console.WriteLine(enc_text); // (Debug)
    
    // Pre encoding hex
    byte[] mem_bytes = Convert.FromBase64String(enc_text);
    Console.WriteLine(BitConverter.ToString(mem_bytes)); // (Debug)
    
    // XML Data
    Console.WriteLine(new UTF8Encoding().GetString(mem_bytes));
    
    // Read byte array into Reader
    using (var stream = new MemoryStream(mem_bytes))
    {
        using (XmlReader reader = XmlReader.Create(stream))
        {
            // Use reader as desired
        }
    }
    
    0 讨论(0)
提交回复
热议问题