当前位置:编程学习 > C#/ASP.NET >>

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

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

 

答案:在数据库类型中,可以用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;
    }
}

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

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


上一个:c#考试题谁有,最好是微软的?
下一个:急一份求C#实训总结报告????

CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,