using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Security;
using System.Security.Cryptography;
using System.Xml;
namespace 測試加密解密
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
/// <summary>
/// 加密
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void button1_Click(object sender, EventArgs e)
{
string sSourceString = textBox1.Text;
string sResult = this.EncryptDes(sSourceString, "");
textBox2.Text = sResult;
}
private void button2_Click(object sender, EventArgs e)
{
string sSourceString = textBox1.Text;
string sResult = this.DecryptDes(sSourceString, "yqj");
textBox2.Text = sResult;
}
/// <summary>
/// 解密字符串
/// </summary>
/// <param name="SourceStr"></param>
/// <param name="DESkey"></param>
/// <returns></returns>
public string EncryptDes(string SourceStr, string DESkey)
{
DESCryptoServiceProvider des = new DESCryptoServiceProvider();
des.Mode = CipherMode.ECB;
des.Key = System.Text.Encoding.UTF8.GetBytes(DESkey);
des.IV = System.Text.Encoding.UTF8.GetBytes(DESkey);
byte[] inputByteArray = System.Text.Encoding.Default.GetBytes(SourceStr);
MemoryStream ms = new MemoryStream();
CryptoStream cs = new CryptoStream(ms, des.CreateEncryptor(), CryptoStreamMode.Write);
StreamWriter sw = new System.IO.StreamWriter(cs);
sw.Write(SourceStr);
sw.Flush();
cs.FlushFinalBlock();
ms.Flush();
int ss = Convert.ToInt32(ms.Length.ToString());
return Convert.ToBase64String(ms.GetBuffer(), 0, ss);
}
/// <summary>
/// 解密字符串
/// </summary>
/// <param name="SourceStr"></param>
/// <param name="DESkey"></param>
/// <returns></returns>
public string DecryptDes(string SourceStr, string DESkey)
{
DESCryptoServiceProvider des = new DESCryptoServiceProvider();
des.Mode = CipherMode.ECB;
des.Key = System.Text.Encoding.UTF8.GetBytes(DESkey);
des.IV = System.Text.Encoding.UTF8.GetBytes(DESkey);
byte[] inputByteArray = Convert.FromBase64String(SourceStr);
MemoryStream ms = new MemoryStream();
CryptoStream cs = new CryptoStream(ms, des.CreateDecryptor(), CryptoStreamMode.Write);
cs.Write(inputByteArray, 0, inputByteArray.Length);
cs.FlushFinalBlock();
string rt = System.Text.Encoding.UTF8.GetString(ms.ToArray());
return rt;
}
private void button3_Click(object sender, EventArgs e)
{
XmlElement theBook = null;
XmlElement theElem = null;
XmlElement root = null;
XmlDocument xmldoc = new XmlDocument();
xmldoc.Load("mydata.xml");
root = xmldoc.DocumentElement;
//--- 新建一本书开始 ----
theBook = xmldoc.CreateElement("mydata");
theElem = xmldoc.CreateElement("userid");
theElem.InnerText = "K1311P99";
theBook.AppendChild(theElem);
theElem = xmldoc.CreateElement("password");
theElem.InnerText = "123456789";
theBook.AppendChild(theElem);
root.AppendChild(theBook);
xmldoc.Save("mydata.xml");
}
private void button4_Click(object sender, EventArgs e)
{
XmlDocument xmldoc = new XmlDocument();
xmldoc.Load("mydata.xml");
XmlNodeList nodeList = xmldoc.SelectSingleNode("mydatas").ChildNodes;
foreach (XmlNode childNode in nodeList)
{
XmlElement childElement = (XmlElement)childNode;
if (childElement.GetAttribute("userid") == "亚胜")//如果genre属性值为“张三”
{
MessageBox.Show(childElement.Name);
MessageBox.Show(childElement.Value);
}
XmlNodeList nls = childElement.ChildNodes;//继续获取xe子节点的所有子节点
foreach (XmlNode xn1 in nls)//遍历
{
XmlElement xe2 = (XmlElement)xn1;//转换类型
//如果找到帳號为K1311P99的节点
if (xe2.Name == "userid")//如果找到
{
xe2.InnerText = "亚胜";//则修改
}
}
}
xmldoc.Save("mydata.xml");
}
private void button5_Click(object sender, EventArgs e)
{
}
}
}
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Security;
using System.Security.Cryptography;
using System.Xml;
namespace 測試加密解密
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
/// <summary>
/// 加密
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void button1_Click(object sender, EventArgs e)
{
string sSourceString = textBox1.Text;
string sResult = this.EncryptDes(sSourceString, "");
textBox2.Text = sResult;
}
private void button2_Click(object sender, EventArgs e)
{
string sSourceString = textBox1.Text;
string sResult = this.DecryptDes(sSourceString, "yqj");
textBox2.Text = sResult;
}
/// <summary>
/// 解密字符串
/// </summary>
/// <param name="SourceStr"></param>
/// <param name="DESkey"></param>
/// <returns></returns>
public string EncryptDes(string SourceStr, string DESkey)
{
DESCryptoServiceProvider des = new DESCryptoServiceProvider();
des.Mode = CipherMode.ECB;
des.Key = System.Text.Encoding.UTF8.GetBytes(DESkey);
des.IV = System.Text.Encoding.UTF8.GetBytes(DESkey);
byte[] inputByteArray = System.Text.Encoding.Default.GetBytes(SourceStr);
MemoryStream ms = new MemoryStream();
CryptoStream cs = new CryptoStream(ms, des.CreateEncryptor(), CryptoStreamMode.Write);
StreamWriter sw = new System.IO.StreamWriter(cs);
sw.Write(SourceStr);
sw.Flush();
cs.FlushFinalBlock();
ms.Flush();
int ss = Convert.ToInt32(ms.Length.ToString());
return Convert.ToBase64String(ms.GetBuffer(), 0, ss);
}
/// <summary>
/// 解密字符串
/// </summary>
/// <param name="SourceStr"></param>
/// <param name="DESkey"></param>
/// <returns></returns>
public string DecryptDes(string SourceStr, string DESkey)
{
DESCryptoServiceProvider des = new DESCryptoServiceProvider();
des.Mode = CipherMode.ECB;
des.Key = System.Text.Encoding.UTF8.GetBytes(DESkey);
des.IV = System.Text.Encoding.UTF8.GetBytes(DESkey);
byte[] inputByteArray = Convert.FromBase64String(SourceStr);
MemoryStream ms = new MemoryStream();
CryptoStream cs = new CryptoStream(ms, des.CreateDecryptor(), CryptoStreamMode.Write);
cs.Write(inputByteArray, 0, inputByteArray.Length);
cs.FlushFinalBlock();
string rt = System.Text.Encoding.UTF8.GetString(ms.ToArray());
return rt;
}
private void button3_Click(object sender, EventArgs e)
{
XmlElement theBook = null;
XmlElement theElem = null;
XmlElement root = null;
XmlDocument xmldoc = new XmlDocument();
xmldoc.Load("mydata.xml");
root = xmldoc.DocumentElement;
//--- 新建一本书开始 ----
theBook = xmldoc.CreateElement("mydata");
theElem = xmldoc.CreateElement("userid");
theElem.InnerText = "K1311P99";
theBook.AppendChild(theElem);
theElem = xmldoc.CreateElement("password");
theElem.InnerText = "123456789";
theBook.AppendChild(theElem);
root.AppendChild(theBook);
xmldoc.Save("mydata.xml");
}
private void button4_Click(object sender, EventArgs e)
{
XmlDocument xmldoc = new XmlDocument();
xmldoc.Load("mydata.xml");
XmlNodeList nodeList = xmldoc.SelectSingleNode("mydatas").ChildNodes;
foreach (XmlNode childNode in nodeList)
{
XmlElement childElement = (XmlElement)childNode;
if (childElement.GetAttribute("userid") == "亚胜")//如果genre属性值为“张三”
{
MessageBox.Show(childElement.Name);
MessageBox.Show(childElement.Value);
}
XmlNodeList nls = childElement.ChildNodes;//继续获取xe子节点的所有子节点
foreach (XmlNode xn1 in nls)//遍历
{
XmlElement xe2 = (XmlElement)xn1;//转换类型
//如果找到帳號为K1311P99的节点
if (xe2.Name == "userid")//如果找到
{
xe2.InnerText = "亚胜";//则修改
}
}
}
xmldoc.Save("mydata.xml");
}
private void button5_Click(object sender, EventArgs e)
{
}
}
}