111,120
社区成员
发帖
与我相关
我的任务
分享
对XML或是TXT进行加解密
#region 对文件进行加密解密
static string iv = "password";
static string key = "password";
/// <summary>
/// DES加密偏移量,必须是>=8位长的字符串
/// </summary>
public static string IV
{
get { return iv; }
set { iv = value; }
}
/// <summary>
/// DES加密的私钥,必须是8位长的字符串
/// </summary>
public static string Key
{
get { return key; }
set { key = value; }
}
/// <summary>
/// 对文件内容进行DES加密
/// </summary>
/// <param name="sourceFile">待加密的文件绝对路径</param>
/// <param name="destFile">加密后的文件保存的绝对路径</param>
public static void EncryptFile(string sourceFile, string destFile)
{
if (!File.Exists(sourceFile)) throw new FileNotFoundException("指定的文件路径不存在!", sourceFile);
byte[] btKey = Encoding.Default.GetBytes(key);
byte[] btIV = Encoding.Default.GetBytes(iv);
DESCryptoServiceProvider des = new DESCryptoServiceProvider();
byte[] btFile = File.ReadAllBytes(sourceFile);
using (FileStream fs = new FileStream(destFile, FileMode.Create, FileAccess.Write))
{
try
{
using (CryptoStream cs = new CryptoStream(fs, des.CreateEncryptor(btKey, btIV), CryptoStreamMode.Write))
{
cs.Write(btFile, 0, btFile.Length);
cs.FlushFinalBlock();
}
}
catch
{
throw;
}
finally
{
fs.Close();
}
}
}
/// <summary>
/// 对文件内容进行DES加密,加密后覆盖掉原来的文件
/// </summary>
/// <param name="sourceFile">待加密的文件的绝对路径</param>
public void EncryptFile(string sourceFile)
{
EncryptFile(sourceFile, sourceFile);
}
/// <summary>
/// 对文件内容进行DES解密
/// </summary>
/// <param name="sourceFile">待解密的文件绝对路径</param>
/// <param name="destFile">解密后的文件保存的绝对路径</param>
public static void DecryptFile(string sourceFile, string destFile)
{
if (!File.Exists(sourceFile))
throw new FileNotFoundException("指定的文件路径不存在!", sourceFile);
byte[] btKey = Encoding.Default.GetBytes(key);
byte[] btIV = Encoding.Default.GetBytes(iv);
DESCryptoServiceProvider des = new DESCryptoServiceProvider();
byte[] btFile = File.ReadAllBytes(sourceFile);
using (FileStream fs = new FileStream(destFile, FileMode.Create, FileAccess.Write))
{
try
{
using (CryptoStream cs = new CryptoStream(fs, des.CreateDecryptor(btKey, btIV), CryptoStreamMode.Write))
{
cs.Write(btFile, 0, btFile.Length);
cs.FlushFinalBlock();
}
}
catch
{
throw;
}
finally
{
fs.Close();
}
}
}
/// <summary>
/// 对文件内容进行DES解密,解密后覆盖掉原来的文件
/// </summary>
/// <param name="sourceFile">待解密的文件的绝对路径</param>
public static void DecryptFile(string sourceFile)
{
DecryptFile(sourceFile, sourceFile);
}
#endregion
读写加密的XML或TXT
#region 读取及操作assembly(XML文件)
public static void SaveXml(string ConnenctionString, string strKey)//写入动态的数据库配置信息
{
CommonClass.DecryptFile(FilePath);
XmlDocument doc = new XmlDocument();
//获得配置文件的全路径
string strFileName = Application.StartupPath+"\\assembly.xml";
doc.Load(strFileName);
//找出名称为“add”的所有元素
XmlNodeList nodes = doc.GetElementsByTagName("add");
for (int i = 0; i < nodes.Count; i++)
{
//获得将当前元素的key属性
XmlAttribute att = nodes[i].Attributes["key"];
//根据元素的第一个属性来判断当前的元素是不是目标元素
if (att.Value == strKey)
{
//对目标元素中的第二个属性赋值
att = nodes[i].Attributes["value"];
att.Value = ConnenctionString;
break;
}
}
doc.Save(strFileName);
CommonClass.EncryptFile(FilePath, FilePath);
}
public static string ReadXml(string strKey)//写入动态的数据库配置信息
{
CommonClass.DecryptFile(FilePath);
string TempValues = "";
XmlDocument doc = new XmlDocument();
//获得配置文件的全路径
string strFileName = Application.StartupPath + "\\assembly.xml";
doc.Load(strFileName);
//找出名称为“add”的所有元素
XmlNodeList nodes = doc.GetElementsByTagName("add");
for (int i = 0; i < nodes.Count; i++)
{
//获得将当前元素的key属性
XmlAttribute att = nodes[i].Attributes["key"];
//根据元素的第一个属性来判断当前的元素是不是目标元素
if (att.Value == strKey)
{
att = nodes[i].Attributes["value"];
TempValues = att.Value;
break;
}
}
doc.Save(strFileName);
CommonClass.EncryptFile(FilePath, FilePath);
return TempValues;
}
public static SqlConnection GetXmlConn()//写入动态的数据库配置信息
{
CommonClass.DecryptFile(FilePath);
SqlConnection Conn;
string tempDataBase = "", tempServerIP = "", tempUser = "", tempPassword = "", tempStr = "" ;
XmlDocument doc = new XmlDocument();
//获得配置文件的全路径
string strFileName = Application.StartupPath + "\\assembly.xml";
doc.Load(strFileName);
//找出名称为“add”的所有元素
XmlNodeList nodes = doc.GetElementsByTagName("add");
for (int i = 0; i < nodes.Count; i++)
{
//获得将当前元素的key属性
XmlAttribute att = nodes[i].Attributes["key"];
//根据元素的第一个属性来判断当前的元素是不是目标元素
if (att.Value == "ServerIP")
{
tempServerIP = nodes[i].Attributes["value"].Value;
}
else if (att.Value == "DataBase")
{
tempDataBase = nodes[i].Attributes["value"].Value;
}
else if (att.Value == "User")
{
tempUser = nodes[i].Attributes["value"].Value;
}
else if (att.Value == "Password")
{
tempPassword = nodes[i].Attributes["value"].Value;
}
}
doc.Save(strFileName);
tempStr = "uid=" + tempUser + ";pwd=" + tempPassword;
tempStr += ";initial catalog=" + tempDataBase + ";Server=" + tempServerIP + ";";
tempStr += "Connect Timeout=30";
Conn = new SqlConnection(tempStr);
CommonClass.EncryptFile(FilePath, FilePath);
return Conn;
}
#endregion
using System;
using System.IO;
using System.Text;
using System.Security.Cryptography;
namespace Tools
{
/// <summary>
/// 加密
/// </summary>
public class AES
{
//默认密钥向量
private static byte[] Keys = { 0x18, 0x72, 0x65, 0x79, 0x6F, 0x57, 0x6D, 0x79, 0x35, 0x6E, 0x6F, 0x77, 0x6D, 0x61, 0x6E, 0x3F };
/// <summary>
/// 设置加密密匙向量
/// </summary>
public static byte[] IV
{
set { Keys = value; }
get { return Keys; }
}
/// <summary>
/// AES加密数据
/// </summary>
/// <param name="encryptString">需要加密的数据</param>
/// <param name="encryptKey">加密密匙</param>
/// <returns>加密后的数据</returns>
public static string Encode(string encryptString, string encryptKey)
{
encryptKey = Utils.GetSubString(encryptKey, 32, "");
encryptKey = encryptKey.PadRight(32, ' ');
RijndaelManaged rijndaelProvider = new RijndaelManaged();
rijndaelProvider.Key = Encoding.UTF8.GetBytes(encryptKey.Substring(0, 32));
rijndaelProvider.IV = IV;
ICryptoTransform rijndaelEncrypt = rijndaelProvider.CreateEncryptor();
byte[] inputData = Encoding.UTF8.GetBytes(encryptString);
byte[] encryptedData = rijndaelEncrypt.TransformFinalBlock(inputData, 0, inputData.Length);
return Convert.ToBase64String(encryptedData);
}
/// <summary>
/// AES解密
/// </summary>
/// <param name="decryptString">需要解密的数据</param>
/// <param name="decryptKey">加密时用的加密密匙</param>
/// <returns>解密后的数据</returns>
public static string Decode(string decryptString, string decryptKey)
{
try
{
decryptKey = Utils.GetSubString(decryptKey, 32, "");
decryptKey = decryptKey.PadRight(32, ' ');
RijndaelManaged rijndaelProvider = new RijndaelManaged();
rijndaelProvider.Key = Encoding.UTF8.GetBytes(decryptKey);
rijndaelProvider.IV = IV;
ICryptoTransform rijndaelDecrypt = rijndaelProvider.CreateDecryptor();
byte[] inputData = Convert.FromBase64String(decryptString);
byte[] decryptedData = rijndaelDecrypt.TransformFinalBlock(inputData, 0, inputData.Length);
return Encoding.UTF8.GetString(decryptedData);
}
catch
{
return "";
}
}
}
/// <summary>
/// 加密
/// </summary>
public class DES
{
//默认密钥向量
private static byte[] Keys = { 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF };
/// <summary>
/// 设置加密密匙向量
/// </summary>
public static byte[] IV
{
set { Keys = value; }
get { return Keys; }
}
/// <summary>
/// DES加密字符串
/// </summary>
/// <param name="encryptString">待加密的字符串</param>
/// <param name="encryptKey">加密密钥,要求为8位</param>
/// <returns>加密成功返回加密后的字符串,失败返回源串</returns>
public static string Encode(string encryptString, string encryptKey)
{
encryptKey = Utils.GetSubString(encryptKey, 8, "");
encryptKey = encryptKey.PadRight(8, ' ');
byte[] rgbKey = Encoding.UTF8.GetBytes(encryptKey.Substring(0, 8));
byte[] rgbIV = IV;
byte[] inputByteArray = Encoding.UTF8.GetBytes(encryptString);
DESCryptoServiceProvider dCSP = new DESCryptoServiceProvider();
MemoryStream mStream = new MemoryStream();
CryptoStream cStream = new CryptoStream(mStream, dCSP.CreateEncryptor(rgbKey, rgbIV), CryptoStreamMode.Write);
cStream.Write(inputByteArray, 0, inputByteArray.Length);
cStream.FlushFinalBlock();
return Convert.ToBase64String(mStream.ToArray());
}
/// <summary>
/// DES解密字符串
/// </summary>
/// <param name="decryptString">待解密的字符串</param>
/// <param name="decryptKey">解密密钥,要求为8位,和加密密钥相同</param>
/// <returns>解密成功返回解密后的字符串,失败返源串</returns>
public static string Decode(string decryptString, string decryptKey)
{
try
{
decryptKey = Utils.GetSubString(decryptKey, 8, "");//"";//
decryptKey = decryptKey.PadRight(8, ' ');
byte[] rgbKey = Encoding.UTF8.GetBytes(decryptKey);
byte[] rgbIV = IV;
byte[] inputByteArray = Convert.FromBase64String(decryptString);
DESCryptoServiceProvider DCSP = new DESCryptoServiceProvider();
MemoryStream mStream = new MemoryStream();
CryptoStream cStream = new CryptoStream(mStream, DCSP.CreateDecryptor(rgbKey, rgbIV), CryptoStreamMode.Write);
cStream.Write(inputByteArray, 0, inputByteArray.Length);
cStream.FlushFinalBlock();
return Encoding.UTF8.GetString(mStream.ToArray());
}
catch
{
return "";
}
}
}
/// <summary>
/// MD5加密
/// </summary>
public class MD5EnCode
{
public static string Encode(string Pwd)
{
MD5 md5 = new MD5CryptoServiceProvider();
byte[] data = System.Text.Encoding.Default.GetBytes(Pwd );
byte[] result = md5.ComputeHash(data);
String ret = "";
for (int i = 0; i < result.Length; i++)
ret += result[i].ToString("x").PadLeft(2, '0');
return ret;
}
}
}