?? 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 + -