1

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

MSDN 页面链接

现在,当调用 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);
}

请告诉我哪里出错了。

4

0 回答 0