大家好,从来没用过asp.net 下面这段c#代码怎么用呀。http://msdn.microsoft.com/zh-cn/library/ms229746.aspx是不是新建一个页面default.aspx,把代码加到default.aspx.cs中就行了呢?加进去报错,痛苦呀,没有接触过.net

解决方案 »

  1.   

    实例很详细,建控制台程序
    也可使用web 程序
      

  2.   

    要先添加对System.Security.dll  的引用。然后代码头:
    using System;
    using System.Xml;
    using System.Security.Cryptography;
    using System.Security.Cryptography.Xml;
      

  3.   

    另外我建的页面是default2.aspx
    头部
    <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default2.aspx.cs" Inherits="Default2" %>另外default2.aspxusing System;
    using System.Xml;
    using System.Security.Cryptography;
    using System.Security.Cryptography.Xml;
    public partial class Default2 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {    }
    }class Program
    {
        static void Main(string[] args)
        {
            // Create an XmlDocument object.
            XmlDocument xmlDoc = new XmlDocument();        // Load an XML file into the XmlDocument object.
            try
            {
                xmlDoc.PreserveWhitespace = true;
                xmlDoc.Load("test.xml");
            }
    .......这样写对吗 那个class Default2  处是不是有问题呀
      

  4.   

    class Default2  这个没问题(什么都没写  怎么会有问题)
    下面那个就不知道了
      

  5.   

    下面那个有问题,这个b/s架构,为什么要有main函数呢?
    web里面就2种代码  一种是页面初始化,也就是page_load的代码
    另一种就是实际操作的代码,比如button的点击事件,你写个事件绑定上去就可以了你建个main什么意思呢?那是c/s的东西
      

  6.   

    http://msdn.microsoft.com/zh-cn/library/ms229746.aspx
    现在假设这个代码是正确的,谁能帮我写一下 ,这个代码具体怎么用。谢谢。现在学习一时半会也上不了手。
      

  7.   

    我们公司的所有机器都访问不了ms的网站的
    我看不了~~~~我们公司上亿的公司却在用盗版的系统和.net
      

  8.   

    using System;
    using System.Xml;
    using System.Security.Cryptography;
    using System.Security.Cryptography.Xml;class Program
    {
        static void Main(string[] args)
        {
            // Create an XmlDocument object.
            XmlDocument xmlDoc = new XmlDocument();        // Load an XML file into the XmlDocument object.
            try
            {
                xmlDoc.PreserveWhitespace = true;
                xmlDoc.Load("test.xml");
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
            }        // Create a new CspParameters object to specify
            // a key container.
            CspParameters cspParams = new CspParameters();
            cspParams.KeyContainerName = "XML_ENC_RSA_KEY";        // Create a new RSA key and save it in the container.  This key will encrypt
            // a symmetric key, which will then be encryped in the XML document.
            RSACryptoServiceProvider rsaKey = new RSACryptoServiceProvider(cspParams);        try
            {
                // Encrypt the "creditcard" element.
                Encrypt(xmlDoc, "creditcard", "EncryptedElement1", rsaKey, "rsaKey");
                // Save the XML document.
                xmlDoc.Save("test.xml");            // Display the encrypted XML to the console.
                Console.WriteLine("Encrypted XML:");
                Console.WriteLine();
                Console.WriteLine(xmlDoc.OuterXml);
                Decrypt(xmlDoc, rsaKey, "rsaKey");
                xmlDoc.Save("test.xml");
                // Display the encrypted XML to the console.
                Console.WriteLine();
                Console.WriteLine("Decrypted XML:");
                Console.WriteLine();
                Console.WriteLine(xmlDoc.OuterXml);        }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
            }
            finally
            {
                // Clear the RSA key.
                rsaKey.Clear();
            }
            Console.ReadLine();
        }    public static void Encrypt(XmlDocument Doc, string ElementToEncrypt, string EncryptionElementID, RSA Alg, string KeyName)
        {
            // Check the arguments.
            if (Doc == null)
                throw new ArgumentNullException("Doc");
            if (ElementToEncrypt == null)
                throw new ArgumentNullException("ElementToEncrypt");
            if (EncryptionElementID == null)
                throw new ArgumentNullException("EncryptionElementID");
            if (Alg == null)
                throw new ArgumentNullException("Alg");
            if (KeyName == null)
                throw new ArgumentNullException("KeyName");        ////////////////////////////////////////////////
            // Find the specified element in the XmlDocument
            // object and create a new XmlElemnt object.
            ////////////////////////////////////////////////
            XmlElement elementToEncrypt = Doc.GetElementsByTagName(ElementToEncrypt)[0] as XmlElement;        // Throw an XmlException if the element was not found.
            if (elementToEncrypt == null)
            {
                throw new XmlException("The specified element was not found");        }
            RijndaelManaged sessionKey = null;        try
            {
                //////////////////////////////////////////////////
                // Create a new instance of the EncryptedXml class
                // and use it to encrypt the XmlElement with the
                // a new random symmetric key.
                //////////////////////////////////////////////////            // Create a 256 bit Rijndael key.
                sessionKey = new RijndaelManaged();
                sessionKey.KeySize = 256;            EncryptedXml eXml = new EncryptedXml();            byte[] encryptedElement = eXml.EncryptData(elementToEncrypt, sessionKey, false);
                ////////////////////////////////////////////////
                // Construct an EncryptedData object and populate
                // it with the desired encryption information.
                ////////////////////////////////////////////////            EncryptedData edElement = new EncryptedData();
                edElement.Type = EncryptedXml.XmlEncElementUrl;
                edElement.Id = EncryptionElementID;
                // Create an EncryptionMethod element so that the
                // receiver knows which algorithm to use for decryption.            edElement.EncryptionMethod = new EncryptionMethod(EncryptedXml.XmlEncAES256Url);
                // Encrypt the session key and add it to an EncryptedKey element.
                EncryptedKey ek = new EncryptedKey();            byte[] encryptedKey = EncryptedXml.EncryptKey(sessionKey.Key, Alg, false);            ek.CipherData = new CipherData(encryptedKey);            ek.EncryptionMethod = new EncryptionMethod(EncryptedXml.XmlEncRSA15Url);            // Create a new DataReference element
                // for the KeyInfo element.  This optional
                // element specifies which EncryptedData
                // uses this key.  An XML document can have
                // multiple EncryptedData elements that use
                // different keys.
                DataReference dRef = new DataReference();            // Specify the EncryptedData URI.
                dRef.Uri = "#" + EncryptionElementID;            // Add the DataReference to the EncryptedKey.
                ek.AddReference(dRef);
                // Add the encrypted key to the
                // EncryptedData object.            edElement.KeyInfo.AddClause(new KeyInfoEncryptedKey(ek));
                // Set the KeyInfo element to specify the
                // name of the RSA key.
                // Create a new KeyInfoName element.
                KeyInfoName kin = new KeyInfoName();            // Specify a name for the key.
                kin.Value = KeyName;            // Add the KeyInfoName element to the
                // EncryptedKey object.
                ek.KeyInfo.AddClause(kin);
                // 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);
            }
            catch (Exception e)
            {
                // re-throw the exception.
                throw e;
            }
            finally
            {
                if (sessionKey != null)
                {
                    sessionKey.Clear();
                }        }    }    public static void Decrypt(XmlDocument Doc, RSA Alg, string KeyName)
        {
            // Check the arguments.  
            if (Doc == null)
                throw new ArgumentNullException("Doc");
            if (Alg == null)
                throw new ArgumentNullException("Alg");
            if (KeyName == null)
                throw new ArgumentNullException("KeyName");        // Create a new EncryptedXml object.
            EncryptedXml exml = new EncryptedXml(Doc);        // Add a key-name mapping.
            // This method can only decrypt documents
            // that present the specified key name.
            exml.AddKeyNameMapping(KeyName, Alg);        // Decrypt the element.
            exml.DecryptDocument();    }}此示例假定一个名为 "test.xml" 的文件与编译的程序在同一目录中。它还假定 "test.xml" 包含一个 "creditcard" 元素。您可以将以下 XML 放入名为 test.xml 的文件中,并将它和此示例一起使用。复制<root>
        <creditcard>
            <number>19834209</number>
            <expiry>02/02/2002</expiry>
        </creditcard>
    </root>