C# 数据库连接设置保存为 ini文件问题

在C# winfrom 窗体中,要填入数据库的连接信息,要把这些填好的信息保存在本地的某个ini文件,该如何编写代码?再次打开程序的时候能够读取ini文件里的信息,而且保存的ini文件中数据库密码这项不能以明文的形式显示出来,要进行加密?

1.读取ini文件键值对的类,这样写

class ConfigureFile
{
public string ConfigureFilePath; //INI文件名

//声明读写INI文件的API函数

[DllImport("kernel32")]
private static extern long WritePrivateProfileString(string section, string key, string val, string filePath);

[DllImport("kernel32")]
private static extern int GetPrivateProfileString(string section, string key, string def, StringBuilder retVal, int size, string filePath);

public ConfigureFile()
{
//
// TODO: 在此处添加构造函数逻辑
//
}

//类的构造函数,传递INI文件名

public ConfigureFile(string ConfigureFilePath)
{
this.ConfigureFilePath = ConfigureFilePath;
}

//写INI文件

public void WriteConfigureFileValue(string Section, string Key, string Value)
{
WritePrivateProfileString(Section, Key, Value, this.ConfigureFilePath);
}

//读取INI文件指定

public string ReadConfigureFileValue(string Section, string Key)
{
StringBuilder temp = new StringBuilder(255);
int i = GetPrivateProfileString(Section, Key, "", temp, 255, this.ConfigureFilePath);
Console.Write(temp.ToString());
if (temp.ToString() == null ||temp.ToString() =="")
{
throw new Exception("Profile.ini读取配置文件为空.");
}
return temp.ToString();
}
}

2.处理读取的键值对这样写

/// <summary>
/// 返回Profile.ini配置参数
/// </summary>
public class Profile
{
private static ConfigureFile ReadConfigureFile = new ConfigureFile(".\\Profile.ini");
/// <summary>
/// 返回数据库服务名
/// </summary>
/// <returns></returns>
public static string getServerName()
{
//对于你的问题 这个返回值就是nor 在ini文件里就是ServerName=nor
return ReadConfigureFile.ReadConfigureFileValue("DataBase", "ServerName");
}
/// <summary>
/// 返回登陆账户
/// </summary>
/// <returns></returns>
public static string getLogId()
{
//对于你的问题 返回值就是 sa 在ini里就是LogId=sa
return ReadConfigureFile.ReadConfigureFileValue("DataBase", "LogId");
}
/// <summary>
/// 返回登陆密码
/// </summary>
/// <returns></returns>
public static string getPassWord()
{
//对于你的问题返回值就是**** 在ini里就是PassWord=*****(***是加密后的密码 密文形式)
return ReadConfigureFile.ReadConfigureFileValue("DataBase", "PassWord");
}
}

我把读ini文件 和读取ini文件中的key/value对分开写 这样是为了让你能更灵活的去添加ini中的key/value对 当你添加key.value对时 只需要在Profile类中添加相应的get方法就可以了 方便修改

ini文件中的键值对写成这样

[DATABASE]
ServerName='nor'
LogId='sa'
PassWord='****'(****是密文形式)
3.这是加密你密码的类 我用MD5配合加盐值进行加密 这样就很安全

public class MD5Encrypt
{
/// <summary>
/// 将明文用MD5加密
/// </summary>
/// <param name="strPwd">需要加密的明文</param>
/// <param name="strKey">用于加密的Key(加盐值)</param>
/// <returns>加密后得到的MD5值</returns>
public static string ToEncodingByMD5(string strPwd, string strKey)
{
string key = strKey.Trim();
string strEnPwd = string.Empty;
if (key.Length == 0)
{
key = "lwk";//默认加盐值 你自己定义加盐值,要验证登录密码是否正确 需要传进和保存其加密时 //相同的加盐值 然后对比他们的密文是不是相同
}
byte[] Original = Encoding.Default.GetBytes(strPwd);
byte[] SaltValue = Encoding.Default.GetBytes(key);
byte[] ToKey = new byte[Original.Length + SaltValue.Length];
Original.CopyTo(ToKey, 0);
SaltValue.CopyTo(ToKey, Original.Length);
MD5 st = MD5.Create();
byte[] SaltPWD = st.ComputeHash(ToKey);
byte[] PWD = new byte[SaltPWD.Length + SaltValue.Length];
SaltPWD.CopyTo(PWD, 0);
SaltValue.CopyTo(PWD, SaltPWD.Length);
strEnPwd = Convert.ToBase64String(PWD);
return strEnPwd;
}
}
温馨提示:答案为网友推荐,仅供参考
第1个回答  2013-06-18
在数据库类型中,可以用int值作为类型的索引值,比如在数据库类型这个ComboBox中Sql Server是第一项,那么它的索引值为0,也就是数据库类型为0.下面是加密解密和输入输出的代码:using System;
using System.Text;
using System.IO;
using System.Security.Cryptography;public class DESCE
{
/// <summary>
/// DES 加密
/// </summary>
/// <param name="strValue"></param>
/// <param name="strKey"></param>
/// <returns></returns>
public static string Encryp(string strValue, string strKey)
{
string EncrypString;
byte[] key = ASCIIEncoding.ASCII.GetBytes(strKey.Substring(0, 8));
byte[] iv = ASCIIEncoding.ASCII.GetBytes(strKey.Insert(0, "w").Substring(0, 8));
if (strValue != "")
{
try
{
DESCryptoServiceProvider dsp = new DESCryptoServiceProvider();
MemoryStream memStream = new MemoryStream();
using (memStream)
{
CryptoStream crypStream = new CryptoStream(memStream, dsp.CreateEncryptor(key, iv), CryptoStreamMode.Write);
StreamWriter sWriter = new StreamWriter(crypStream);
sWriter.Write(strValue);
sWriter.Flush();
crypStream.FlushFinalBlock();
memStream.Flush();
EncrypString = Convert.ToBase64String(memStream.GetBuffer(), 0, (int)memStream.Length);
}
}
catch (ArgumentException)
{
EncrypString = null;
}
catch (CryptographicException)
{
EncrypString = null;
}
}
else
{
EncrypString = "";
}
return EncrypString;
} /// <summary>
/// DES解密
/// </summary>
/// <param name="EncValue"></param>
/// <param name="strKey"></param>
/// <returns></returns>
public static string Decryp(string EncValue, string strKey)
{
string strValue;
byte[] key = ASCIIEncoding.ASCII.GetBytes(strKey.Substring(0, 8));
byte[] iv = ASCIIEncoding.ASCII.GetBytes(strKey.Insert(0, "w").Substring(0, 8));
if (EncValue != "")
{
try
{
DESCryptoServiceProvider dsp = new DESCryptoServiceProvider();
byte[] buffer = Convert.FromBase64String(EncValue);
MemoryStream memStream = new MemoryStream();
using (memStream)
{
CryptoStream crypStream = new CryptoStream(memStream, dsp.CreateDecryptor(key, iv), CryptoStreamMode.Write);
crypStream.Write(buffer, 0, buffer.Length);
crypStream.FlushFinalBlock();
strValue = ASCIIEncoding.UTF8.GetString(memStream.ToArray());
}
}
catch (ArgumentException)
{
strValue = null;
}
catch (CryptographicException)
{
strValue = null;
}
}
else
{
strValue = "";
}
return strValue;
}
}本回答被网友采纳

相关了解……

你可能感兴趣的内容

本站内容来自于网友发表,不代表本站立场,仅表示其个人看法,不对其真实性、正确性、有效性作任何的担保
相关事宜请发邮件给我们
© 非常风气网