zoukankan      html  css  js  c++  java
  • 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

  • 相关阅读:
    java spring boot- freemarker 配置 yml使用流程
    layer 漂亮的弹窗
    react-native 打包apk 更新js和常见问题
    mysql 运行中 偶尔 报错 2002 也许是这个问题,内存不足导致的
    关于rsa公钥格式的处理,一行纯内容进行换行格式化
    第十篇、让UIScrollView的滚动条常显
    第九篇、自定义底部UITabBar
    第八篇、封装NSURLSession网络请求框架
    第二篇、Swift_自定义 tabbar 的 badgeValue显示样式
    第七篇、OC_图片的裁剪基于SDWebImage
  • 原文地址:https://www.cnblogs.com/pato/p/1836195.html
Copyright © 2011-2022 走看看