c# 账号密码加密, 写入读取ini文件

        [DllImport("kernel32")]
        private static extern int GetPrivateProfileString(string section, string key, string def, StringBuilder retvalue, int siz, string inipath);
        [DllImport("kernel32")]
        private static extern long WritePrivateProfileString(string section, string key, string val, string filePath);
        
        private static string m_iniPath = System.Windows.Forms.Application.StartupPath + "\\config.ini";//配置路径


        //写INI文件     
        public static void IniWriteValue(string Section, string Key, string Value)    
        {

            WritePrivateProfileString(Section, Key, Value, m_iniPath);

        }



        //读取INI文件指定     
        public static string IniReadValue(string Section, string Key)

        {
            StringBuilder temp = new StringBuilder(255);

            int i = GetPrivateProfileString(Section, Key, "", temp, 255, m_iniPath);

            return temp.ToString();

        }

以上为Config类文件内。

 

 

       Config.IniWriteValue("UserName", "account", account);//写入密码不加密
       private string passwdKey = "asfafagsgdsgkhk"; //加密的秘钥</span>
       StringBuilder ps = new StringBuilder();
       for (int i = 0; i < passwd.Length; ++i)
       {
          int c = passwd[i] ^ passwdKey[i];   //异或加密,使用int存储
          ps.Append(c + " ");
       }
        Config.IniWriteValue("PassWord", "passwd", ps.ToString());
        string account = Config.IniReadValue("UserName", "account");                
        string passwd = Config.IniReadValue("PassWord", "passwd");

//读取密码需要解密

 

               string[] ps = passwd.Split(' ');

                StringBuilder sb = new StringBuilder();

                for (int i = 0; i < ps.Length; i++)

                {

                    sb.Append((char)(passwdKey[i] ^ int.Parse(ps[i]))); //异或解密, 转换成char

                }  

passwd = sb.ToString();

 

 

 

 

 

 


版权声明:本文为zheng2263原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。