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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
XML加密解密工具的手机版是一种可以在手机上使用的应用程序,旨在帮助用户对XML文件进行加密解密操作。 XML(可扩展标记语言)是一种常用的数据交换格式,但它的内容通常是以文本形式呈现,并不具备安全性。因此,有时候需要对其中的敏感信息进行加密以保护数据的安全性。 XML加密解密工具手机版提供了一种方便快捷的方式来进行这些操作。用户只需将需要加密XML文件导入到应用程序中,并设置一个加密密码。应用程序将使用高级加密算法对XML文件进行加密处理,确保加密后的文件无法被未授权的访问者读取。 同样地,用户也可以通过应用程序将加密后的XML文件导入,并输入正确的解密密码。应用程序将使用相同的加密算法和密码进行解密操作,还原XML文件的内容。用户可以随时进行加密解密操作,方便灵活。 XML加密解密工具手机版还提供了其他功能,例如加密解密日志的记录、设置不同加密算法和密码长度等。用户可以根据自己的需求进行设置,以确保加密过程的安全性和稳定性。 总而言之,XML加密解密工具手机版是一款方便实用的应用程序,可以帮助用户对XML文件进行加密解密操作,保护敏感数据的安全性。它为用户提供了高效、安全的加密解密服务,为数据交换提供了更加稳定和可靠的保护。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值