我最近用c#加密了xml文档,是msdn上一个例子,对称加密,我主要是想在save的时候加密,用户打开就是乱码,然后load 的时候解密,程序能读取,但是郁闷的事好像是必须先加密,然后才能解密,不然就会报错,说那个默认密钥为空。。上部分代码:  public void fileEncryption()
        {
            XmlDocument xmlDoc = new XmlDocument();
            xmlDoc.Load(_xmlPath);            if (string.IsNullOrEmpty(_xmlPath) == false)
            {
                try
                {
                    rsa = new RSACryptoServiceProvider();                    XmlElement xmlElemt;
                    xmlElemt = xmlDoc.DocumentElement;                    EncryptedXml xmlEnc = new EncryptedXml(xmlDoc);                    xmlEnc.AddKeyNameMapping("asyncKey66", rsa);                    EncryptedData encXml = xmlEnc.Encrypt(xmlElemt, "asyncKey66");                    EncryptedXml.ReplaceElement(xmlElemt, encXml, false);
                }
                catch (XmlException ex)
                {
                    //likely the xml is not well formed
                    throw ex;
                }
            }
            xmlDoc.Save(_xmlPath);
        }public void fileDecrypt()
        {
            if (string.IsNullOrEmpty(_xmlPath) == false)
            {
                //1. Load the encrypted xml
                XmlDocument xmlEncDoc = new XmlDocument();
                xmlEncDoc.Load(_xmlPath);                //2. Create the encrypted xml and specify the same key, and key name, that was used to encrypt it
                //private rsa key is used here to decrypt the data
                EncryptedXml encXml = new EncryptedXml(xmlEncDoc);
                encXml.AddKeyNameMapping("asyncKey66", rsa);                //3. Decrypt the document <EncryptedData> elements containing the names key
                encXml.DecryptDocument();                xmlEncDoc.Save(_xmlPath);                //xml document has been modified and now contains the decrypted xml
            }代码应该比较清晰的,加密解密也没问题。。

解决方案 »

  1.   

    XmlDocument.Save会检查xml格式,需符合xml规范
      

  2.   

    加密解密其实好做,难的就难在 加密算法。你实际上只是想让xml不暴露为明文。那就好搞了。
    只是这个加密算法你一定不能让别人知道,如果你现在使用主流的加密算法,迟早客户会给你爆了。
    最简单的思路,把文件读到内存中,根据一种算法交换 文件byte[]的位置,解密的时候根据解密算法换回来就OK。能够实现简单的加密解密,目的只是让文件不明文显示。又简单,又能达到目的。
    加密方法(颠倒数据的方法,解密也是一样把数据倒回来)using (FileStream outFs = new FileStream("文件路径", FileMode.Create))
                {
                    using (FileStream openFs = new FileStream(@"加密后文件的地址", FileMode.Open))
                    {
                        FileInfo fileInfo = new FileInfo(@"加密后文件的地址");
                        byte[] bytes = new byte[1024 * 1024 * 4];
                        int readLength;
                        while ((readLength = openFs.Read(bytes, 0, bytes.Length)) > 0)
                        {
                            for (int i = 0; i < readLength; i++)
                            {
                                bytes[i] = (byte)(byte.MaxValue - bytes[i]);
                            }
                            outFs.Write(bytes, 0, readLength);
                        }
                    }
                }
      

  3.   

    参考如何使用C#加密解密XML文档
      

  4.   


    你们说的都有道理,关键是我加密了,读取了 ,用户打开又是明文,我发个demo 你看看,看程序怎么既能读取数据,用户直接打开xml为乱码,这里面我引用了别人一个加密类。。