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);//寫入密碼不加密
<span style="font-family: 微軟雅黑; font-size: 14px; line-height: 21px;">           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());
<pre name="code" class="csharp"><div style="font-family: 微軟雅黑; font-size: 14px; line-height: 21px;">            string account = Config.IniReadValue("UserName", "account");    </div><div style="font-family: 微軟雅黑; font-size: 14px; line-height: 21px;">            string passwd = Config.IniReadValue("PassWord", "passwd");</div>
//讀取密碼需要解密
               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();



發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章