📄 xmlcomponent.cs
字号:
using System;
using System.Data;
using System.Data.SqlClient;
using System.Xml;
using System.Security;
using System.Security.Cryptography;
using System.Text;
using System.IO;
using System.Web;
using System.Web.UI.WebControls;
namespace Example_8_8
{
/// <summary>
/// Summary description for XMLComponent.
/// </summary>
public class XMLComponent
{
public static String DataFile = "bin/XMLDataFile.xml";
public DataSet GetDataFromXml(int nUserID,String xmlDataFile)
{
DataSet ds = new DataSet();
DataTable dataTable = new DataTable("Account");
dataTable.Columns.Add("消费主题",typeof(string));
dataTable.Columns.Add("消费现金(单位:元)",typeof(Decimal));
dataTable.Columns.Add("消费时间",typeof(DateTime));
dataTable.Columns.Add("消费信息详细说明",typeof(string));
dataTable.Columns.Add("消费类型",typeof(string));
try
{
SymmetricMethodData symmetric = new SymmetricMethodData();
XmlDocument accountDoc = new XmlDocument();
accountDoc.Load(xmlDataFile);
XmlNodeList nodeList = accountDoc.SelectNodes("/Accounts/Account[@UserID=" + nUserID.ToString() + "]");
int index = 0;
foreach(XmlNode node in nodeList)
{
if(index < 15)
{
DataRow row = dataTable.NewRow();
row["消费主题"] = symmetric.DecryptoData(node.Attributes["Title"].Value);
row["消费现金(单位:元)"] = Convert.ToDecimal(symmetric.DecryptoData(node.Attributes["Money"].Value));
row["消费时间"] = Convert.ToDateTime(node.Attributes["Pubdate"].Value);
row["消费信息详细说明"] = symmetric.DecryptoData(node.Attributes["OtherInfo"].Value);
row["消费类型"] = symmetric.DecryptoData(node.Attributes["AccountType"].Value);
dataTable.Rows.Add(row);
}
index++;
}
}
catch(Exception ex)
{
String exMessage = ex.Message;
}
ds.Tables.Add(dataTable);
return(ds);
}
}
/// <summary>
/// 对称加密类的构造函数
/// </summary>
public class SymmetricMethodData
{
private SymmetricAlgorithm mobjCryptoService;
private string Key;
public SymmetricMethodData()
{
mobjCryptoService = new RijndaelManaged();
Key = "Guz(%&hj7x89H$yuBI0456FtmaT5&fvHUFCy76*h%(HilJ$lhj!y6&(*jkP87jH7";
}
/// <summary>
/// 获得密钥
/// </summary>
/// <returns>密钥</returns>
private byte[] GetLegalKey()
{
string sTemp = Key;
mobjCryptoService.GenerateKey();
byte[] bytTemp = mobjCryptoService.Key;
int KeyLength = bytTemp.Length;
if (sTemp.Length > KeyLength)
sTemp = sTemp.Substring(0, KeyLength);
else if (sTemp.Length < KeyLength)
sTemp = sTemp.PadRight(KeyLength, ' ');
return ASCIIEncoding.ASCII.GetBytes(sTemp);
}
/// <summary>
/// 获得初始向量IV
/// </summary>
/// <returns>初试向量IV</returns>
private byte[] GetLegalIV()
{
string sTemp = "E4ghj*Ghg7!rNIfb&95GUY86GfghUb#er57HBh(u%g6HJ($jhWk7&!hg4ui%$hjk";
mobjCryptoService.GenerateIV();
byte[] bytTemp = mobjCryptoService.IV;
int IVLength = bytTemp.Length;
if (sTemp.Length > IVLength)
sTemp = sTemp.Substring(0, IVLength);
else if (sTemp.Length < IVLength)
sTemp = sTemp.PadRight(IVLength, ' ');
return ASCIIEncoding.ASCII.GetBytes(sTemp);
}
/// <summary>
/// 加密方法
/// </summary>
/// <param name="Source">待加密的串</param>
/// <returns>经过加密的串</returns>
public string EncryptoData(string Source)
{
byte[] bytIn = UTF8Encoding.UTF8.GetBytes(Source);
MemoryStream ms = new MemoryStream();
mobjCryptoService.Key = GetLegalKey();
mobjCryptoService.IV = GetLegalIV();
ICryptoTransform encrypto = mobjCryptoService.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>
/// 解密方法
/// </summary>
/// <param name="Source">待解密的串</param>
/// <returns>经过解密的串</returns>
public string DecryptoData(string Source)
{
byte[] bytIn = Convert.FromBase64String(Source);
MemoryStream ms = new MemoryStream(bytIn, 0, bytIn.Length);
mobjCryptoService.Key = GetLegalKey();
mobjCryptoService.IV = GetLegalIV();
ICryptoTransform encrypto = mobjCryptoService.CreateDecryptor();
CryptoStream cs = new CryptoStream(ms, encrypto, CryptoStreamMode.Read);
StreamReader sr = new StreamReader(cs);
return sr.ReadToEnd();
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -