有时候可能会用到,给数据库连接的字符串加密,让人不那么容易看出服务器的地址和帐号信息,所以大家可以用这个工具,把字符串加密,然后执行的时候解码,就可以了。方便实用。
方法一:
using System;
using System.Collections.Generic;
using System.Text;
namespace StringEncry
{
class EncodeAndDecode
{
/// <summary>
/// Base64加密
/// </summary>
/// <param name="codeName">加密采用的编码方式</param>
/// <param name="source">待加密的明文</param>
/// <returns></returns>
public static string EncodeBase64(Encoding encode, string source)
{
string enstring = "";
byte[] bytes = encode.GetBytes(source);
try
{
enstring = Convert.ToBase64String(bytes);
}
catch
{
enstring = source;
}
return enstring;
}
/// <summary>
/// Base64加密,采用utf8编码方式加密
/// </summary>
/// <param name="source">待加密的明文</param>
/// <returns>加密后的字符串</returns>
public static string EncodeBase64(string source)
{
return EncodeBase64(Encoding.UTF8, source);
}
/// <summary>
/// Base64解密
/// </summary>
/// <param name="codeName">解密采用的编码方式,注意和加密时采用的方式一致</param>
/// <param name="result">待解密的密文</param>
/// <returns>解密后的字符串</returns>
public static string DecodeBase64(Encoding encode, string result)
{
string decode = "";
byte[] bytes = Convert.FromBase64String(result);
try
{
decode = encode.GetString(bytes);
}
catch
{
decode = result;
}
return decode;
}
/// <summary>
/// Base64解密,采用utf8编码方式解密
/// </summary>
/// <param name="result">待解密的密文</param>
/// <returns>解密后的字符串</returns>
public static string DecodeBase64(string result)
{
return DecodeBase64(Encoding.UTF8, result);
}
}
}
这个类,大家可以直接使用。
效果图如下:
方法二:
/// <summary>
/// MD5加密,输入明文,返回密文
/// </summary>
/// <param name="pToEncrypt"></param>
/// <returns>加密后字符串</returns>
[WebMethod(Description = "【函数描述】加密数据库连接字符串")]
public string MD5Encrypt(string pToEncrypt)
{
DESCryptoServiceProvider des = new DESCryptoServiceProvider();
byte[] inputByteArray = Encoding.Default.GetBytes(pToEncrypt);
//des.Key = ASCIIEncoding.ASCII.GetBytes(sKey);
//des.IV = ASCIIEncoding.ASCII.GetBytes(sKey);
byte[] iv = { 102, 16, 93, 156, 78, 4, 218, 32 };//定义偏移量
byte[] key = { 55, 103, 246, 79, 36, 99, 167, 3 };//定义密钥
des.Key = key;
des.IV = iv;
MemoryStream ms = new 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);
}
ret.ToString();
return ret.ToString();
}
/// <summary>
/// 解密连接字符串,输入加密后字符串,返回解密密后的字符串
/// </summary>
/// <param name="pToDecrypt">密文</param>
/// <returns>明文</returns>
[WebMethod(Description = "【函数描述】解密数据库连接字符串")]
public string MD5Decrypt(string pToDecrypt)
{
DESCryptoServiceProvider des = new DESCryptoServiceProvider();
byte[] inputByteArray = new byte[pToDecrypt.Length / 2];
for (int x = 0; x < pToDecrypt.Length / 2; x++)
{
int i = (Convert.ToInt32(pToDecrypt.Substring(x * 2, 2), 16));
inputByteArray[x] = (byte)i;
}
//des.Key = ASCIIEncoding.ASCII.GetBytes(sKey);
//des.IV = ASCIIEncoding.ASCII.GetBytes(sKey);
byte[] iv = { 102, 16, 93, 156, 78, 4, 218, 32 };//定义偏移量
byte[] key = { 55, 103, 246, 79, 36, 99, 167, 3 };//定义密钥
des.Key = key;
des.IV = iv;
MemoryStream ms = new MemoryStream();
CryptoStream cs = new CryptoStream(ms, des.CreateDecryptor(), CryptoStreamMode.Write);
cs.Write(inputByteArray, 0, inputByteArray.Length);
cs.FlushFinalBlock();
StringBuilder ret = new StringBuilder();
return System.Text.Encoding.Default.GetString(ms.ToArray());
}