C# WINFORM安装后的项目 文件夹里有连接字符串的配置文件 请问要怎么请解决 高手求解

2025-01-04 09:32:33
推荐回答(3个)
回答1:

可以通过加密的方法来解决,下面是我以前做的程序里的一段加密代码, 不过不是我写的, 你可以参考一下,
在写入配置文件时,将连接字符串加密,方法如下
///


/// 加密
///

///
///
public static string Encrypt(string Text)
{
return Encrypt(Text,"litianping");
}
///
/// 加密数据
///

///
///
///
public static string Encrypt(string Text,string sKey)
{
DESCryptoServiceProvider des = new DESCryptoServiceProvider();
byte[] inputByteArray;
inputByteArray=Encoding.Default.GetBytes(Text);
des.Key = ASCIIEncoding.ASCII.GetBytes(System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(sKey, "md5").Substring(0, 8));
des.IV = ASCIIEncoding.ASCII.GetBytes(System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(sKey, "md5").Substring(0, 8));
System.IO.MemoryStream ms=new System.IO.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);
}
return ret.ToString();
}

读取时解密如下:
///
/// 解密
///

///
///
public static string Decrypt(string Text)
{
return Decrypt(Text,"随便搞个字符串作为key");
}
///
/// 解密数据
///

///
///
///
public static string Decrypt(string Text,string sKey)
{
DESCryptoServiceProvider des = new DESCryptoServiceProvider();
int len;
len=Text.Length/2;
byte[] inputByteArray = new byte[len];
int x,i;
for(x=0;x {
i = Convert.ToInt32(Text.Substring(x * 2, 2), 16);
inputByteArray[x]=(byte)i;
}
des.Key = ASCIIEncoding.ASCII.GetBytes(System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(sKey, "md5").Substring(0, 8));
des.IV = ASCIIEncoding.ASCII.GetBytes(System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(sKey, "md5").Substring(0, 8));
System.IO.MemoryStream ms=new System.IO.MemoryStream();
CryptoStream cs=new CryptoStream(ms,des.CreateDecryptor(),CryptoStreamMode.Write);
cs.Write(inputByteArray,0,inputByteArray.Length);
cs.FlushFinalBlock();
return Encoding.Default.GetString(ms.ToArray());
}

回答2:

什么东西。说明白点。。或者把你配置文件的代码贴出来。。

回答3:

你要解决什么?