using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Security.Cryptography;
using System.IO;
namespace JHEMR.JHOODCommonLib.Utility
{
public static class SecurityDES
{
private static byte[] Keys = { 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF };
public static string EncryptDES(string encryptString, string encryptKey)
{
try
{
byte[] rgbKey = Encoding.UTF8.GetBytes(encryptKey.Substring(0, 8));
byte[] rgbIV = Keys;
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());
}
catch (Exception ex)
{
MessageBoxHelper.ShowInfo(ex.Message.ToString());
return string.Empty;
}
}
public static string DecryptDES(string decryptString, string decryptKey)
{
try
{
byte[] rgbKey = Encoding.UTF8.GetBytes(decryptKey);
byte[] rgbIV = Keys;
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 (Exception ex)
{
MessageBoxHelper.ShowInfo(ex.Message.ToString());
return string.Empty;
}
}
}
}
private void btn_leadingout_Click(object sender, EventArgs e)
{
string localFilePath = "";
SaveFileDialog sfd = new SaveFileDialog();
sfd.Filter = "XML(*.XML)|*.XML";
sfd.FilterIndex = 1;
sfd.RestoreDirectory = true;
if (sfd.ShowDialog() == DialogResult.OK)
{
localFilePath = sfd.FileName.ToString();
try
{
StreamWriter sw = new StreamWriter(localFilePath, false);
if (_dtDiagnosis.Rows.Count > 0)
{
DataRow[] r= _dtDiagnosis.Select("selected");
if (r.Count() <= 0)
{
MessageBox.Show("请在要导出行的复选框中打钩。");
sw.Close();
return;
}
DataTable dt = r.CopyToDataTable();
dt.TableName = "Diagnosis";
System.IO.TextWriter tw = new System.IO.StringWriter();
dt.WriteXml(tw);
string str = SecurityDES.EncryptDES(tw.ToString(), "12345678");
if(string.IsNullOrEmpty(str))
{
MessageBoxHelper.ShowInfo("导出失败,请重试。");
sw.Close();
return;
}
sw.WriteLine(str);
}
sw.Close();
MessageBoxHelper.ShowInfo("导出成功");
}
catch(Exception f)
{
MessageBoxHelper.ShowInfo("导出失败,请重试");
}
}
}
private void btn_leadingin_Click(object sender, EventArgs e)
{
OpenFileDialog fileDialog = new OpenFileDialog();
fileDialog.Multiselect = true;
fileDialog.Title = "XML";
fileDialog.Filter = "XML(*XML*)|*.XML*";
if (fileDialog.ShowDialog() == DialogResult.OK)
{
string file = fileDialog.FileName;
string strTxtAll = "";
FileStream fs = new FileStream(file, FileMode.Open, FileAccess.Read, FileShare.None);
StreamReader sr = new StreamReader(fs, Encoding.GetEncoding("UTF-8"));
string line = sr.ReadLine();
strTxtAll = line + "\r\n";
for (int ac = 0; line != null; ac++)
{
line = sr.ReadLine();
strTxtAll += line + "\r\n";
}
string xml = SecurityDES.DecryptDES(strTxtAll, "12345678");
if (string.IsNullOrEmpty(xml))
{
MessageBoxHelper.ShowInfo("导入失败,请重试。");
sr.Close();
fs.Close();
return;
}
StringReader stream = null;
XmlTextReader reader = null;
try
{
DataSet xmlDS = new DataSet();
stream = new StringReader(xml);
reader = new XmlTextReader(stream);
xmlDS.ReadXml(reader);
reader.Close();
_bl.DiagnoisLeadingin(xmlDS.Tables[0]);
}
catch (Exception ex)
{
MessageBoxHelper.ShowInfo("导入失败,请重试。");
reader.Close();
}
sr.Close();
fs.Close();
}
}