1

在我的项目中,我有 xml 文件来存储和检索Device对象,我希望用户只能通过我的应用程序而不是手动在我的 xml 文件中添加或删除,所以我需要以某种格式记录数据,用户无法理解它,例如 asp.net 中的 viewStatebase64或别的东西

我的问题是如何将数据保存在 xml 文件中,但我希望用户无法理解所写的内容

这是我的设备类

public class Device
    {
        public string Username { get; set; }
        public string AgentName { get; set; }
        public string Password { get; set; }
        public string Domain { get; set; }
        public string PeerURI { get; set; }
        public string SipURI { get; set; }
        public string FQDN { get; set; }
        public Enums.DeviceType Type { get; set; }
        public Enums.ServerTransportType TransportType { get; set; }
        public bool IsInitialized { get; set; }
    }

这是我的 xml 文件

<?xml version="1.0" encoding="utf-8"?>
<settings>
  <username>foo</username>
  <AgentName>foo1</AgentName>
  <password>foo2</password>
  <domain>go</domain>
  <peerUri>140.242.250.200</peerUri>
  <sipUri>sip:xxx@xxxxx.com</sipUri>
  <fqdn>ff.go.xxxx.com</fqdn>
  <type>2</type>
  <transportType>2</transportType>
</settings>
4

4 回答 4

2

像这样的东西应该适用于 Base64 编码整个序列化过程......

using System.IO;
using System.Security.Cryptography;
using System.Xml.Serialization;

...

private void WriteToFile(Device device, string filePath)
{
    using (FileStream fs = new FileStream(filePath, FileMode.Create))
    {
        using (CryptoStream cs = new CryptoStream(fs, new ToBase64Transform(), CryptoStreamMode.Write))
        {
            XmlSerializer x = new XmlSerializer(typeof(Device));
            x.Serialize(cs, device);
        }
    }
}

private Device ReadFromFile(string filePath)
{
    using (FileStream fs = new FileStream(filePath, FileMode.Open))
    {
        using (CryptoStream cs = new CryptoStream(fs, new FromBase64Transform(), CryptoStreamMode.Read))
        {
            XmlSerializer x = new XmlSerializer(typeof(Device));
            return x.Deserialize(cs) as Device;
        }
    }
}
于 2012-07-28T16:24:24.660 回答
2

Base64 应该可以。

String username = "foo";
byte[] EncodeAsBase64 = System.Text.ASCIIEncoding.ASCII.GetBytes(username);
string enc_username = System.Convert.ToBase64String(EncodeAsBase64);

然后使用serializer来存储 enc_username。

于 2012-07-28T16:10:55.413 回答
1

如果您不希望任何人能够使用文本编辑器编辑您的 XML 数据文件,则:

  • 您不应该使用 XML,而应该将其保存为某种自定义二进制格式,或者
  • 您应该使用加密的 XML
于 2012-07-28T04:59:56.517 回答
1

如果您使用二进制序列化,则该文件根本不是人类可读的。

这是您的问题的一个简短示例:

public class Enums
{
    public enum DeviceType
    {
        Mouse,
        HardDisk,
        CdRom,
    }

    public enum ServerTransportType
    {
        Udp,
        Tcp,
    }
}

[Serializable]
public class Device
{
    public string Username { get; set; }
    public string AgentName { get; set; }
    public string Password { get; set; }
    public string Domain { get; set; }
    public string PeerURI { get; set; }
    public string SipURI { get; set; }
    public string FQDN { get; set; }
    public Enums.DeviceType Type { get; set; }
    public Enums.ServerTransportType TransportType { get; set; }
    public bool IsInitialized { get; set; }
}

public class BinarySerialize
{
    public void Test()
    {
        var device = new Device();

        device.Username = "userName";
        device.AgentName = "agentName";
        device.Password = "password";
        device.Domain = "domain";
        device.PeerURI = "peerURI";
        device.SipURI = "sipURI";
        device.FQDN = "fqdn";
        device.Type = Enums.DeviceType.HardDisk;
        device.TransportType = Enums.ServerTransportType.Tcp;
        device.IsInitialized = true;

        string fileName = @"C:\temp\device.bin";
        this.Serialize(device, fileName);

        var d = this.Deserialize(fileName);
    }

    public void Serialize(Device device, string fileName)
    {
        using (Stream stream = File.Open(fileName, FileMode.Create))
        {
            BinaryFormatter bformatter = new BinaryFormatter();
            bformatter.Serialize(stream, device);
            stream.Close();
        }
    }

    public Device Deserialize(string fileName)
    {
        var device = new Device();

        using (Stream stream = File.Open(fileName, FileMode.Open))
        {
            BinaryFormatter bformatter = new BinaryFormatter();
            device = (Device)bformatter.Deserialize(stream);
            stream.Close();
        }

        return device;
    }
于 2012-07-28T16:49:58.107 回答