1

私のプロジェクトでは、XMLファイルを暗号化および復号化する2つの方法があります

XML ファイルにデータを保存およびロードする別の 2 つのメソッド

データをロードするときにこの例外が発生する理由がわかりませんLoad()

入力は有効な Base-64 文字列ではありません。非 Base 64 文字、3 つ以上の埋め込み文字、または埋め込み文字の間に非空白文字が含まれているためです。

これが私の方法です

      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;
      }

データを保存およびロードする別の 2 つの方法

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

OK、エラーは、データを行ごとに暗号化し、ブロック全体として復号化しようとすることです。

Base64 は、各 3 バイトを 4 文字に変換します。入力データが 3 バイトの倍数でない場合、エンコーディングはパディングとして 1 または 2 バイトのゼロを追加します。これは、ストリームの最後にある 1 つまたは 2 つの「=」文字によって示されます。

ここで、連結された複数のコード ブロックをデコードしようとすると、ストリーム内に「=」文字が含まれる可能性が高くなりますが、これは違法です。

解決策: Save-Method を書き直して、最初にすべての xml エンティティを 1 つの大きな文字列/メモリ ブロックに格納し (StringBuilder を使用するか、MemoryStream に書き込みます)、データ ブロック全体に対して一度に暗号化メソッドを使用します。

于 2012-07-29T14:04:51.440 に答える