发布网友 发布时间:2022-04-24 23:54
共4个回答
热心网友 时间:2022-05-02 06:02
我来回答吧: 不懂可以Hi,我一般都是这么做的
先给你个加密和解密类
告诉你个加密 解密类。
using System;
using System.IO;
using System.Security.Cryptography;
namespace CryptInfoNameSpace
{
class CryptInfo
{
//加密
public static string Encrypt(string text)
{
Rijndael crypt=Rijndael.Create();
byte[] key=new byte[32]{0XA6,0X7D,0XE1,0X3F,0X35,0X0E,0XE1,0XA9,0X83,0XA5,0X62,0XAA,0X7A,0XAE,0X79,0X98,
0XA7,0X33,0X49,0XFF,0XE6,0XAE,0XBF,0X8D,0X8D,0X20,0X8A,0X49,0X31,0X3A,0X12,0X40};
byte[] iv=new byte[16]{0XF8,0X8B,0X01,0XFB,0X08,0X85,0X9A,0XA4,0XBE,0X45,0X28,0X56,0X03,0X42,0XF6,0X19};
crypt.Key=key;
crypt.IV=iv;
MemoryStream ms=new MemoryStream();
ICryptoTransform transformEncode=new ToBaseTransform();
//Base编码
CryptoStream csEncode=new CryptoStream(ms,transformEncode,CryptoStreamMode.Write);
CryptoStream csEncrypt=new CryptoStream(csEncode,crypt.CreateEncryptor(),CryptoStreamMode.Write);
System.Text.UTF8Encoding enc=new System.Text.UTF8Encoding();
byte[] rawData=enc.GetBytes(text);
csEncrypt.Write(rawData,0,rawData.Length);
csEncrypt.FlushFinalBlock();
byte[] encryptedData=new byte[ms.Length];
ms.Position=0;
ms.Read(encryptedData,0,(int)ms.Length);
return enc.GetString(encryptedData);
}
//解密,加密解密中的 byte[] key,byte[] iv可以自行修改,加密解密的 数组值一定一致,否则无法对应加密 解密。
public static string Decrypt(string text)
{
Rijndael crypt=Rijndael.Create();
byte[] key=new byte[32]{0XA6,0X7D,0XE1,0X3F,0X35,0X0E,0XE1,0XA9,0X83,0XA5,0X62,0XAA,0X7A,0XAE,0X79,0X98,
0XA7,0X33,0X49,0XFF,0XE6,0XAE,0XBF,0X8D,0X8D,0X20,0X8A,0X49,0X31,0X3A,0X12,0X40};
byte[] iv=new byte[16]{0XF8,0X8B,0X01,0XFB,0X08,0X85,0X9A,0XA4,0XBE,0X45,0X28,0X56,0X03,0X42,0XF6,0X19};
crypt.Key=key;
crypt.IV=iv;
MemoryStream ms=new MemoryStream();
CryptoStream csDecrypt=new CryptoStream(ms,crypt.CreateDecryptor(),CryptoStreamMode.Write);
ICryptoTransform transformDecode=new FromBaseTransform();
CryptoStream csDecode=new CryptoStream(csDecrypt,transformDecode,CryptoStreamMode.Write);
System.Text.UTF8Encoding enc=new System.Text.UTF8Encoding();
byte[] rawData=enc.GetBytes(text);
csDecode.Write(rawData,0,rawData.Length);
csDecode.FlushFinalBlock();
byte[] decryptedData=new byte[ms.Length];
ms.Position=0;
ms.Read(decryptedData,0,(int)ms.Length);
return(enc.GetString(decryptedData));
}
}
}
在项目中 引用该类,
下面个思路:
把数据连接字符串 ,经过CryptInfoNameSpace.CryptInfo.Encrypt(string text)加密后 ,写入配置文件inf 或者 配置文件txt文件。
当执行Main函数时,读取配置文件,(如果配置文件不存在,或者读取错误, 重新加密的数据库连接字符串加密后写入配置文件)。然后通过函数 CryptInfoNameSpace.CryptInfo.Decrypt(string text)方法解密成原文连接字符串,并传递给其他类使用。即可。
热心网友 时间:2022-05-02 07:20
加密意义不大,最好在客户端程序和服务器数据库之间加个层,例如:WEB服务、WCF,
千万不要把服务器数据库直接暴露在互联网上,太危险了!
热心网友 时间:2022-05-02 08:55
string consstr = "连接字符串";
//加密
byte[] buffer=System.Text.Encoding.UTF8.GetBytes(consstr);
string k = Convert.ToBaseString(buffer);
//解密
byte[] buffer1 = Convert.FromBaseString(k);
string y = System.Text.Encoding.UTF8.GetString(buffer1);
热心网友 时间:2022-05-02 10:46
楼上是可行的
不过最好把class CryptInfo,前面加个public,否则访问不了