明天的明天 永远的永远 未知的一切 我与你一起承担 ??

是非成败转头空 青山依旧在 几度夕阳红 。。。
  博客园  :: 首页  :: 管理

public sealed class IniFile
    {
        private string path;

        /// <summary>
        /// 实例初始化为指定路径的INI文件。
        /// </summary>
        /// <param name="path">INI文件路径。</param>
        public IniFile(string path)
        {
            this.path = path;
        }

        /// <summary>
        /// 获取INI文件的路径。
        /// </summary>
        public string Path
        {
            get
            {
                return path;
            }
        }

        /// <summary>
        /// 读取指定小节下指定条目的字符串。
        /// </summary>
        /// <param name="sectionName">欲在其中查找条目的小节名称。这个字串不区分大小写。</param>
        /// <param name="keyName">欲获取的项名或条目名。这个字串不区分大小写。</param>
        /// <param name="defaultValue">指定的条目没有找到时返回的默认值。</param>
        /// <returns>指定小节下指定条目的字符串。</returns>
        /// <remarks>如果sectionName为null,则返回所有小节的列表,如果keyName为null,指定小节所有项的列表。</remarks>
        public string ReadString(string sectionName,string keyName,string defaultValue)
        {
            const int MAXSIZE = 255;
            StringBuilder temp = new StringBuilder(MAXSIZE);
            GetPrivateProfileString(sectionName, keyName, defaultValue, temp, 255, this.path);
            return temp.ToString();
        }

        public void WriteString(string sectionName, string keyName, string value)
        {
            WritePrivateProfileString(sectionName, keyName, value, this.path);
        }

        public int ReadInteger(string sectionName, string keyName, int defaultValue)
        {
            return GetPrivateProfileInt(sectionName, keyName, defaultValue,this.path);
        }

        public void WriteInteger(string sectionName, string keyName, int value)
        {
            WritePrivateProfileString(sectionName, keyName, value.ToString(), this.path);
        }

        public bool ReadBoolean(string sectionName, string keyName, bool defaultValue)
        {
            int temp = defaultValue ? 1 : 0;
            int result = GetPrivateProfileInt(sectionName, keyName, temp, this.path);
            return (result == 0 ? false : true);
        }

        public void WriteBoolean(string sectionName, string keyName, bool value)
        {
            string temp = value ? "1" : "0";
            WritePrivateProfileString(sectionName, keyName, temp, this.path);
        }

        /// <summary>
        /// 删除这个项现有的字串。
        /// </summary>
        /// <param name="sectionName">要设置的项名或条目名。这个字串不区分大小写。</param>
        /// <param name="keyName">要删除的项名或条目名。这个字串不区分大小写。</param>
        public void DeleteKey(string sectionName,string keyName)
        {
            WritePrivateProfileString(sectionName, keyName, null, this.path);
        }

        /// <summary>
        /// 删除这个小节的所有设置项。
        /// </summary>
        /// <param name="sectionName">要删除的小节名。这个字串不区分大小写。</param>
        public void EraseSection(string sectionName)
        {
            WritePrivateProfileString(sectionName,null,null,this.path);
        }

[DllImport("kernel32")]
public static extern int WritePrivateProfileString(string lpApplicationName, string lpKeyName, string lpString, string lpFileName);
[DllImport("kernel32")]
public static extern int GetPrivateProfileInt(string lpApplicationName, string lpKeyName, int nDefault, string lpFileName);
[DllImport("kernel32")]
public static extern int GetPrivateProfileString(string lpApplicationName, string lpKeyName, string lpDefault, StringBuilder lpReturnedString, int nSize, string lpFileName);
    }






***************************************************


// 初始化DES加密的密钥和一个随机的、8比特的初始化向量(IV)
private byte[] key_8 = {0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef};
private byte[] IV_8 = {0x12, 0x34, 0x56, 0x78, 0x90, 0xab, 0xcd, 0xef};


/// <summary>
/// DES加密字符串
/// </summary>
/// <param name="encryptString">待加密的字符串</param>
/// <param name="rgbKey">加密密钥,要求为8位</param>
/// <param name="rgbIV">密钥向量</param>
/// <returns>加密成功返回加密后的字符串,失败返回源字符串</returns>
private string DES_Encrypt(string encryptString, byte[] rgbKey, byte[] rgbIV)
{
try
{
byte[] inputByteArray = Encoding.UTF8.GetBytes(encryptString);
DESCryptoServiceProvider dCSP = new DESCryptoServiceProvider();
MemoryStream mStream = new MemoryStream();
CryptoStream cStream = new CryptoStream(mStream, dCSP.CreateEncryptor(rgbKey, rgbIV), CryptoStreamMode.Write);
cStream.Write(inputByteArray, 0, inputByteArray.Length);
cStream.FlushFinalBlock();
return Convert.ToBase64String(mStream.ToArray());
}
catch
{
return encryptString;
}
}

/// <summary>
/// DES解密字符串
/// </summary>
/// <param name="decryptString">待解密的字符串</param>
/// <param name="rgbKey">解密密钥,要求为8位,和加密密钥相同</param>
/// <param name="rgbIV">密钥向量</param>
/// <returns>解密成功返回解密后的字符串,失败返源字符串</returns>
private string DES_Decrypt(string decryptString, byte[] rgbKey, byte[] rgbIV)
{
try
{
byte[] inputByteArray = Convert.FromBase64String(decryptString);
DESCryptoServiceProvider DCSP = new DESCryptoServiceProvider();
MemoryStream mStream = new MemoryStream();
CryptoStream cStream = new CryptoStream(mStream, DCSP.CreateDecryptor(rgbKey, rgbIV), CryptoStreamMode.Write);
cStream.Write(inputByteArray, 0, inputByteArray.Length);
cStream.FlushFinalBlock();
return Encoding.UTF8.GetString(mStream.ToArray());
}
catch
{
return decryptString;
}
}