using System;
using System.Collections.Generic;
using System.Text;
using System.Runtime.InteropServices;
using System.IO;
using System.Security.Cryptography;
    
    
public class WriteIniFile
{
    private string strKey = "GZ530@%@!";
    public string strinipath;
    [DllImport("kernel32")]
    private static extern long WritePrivateProfileString(string StrProjcetName, string skey, string sval, string sfilePath);
    [DllImport("kernel32")]
    private static extern int GetPrivateProfileString(string StrProjcetName, string skey, string sdef, StringBuilder retVal, int Intsize, string sfilePath);
    
    /// 
    /// 构造方法
    /// 
    /// 文件路径
    public WriteIniFile(string INIPath)
    {
        strinipath = INIPath;
    }
    /// 
    /// 写入INI文件
    /// 
    /// 项目名称(如 [MyProjectName] )
    /// 键
    /// 值
    public void IniWriteValue(string StrProjcetName, string skey, string StrValue)
    {
        WritePrivateProfileString(StrProjcetName, skey, StrValue, this.strinipath);
    }
    
    /// 
    /// Md5加密写入INI文件
    /// 
    /// 项目名称(如 [MyProjectName] )
    /// 键
    /// 值
    public void IniMd5WriteValue(string StrProjcetName, string skey, string StrValue)
    {
        string str = MD5Encrypt(StrValue, strKey);
        WritePrivateProfileString(StrProjcetName, skey, str, this.strinipath);
    }
    
    /// 
    /// 读出INI文件
    /// 
    /// 项目名称(如 [MyProjectName] )
    /// 键
    public string IniReadValue(string StrProjcetName, string skey)
    {
        StringBuilder temp = new StringBuilder(500);
        int i = GetPrivateProfileString(StrProjcetName, skey, "", temp, 500, this.strinipath);
        return temp.ToString();
    }
    
    /// 
    /// 读出INI文件
    /// 
    /// 项目名称(如 [MyProjectName] )
    /// 键
    public string IniMd5ReadValue(string StrProjcetName, string skey)
    {
        StringBuilder temp = new StringBuilder(500);
        int i = GetPrivateProfileString(StrProjcetName, skey, "", temp, 500, this.strinipath);
        string str = temp.ToString();
    
        return MD5Decrypt(str, strKey);
    }
    
    /// 
    /// 验证文件是否存在
    /// 
    /// 布尔值
    public bool ExistINIFile()
    {
        return File.Exists(strinipath);
    }
    
    /// 
    /// 带参数的MD5加密
    /// 
    /// 
    /// 
    /// 
    public string MD5Encrypt(string spToEncrypt, string sKey)
    {
        DESCryptoServiceProvider des = new DESCryptoServiceProvider();
        byte[] inputByteArray = Encoding.Default.GetBytes(spToEncrypt);
        des.skey = ASCIIEncoding.ASCII.GetBytes(sKey);
        des.IV = ASCIIEncoding.ASCII.GetBytes(sKey);
        MemoryStream ms = new MemoryStream();
        CryptoStream cs = new CryptoStream(ms, des.CreateEncryptor(), CryptoStreamMode.Write);
        cs.Write(inputByteArray, 0, inputByteArray.Length);
        cs.FlushFinalBlock();
        StringBuilder ret = new StringBuilder();
        foreach (byte b in ms.ToArray())
        {
            ret.AppendFormat("{0:X2}", b);
        }
        ret.ToString();
        return ret.ToString();
    
    
    }
    
    /// 
    /// 带参数的MD5解密
    /// 
    /// 
    /// 
    /// 
    public string MD5Decrypt(string spToDecrypt, string sKey)
    {
        DESCryptoServiceProvider des = new DESCryptoServiceProvider();
    
        byte[] inputByteArray = new byte[spToDecrypt.Length / 2];
        for (int x = 0; x < spToDecrypt.Length / 2; x++)
        {
            int i = (Convert.ToInt32(spToDecrypt.Substring(x * 2, 2), 16));
            inputByteArray[x] = (byte)i;
        }
    
        des.skey = ASCIIEncoding.ASCII.GetBytes(sKey);
        des.IV = ASCIIEncoding.ASCII.GetBytes(sKey);
        MemoryStream ms = new MemoryStream();
        CryptoStream cs = new CryptoStream(ms, des.CreateDecryptor(), CryptoStreamMode.Write);
        cs.Write(inputByteArray, 0, inputByteArray.Length);
        cs.FlushFinalBlock();
    
        StringBuilder ret = new StringBuilder();
    
        return System.Text.Encoding.Default.GetString(ms.ToArray());
    }
}