C# 数据库链接字符串加密解密工具代码详解
程序员文章站
2023-12-15 08:56:34
有些项目尤其是winform或者是wpf项目,针对一些工具形式的小项目,不想软件流出去之后,懂程序的的拿到手之后一看配置文件就知道了我们数据库的用户名和密码,如果外网能访问...
有些项目尤其是winform或者是wpf项目,针对一些工具形式的小项目,不想软件流出去之后,懂程序的的拿到手之后一看配置文件就知道了我们数据库的用户名和密码,如果外网能访问的话,那就麻烦大了。所以这里为了防止项目外泄之后这些信息不被别人看到,我们就需要对链接字符串或者其他重要信息进行加密,用的时候在解密。
思路:使用两个数对连接字符串进行加密,再用这两个数进行解密。
<add key="configstring" value="4hsxbrnxtken0zokdewfe501tksqlzuyj0zf+c7s5+gpd1sbwbiuh4pg6jefgcnctfr0qfw8fn40m/s8xmqq+8srl8tamlo23z6gsmaqjom="/>
直接上代码:
1:定义一个初始化源数据的类。
public class configinformation { private static configinformation _configinformation; public configinformation instance { get { if (_configinformation == null) { _configinformation = new configinformation(); } return _configinformation; } } // 数据库链接字符串加解密 key value public static string key = "27e167e9-2660-4bc1-bea0-c8781a9f01cb"; public static string vector = "8280d587-f9bf-4127-bbfa-5e0b4b672958"; }
2:加解密方法:
/// <summary> /// 加密 解密 /// </summary> public class decryptandencryptionhelper { private readonly symmetricalgorithm _symmetricalgorithm; private const string defkey = "qazwsxedcrfvtgb!@#$%^&*(tgbrfvedcwsxqaz)(*&^%$#@!"; private string _key = ""; public string key { get { return _key; } set { if (!string.isnullorempty(value)) { _key = value; } else { _key = defkey; } } } private const string defiv = "tgbrfvedcwsxqaz)(*&^%$#@!qazwsxedcrfvtgb!@#$%^&*("; private string _iv = ""; public string iv { get { return _iv; } set { if (!string.isnullorempty(value)) { _iv = value; } else { _iv = defiv; } } } public decryptandencryptionhelper() { _symmetricalgorithm = new rijndaelmanaged(); } public decryptandencryptionhelper(string key, string iv) { _symmetricalgorithm = new rijndaelmanaged(); _key = string.isnullorempty(key) ? defkey : key; _iv = string.isnullorempty(iv) ? defiv : iv; } /// <summary> /// get key /// </summary> /// <returns>密钥</returns> private byte[] getlegalkey() { _symmetricalgorithm.generatekey(); byte[] byttemp = _symmetricalgorithm.key; int keylength = byttemp.length; if (_key.length > keylength) _key = _key.substring(0, keylength); else if (_key.length < keylength) _key = _key.padright(keylength, '#'); return asciiencoding.ascii.getbytes(_key); } /// <summary> /// get iv /// </summary> private byte[] getlegaliv() { _symmetricalgorithm.generateiv(); byte[] byttemp = _symmetricalgorithm.iv; int ivlength = byttemp.length; if (_iv.length > ivlength) _iv = _iv.substring(0, ivlength); else if (_iv.length < ivlength) _iv = _iv.padright(ivlength, '#'); return asciiencoding.ascii.getbytes(_iv); } /// <summary> /// encrypto 加密 /// </summary> public string encrypto(string source) { byte[] bytin = utf8encoding.utf8.getbytes(source); memorystream ms = new memorystream(); _symmetricalgorithm.key = getlegalkey(); _symmetricalgorithm.iv = getlegaliv(); icryptotransform encrypto = _symmetricalgorithm.createencryptor(); cryptostream cs = new cryptostream(ms, encrypto, cryptostreammode.write); cs.write(bytin, 0, bytin.length); cs.flushfinalblock(); ms.close(); byte[] bytout = ms.toarray(); return convert.tobase64string(bytout); } /// <summary> /// decrypto 解密 /// </summary> public string decrypto(string source) { byte[] bytin = convert.frombase64string(source); memorystream ms = new memorystream(bytin, 0, bytin.length); _symmetricalgorithm.key = getlegalkey(); _symmetricalgorithm.iv = getlegaliv(); icryptotransform encrypto = _symmetricalgorithm.createdecryptor(); cryptostream cs = new cryptostream(ms, encrypto, cryptostreammode.read); streamreader sr = new streamreader(cs); return sr.readtoend(); } }
3:使用
// 获取加密的链接字符串,然后解密 string enstring = configurationmanager.appsettings["configstring"]; decryptandencryptionhelper helper = new decryptandencryptionhelper(configinformation.key, configinformation.vector); // 明文 var configstr = helper.decrypto(enstring); return configstr;
这样至少保证了数据的不外泄。
注意:这个加密和解密的算法方法,应该放在服务器。通过请求加解密方法。不应该放在本地代码里,技术牛的的人,把你的项目反编译一样可以看到源代码。
我们在把加密源数据找出来。
所以这个加解密代码不能写在本地,必须部署到安全的服务器上。
总结
以上所述是小编给大家介绍的c# 数据库链接字符串加密解密工具代码详解,希望对大家有所帮助