1

在我的项目中,我有两种加密和解密 XML 文件的方法

以及另一种在 XML 文件中保存和加载数据的方法

我不知道为什么加载数据时会出现此异常Load()

输入不是有效的 Base-64 字符串,因为它包含非 base 64 字符、两个以上的填充字符或填充字符中的非空白字符。

这是我的方法

      public static string Encrypt(string plainText)
      {
            byte[] initVectorBytes = Encoding.ASCII.GetBytes("teto1620@#$%asdf");
            byte[] plainTextBytes = Encoding.UTF8.GetBytes(plainText);
            byte[] keyBytes = Encoding.Unicode.GetBytes("_+)&qwer9512popo");
            var symmetricKey = new RijndaelManaged();
            symmetricKey.Mode = CipherMode.CBC;
            ICryptoTransform encryptor = symmetricKey.CreateEncryptor(keyBytes, initVectorBytes);
            MemoryStream memoryStream = new MemoryStream();
            CryptoStream cryptoStream = new CryptoStream(memoryStream, encryptor, CryptoStreamMode.Write);
            cryptoStream.Write(plainTextBytes, 0, plainTextBytes.Length);
            cryptoStream.FlushFinalBlock();
            byte[] cipherTextBytes = memoryStream.ToArray();
            memoryStream.Close();
            cryptoStream.Close();
            string cipherText = Convert.ToBase64String(cipherTextBytes);
            return cipherText;

      }
      public static string Decrypt(string cipherText)
      {
            byte[] initVectorBytes = Encoding.ASCII.GetBytes("teto1620@#$%asdf");
            byte[] cipherTextBytes = Convert.FromBase64String(cipherText);
            byte[] keyBytes = Encoding.Unicode.GetBytes("_+)&qwer9512popo");
            RijndaelManaged symmetricKey = new RijndaelManaged();
            symmetricKey.Mode = CipherMode.CBC;
            ICryptoTransform decryptor = symmetricKey.CreateDecryptor(keyBytes, initVectorBytes);
            MemoryStream memoryStream = new MemoryStream(cipherTextBytes);
            CryptoStream cryptoStream = new CryptoStream(memoryStream, decryptor, CryptoStreamMode.Read);
            byte[] plainTextBytes = new byte[cipherTextBytes.Length];
            int decryptedByteCount = cryptoStream.Read(plainTextBytes, 0, plainTextBytes.Length);
            memoryStream.Close();
            cryptoStream.Close();
            string plainText = Encoding.UTF8.GetString(plainTextBytes, 0, decryptedByteCount);
            return plainText;
      }

以及另外两种保存和加载数据的方法

public bool Save()
{
    bool isSaved = false;

    try
    {
        if (SharedData.DeviceList != null)
        {
            var fileInfo = new FileInfo(SharedData.CONFIGURATION_FULL_PATH);
            if (!fileInfo.Exists)
            {
                File.Create(SharedData.CONFIGURATION_FULL_PATH).Close();

            }
            var streamWriter = new StreamWriter(SharedData.CONFIGURATION_FULL_PATH);
            streamWriter.Write(Encrypt("<?xml version=\"1.0\" encoding=\"utf-8\"?><settings>"));
            if (SharedData.DeviceList.Count > 0)
            {
                foreach (var device in SharedData.DeviceList)
                {
                    streamWriter.Write(Encrypt("<username>" +device.Username + "</username>"));
                    streamWriter.Write(Encrypt("<AgentName>" +device.AgentName + "</AgentName>"));
                    streamWriter.Write(Encrypt("<password>" + device.Password + "</password>"));
                    streamWriter.Write(Encrypt("<domain>" + device.Domain + "</domain>"));
                    streamWriter.Write(Encrypt("<peerUri>" + device.PeerURI + "</peerUri>"));
                    streamWriter.Write(Encrypt("<sipUri>" + device.SipURI + "</sipUri>"));
                    streamWriter.Write(Encrypt("<fqdn>" + device.FQDN+ "</fqdn>"));
                    streamWriter.Write(Encrypt("<type>" + ((byte)device.Type).ToString() + "</type>"));
                    streamWriter.Write(Encrypt("<transportType>" +((byte)device.TransportType).ToString() + "</transportType>"));
                }
            }

            streamWriter.Write(Encrypt("</settings>"));
            streamWriter.Close();
            isSaved = true;
        }
        else isSaved = false;
    }
    catch { isSaved = false; }
    return isSaved;
}

private bool Load()
{
    bool isLoaded = false;
    try
    {
        if (SharedData.DeviceList != null)
        {
            var fileInfo = new FileInfo(SharedData.CONFIGURATION_FULL_PATH);
            if (fileInfo.Exists)
            {
                var textTodecrypt = File.ReadAllText(SharedData.CONFIGURATION_FULL_PATH);
                var xmlReader = new XmlTextReader(Decrypt(textTodecrypt));
                var nodeElement = string.Empty;
                var thisDevice = new Device();
                while (xmlReader.Read())
                {
                    if (xmlReader.NodeType == XmlNodeType.Element) nodeElement = xmlReader.Name;
                    if (xmlReader.NodeType == XmlNodeType.Text)
                    {
                        switch (nodeElement)
                        {
                            case @"username":
                                                 if (xmlReader.Value.Length > 0) thisDevice = new Device
                                                                                                {
                                                                                                    Username = xmlReader.Value
                                                                                                };
                                break;
                            case @"AgentName":
                                if (xmlReader.Value.Length > 0)
                                                      thisDevice.AgentName = xmlReader.Value;
                                break;
                            case @"password":
                                if (xmlReader.Value.Length > 0)
                                                      thisDevice.Password = xmlReader.Value;
                                break;
                            case @"peerUri":
                                if (xmlReader.Value.Length > 0)
                                                      thisDevice.PeerURI = xmlReader.Value;
                                break;
                            case @"sipUri":
                                if (xmlReader.Value.Length > 0)
                                                      thisDevice.SipURI = xmlReader.Value;
                                break;
                            case @"domain":
                                if (xmlReader.Value.Length > 0)
                                                      thisDevice.Domain = xmlReader.Value;
                                break;
                            case @"fqdn":
                                if (xmlReader.Value.Length > 0)
                                                      thisDevice.FQDN = xmlReader.Value;
                                break;
                            case @"type":
                                if (xmlReader.Value.Length > 0)
                                                      thisDevice.Type = (Enums.DeviceType)byte.Parse(xmlReader.Value);
                                break;
                            case @"transportType":
                                if (xmlReader.Value.Length > 0)
                                {
                                                      thisDevice.TransportType = (Enums.ServerTransportType)byte.Parse(xmlReader.Value);
                                    if (!IsExist(thisDevice, false)) SharedData.DeviceList.Add(thisDevice);
                                }
                                break;
                        }
                    }
                }
                xmlReader.Close();
                SharedData.TempDeviceList = SharedData.DeviceList;
                isLoaded = true;
            }
            else isLoaded = false;
        }
    }
    catch (Exception)
    {
        isLoaded = false;
    }
    return isLoaded;
}
4

1 回答 1

5

好的,您的错误是您逐行加密数据并尝试将其作为一个整体解密。

Base64 将每 3 个字节转换为 4 个字符。如果输入数据不是 3 字节的倍数,则编码添加 1 或 2 个零字节作为填充。然后在流的末尾用一两个“=”字符表示这一点。

现在,如果您尝试解码多个连接的代码块,您很可能在流中包含“=”字符,这是非法的。

解决方案:重写您的 Save-Method 以首先将所有 xml-entities 存储在一个大字符串/内存块中(使用 StringBuilder 或写入 MemoryStream),然后立即对整个数据块使用您的加密方法。

于 2012-07-29T14:04:51.440 回答