XML 加密、解密

对XML或是TXT进行加解密

         #region 对文件进行加密解密
        static string iv = "password";
        static string key = "password";

        /// <summary>
        /// DES加密偏移量,必须是>=8位长的字符串
        /// </summary>
        public static string IV
        {
            get { return iv; }
            set { iv = value; }
        }

        /// <summary>
        /// DES加密的私钥,必须是8位长的字符串
        /// </summary>
        public static string Key
        {
            get { return key; }
            set { key = value; }
        }

        /// <summary>
        /// 对文件内容进行DES加密
        /// </summary>
        /// <param name="sourceFile">待加密的文件绝对路径</param>
        /// <param name="destFile">加密后的文件保存的绝对路径</param>
        public static void EncryptFile(string sourceFile, string destFile)
        {
      
            if (!File.Exists(sourceFile)) throw new FileNotFoundException("指定的文件路径不存在!", sourceFile);

            byte[] btKey = Encoding.Default.GetBytes(key);
            byte[] btIV = Encoding.Default.GetBytes(iv);
            DESCryptoServiceProvider des = new DESCryptoServiceProvider();
            byte[] btFile = File.ReadAllBytes(sourceFile);
         
        
            using (FileStream fs = new FileStream(destFile, FileMode.Create, FileAccess.Write))
            {
                try
                {
                    using (CryptoStream cs = new CryptoStream(fs, des.CreateEncryptor(btKey, btIV), CryptoStreamMode.Write))
                    {
                        cs.Write(btFile, 0, btFile.Length);
                        cs.FlushFinalBlock();
                    }
                }
                catch
                {
                    throw;
                }
                finally
                {
                    fs.Close();
                }
            }
        }

        /// <summary>
        /// 对文件内容进行DES加密,加密后覆盖掉原来的文件
        /// </summary>
        /// <param name="sourceFile">待加密的文件的绝对路径</param>
        public void EncryptFile(string sourceFile)
        {
            EncryptFile(sourceFile, sourceFile);
        }

        /// <summary>
        /// 对文件内容进行DES解密
        /// </summary>
        /// <param name="sourceFile">待解密的文件绝对路径</param>
        /// <param name="destFile">解密后的文件保存的绝对路径</param>
        public static void DecryptFile(string sourceFile, string destFile)
        {

            if (!File.Exists(sourceFile))
                throw new FileNotFoundException("指定的文件路径不存在!", sourceFile);

            byte[] btKey = Encoding.Default.GetBytes(key);
            byte[] btIV = Encoding.Default.GetBytes(iv);
            DESCryptoServiceProvider des = new DESCryptoServiceProvider();
            byte[] btFile = File.ReadAllBytes(sourceFile);
         
          
            using (FileStream fs = new FileStream(destFile, FileMode.Create, FileAccess.Write))
            {
                try
                {
                    using (CryptoStream cs = new CryptoStream(fs, des.CreateDecryptor(btKey, btIV), CryptoStreamMode.Write))
                    {
                        cs.Write(btFile, 0, btFile.Length);
                        cs.FlushFinalBlock();
                    }
                }
                catch
                {
                    throw;
                }
                finally
                {
                    fs.Close();
                }
            }
        }

        /// <summary>
        /// 对文件内容进行DES解密,解密后覆盖掉原来的文件
        /// </summary>
        /// <param name="sourceFile">待解密的文件的绝对路径</param>
        public static void DecryptFile(string sourceFile)
        {
            DecryptFile(sourceFile, sourceFile);
        }

        #endregion

 

读写加密的XML或TXT

        #region 读取及操作assembly(XML文件)
        public static void SaveXml(string ConnenctionString, string strKey)//写入动态的数据库配置信息
        {
            CommonClass.DecryptFile(FilePath);
            XmlDocument doc = new XmlDocument();
            //获得配置文件的全路径
            string strFileName = Application.StartupPath+"\\assembly.xml";
            doc.Load(strFileName);
            //找出名称为“add”的所有元素
            XmlNodeList nodes = doc.GetElementsByTagName("add");
            for (int i = 0; i < nodes.Count; i++)
            {
                //获得将当前元素的key属性
                XmlAttribute att = nodes[i].Attributes["key"];
                //根据元素的第一个属性来判断当前的元素是不是目标元素
                if (att.Value == strKey)
                {
                    //对目标元素中的第二个属性赋值
                    att = nodes[i].Attributes["value"];
                    att.Value = ConnenctionString;
                    break;
                }
            }
            doc.Save(strFileName);
            CommonClass.EncryptFile(FilePath, FilePath);
        }

        public static string ReadXml(string strKey)//写入动态的数据库配置信息
        {
            CommonClass.DecryptFile(FilePath);
            string TempValues = "";
            XmlDocument doc = new XmlDocument();
            //获得配置文件的全路径
            string strFileName = Application.StartupPath + "\\assembly.xml";
            doc.Load(strFileName);
            //找出名称为“add”的所有元素
            XmlNodeList nodes = doc.GetElementsByTagName("add");
            for (int i = 0; i < nodes.Count; i++)
            {
                //获得将当前元素的key属性
                XmlAttribute att = nodes[i].Attributes["key"];
                //根据元素的第一个属性来判断当前的元素是不是目标元素
                if (att.Value == strKey)
                {
                    att = nodes[i].Attributes["value"];
                    TempValues = att.Value;
                    break;
                }
            }
            doc.Save(strFileName);
            CommonClass.EncryptFile(FilePath, FilePath);
            return TempValues;
        }

        public static SqlConnection GetXmlConn()//写入动态的数据库配置信息
        {
            CommonClass.DecryptFile(FilePath);
            SqlConnection Conn;
            string tempDataBase = "", tempServerIP = "", tempUser = "", tempPassword = "", tempStr = "" ;
            XmlDocument doc = new XmlDocument();
            //获得配置文件的全路径
            string strFileName = Application.StartupPath + "\\assembly.xml";
            doc.Load(strFileName);
            //找出名称为“add”的所有元素
            XmlNodeList nodes = doc.GetElementsByTagName("add");
            for (int i = 0; i < nodes.Count; i++)
            {
                //获得将当前元素的key属性
                XmlAttribute att = nodes[i].Attributes["key"];
                //根据元素的第一个属性来判断当前的元素是不是目标元素
                if (att.Value == "ServerIP")
                {
                    tempServerIP = nodes[i].Attributes["value"].Value;
                }
                else if (att.Value == "DataBase")
                {
                    tempDataBase = nodes[i].Attributes["value"].Value;
                }
                else if (att.Value == "User")
                {
                    tempUser = nodes[i].Attributes["value"].Value;
                }
                else if (att.Value == "Password")
                {
                    tempPassword = nodes[i].Attributes["value"].Value;
                }
            }
            doc.Save(strFileName);
            tempStr = "uid=" + tempUser + ";pwd=" + tempPassword;
            tempStr += ";initial catalog=" + tempDataBase + ";Server=" + tempServerIP + ";";
            tempStr += "Connect Timeout=30";
            Conn = new SqlConnection(tempStr);
            CommonClass.EncryptFile(FilePath, FilePath);
            return Conn;
        }

        #endregion

转载于:https://www.cnblogs.com/pato/archive/2010/09/26/1836195.html

xml加密XML Encryption)是w3c加密xml的标准。这个加密过程包括加密xml文档的元素及其子元素,通过加密xml的初始内容将被替换,但其xml格式仍然被完好的保留。 介绍 我们有3个加密xml的方法 1、仅仅使用对称加密的方法加密xml 这种加密方法只使用一个密钥,也就是说无论是加密xml还是解密xml都使用一个相同的密钥。因为这个密钥不会在被加密xml中保存,所以我们需要在加密解密的过程中加载这个密钥并保护它不被窃取。 2、使用对称加密和非对称加密相结合的方法来加密xml 这种方法需要一个用于加密数据的对称密钥和一个用于保护这个对称密钥的非对称密钥。被加密的对称密钥和被加密的数据一起保存在xml文档中。当用私有非对称密钥解密密钥的时候要用公开非对称密钥对密钥进行加密。 本文就将使用这种方法。想学到其他更多的方法请参看MSDN等到更多的信息。 (译者注:非对称加密算法需要两个密钥:公开密钥(publickey)和私有密钥(privatekey)。公开密钥与私有密钥是一对,如果用公开密钥对数据进行加密,只有用对应的私有密钥才能解密;如果用私有密钥对数据进行加密,那么只有用对应的公开密钥才能解密。因为加密解密使用的是两个不同的密钥,所以这种算法叫作非对称加密算法。) 3、使用X.509加密xml,这种方法是用X.509作为非对称密钥,它由诸如VeriSign之类的第三方提供。 方法 不管xml加密是如何完成的,保存加密数据总是用两种方法之一。 1、加密后所有的元素都被命名为 2、加密后只有数据被替换,而元素名称仍然是可读的,不会发生变化。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值