C# XML文件的加密

C# XML文件的加密,c#,asp.net,xml,encryption,C#,Asp.net,Xml,Encryption,我想加密一个XML文件。我获得了以下代码的帮助: 现在,当调用encrypt函数时,问题就来了。将所有值插入xml文件后,我调用encrypt函数。它通向开关箱256。但情况依然如此 XmlElement machmac = doc.CreateElement("Mach_MAC"); machmac.InnerText = txtMachMac.Text; root.AppendChild(machmac); doc.AppendChil

我想加密一个XML文件。我获得了以下代码的帮助:

现在,当调用encrypt函数时,问题就来了。将所有值插入xml文件后,我调用encrypt函数。它通向开关箱256。但情况依然如此

  XmlElement machmac = doc.CreateElement("Mach_MAC");
        machmac.InnerText = txtMachMac.Text;
        root.AppendChild(machmac);

        doc.AppendChild(root);
        doc.Save(@"D:\DEV\Gener_Lic.xml");
        lblmsg.Text = "Your data is saved";

        RijndaelManaged key = null;

        try
        {
            // Create a new Rijndael key.
            key = new RijndaelManaged();
            // Load an XML document.
            XmlDocument xmlDoc = new XmlDocument();

            xmlDoc.Load("D:\\DEV\\Gener_Lic.xml");

            // Encrypt the "Generate_License" element.
            Encrypt(xmlDoc, "Generate_License", key);

            Console.WriteLine("The element was encrypted");

            Console.WriteLine(xmlDoc.InnerXml);

            //Decrypt(xmlDoc, key);

            //Console.WriteLine("The element was decrypted");

            //Console.WriteLine(xmlDoc.InnerXml);


        }
        catch (Exception exc)
        {
            Console.WriteLine(exc.Message);
        }
        finally
        {
            // Clear the key. if (key != null)
            {
                key.Clear();
            }
        }
    }

    catch (Exception ex)
    {
        Console.WriteLine("error : " + ex.Message);
    }


}
加密代码如下:

 public static void Encrypt(XmlDocument Doc, string ElementName, SymmetricAlgorithm Key)
{
    // Check the arguments 

    if (Doc == null)
        throw new ArgumentNullException("Doc");
    if (ElementName == null)
        throw new ArgumentNullException("ElementToEncrypt");
    if (Key == null)
        throw new ArgumentNullException("Alg");

    // Find the specified element in the XmlDocument // object and create a new XmlElemnt object.

    XmlElement elementToEncrypt = Doc.GetElementsByTagName(ElementName)[0] as XmlElement;

    // Throw an XmlException if the element was not found. 

    if (elementToEncrypt == null)
    {
        throw new XmlException("The specified element was not found");

    }

    // Create a new instance of the EncryptedXml class
    // and use it to encrypt the XmlElement with the
    // symmetric key.

    EncryptedXml eXml = new EncryptedXml();

    byte[] encryptedElement = eXml.EncryptData(elementToEncrypt, Key, false);

    // Construct an EncryptedData object and populate
    // it with the desired encryption information

    EncryptedData edElement = new EncryptedData();
    edElement.Type = EncryptedXml.XmlEncElementUrl;

    // Create an EncryptionMethod element so that the
    // receiver knows which algorithm to use for decryption
    // Determine what kind of algorithm is being used and
    // supply the appropriate URL to the EncryptionMethod element. 

    string encryptionMethod = null;

    if (Key is TripleDES)
    {
        encryptionMethod = EncryptedXml.XmlEncTripleDESUrl;
    }
    else if (Key is DES)
    {
        encryptionMethod = EncryptedXml.XmlEncDESUrl;
    }
    if (Key is Rijndael)
    {
        switch (Key.KeySize)
        {
            case 128:
                encryptionMethod = EncryptedXml.XmlEncAES128Url;
                break;
            case 192:
                encryptionMethod = EncryptedXml.XmlEncAES192Url;
                break;
            case 256:
                encryptionMethod = EncryptedXml.XmlEncAES256Url;
                break;
        }
    }
    else
    {
        // Throw an exception if the transform is not in the previous categories 
        throw new CryptographicException("The specified algorithm is not supported for XML Encryption.");
    }

    edElement.EncryptionMethod = new EncryptionMethod(encryptionMethod);

    // Add the encrypted element data to the
    // EncryptedData object.
    edElement.CipherData.CipherValue = encryptedElement;

    /// Replace the element from the original XmlDocument
    // object with the EncryptedData element
    EncryptedXml.ReplaceElement(elementToEncrypt, edElement, false);
}

请告诉我哪里出了问题。

您是否尝试在
Encrypt
方法中设置断点并查看其中发生了什么?是的。是的。它转到交换机案例256。我的xml数据仍然保持不变。没有发生加密。请尝试在此函数调用中替换“Mach_MAC”而不是“Generate_License”-Encrypt(xmlDoc,“Generate_License”,key);它进入catch块
catch(异常exc){Console.WriteLine(exc.Message);}