using System;
|
using System.Collections;
|
using System.Collections.Generic;
|
using System.IO;
|
using System.Linq;
|
using System.Security.Cryptography;
|
using System.Text;
|
using System.Web;
|
using System.Xml;
|
|
namespace VueWebApi.Tools
|
{
|
public class Encrypt
|
{
|
private static RijndaelManaged RMCrypto = null;
|
|
private Hashtable keyList = new Hashtable();
|
|
public static string EncryptStr(string str, string strKey, string strIV)
|
{
|
CryptoStream cryptoStream = null;
|
MD5CryptoServiceProvider mD5CryptoServiceProvider = new MD5CryptoServiceProvider();
|
string result;
|
try
|
{
|
Encrypt.RMCrypto = new RijndaelManaged();
|
if (str == null || str.Trim().Length == 0)
|
{
|
result = "";
|
}
|
else
|
{
|
byte[] rgbKey = mD5CryptoServiceProvider.ComputeHash(Encoding.ASCII.GetBytes(strKey));
|
byte[] rgbIV = mD5CryptoServiceProvider.ComputeHash(Encoding.ASCII.GetBytes(strIV));
|
MemoryStream memoryStream = new MemoryStream(Encoding.UTF8.GetBytes(str));
|
cryptoStream = new CryptoStream(memoryStream, Encrypt.RMCrypto.CreateEncryptor(rgbKey, rgbIV), CryptoStreamMode.Write);
|
result = Convert.ToBase64String(memoryStream.ToArray());
|
}
|
}
|
catch (Exception ex)
|
{
|
throw ex;
|
}
|
finally
|
{
|
if (null != cryptoStream)
|
{
|
cryptoStream = null;
|
}
|
if (null != Encrypt.RMCrypto)
|
{
|
Encrypt.RMCrypto.Clear();
|
Encrypt.RMCrypto = null;
|
}
|
}
|
return result;
|
}
|
|
public static string DecryptStr(string str, string strKey, string strIV)
|
{
|
string result;
|
if (str == null || str.Trim().Length == 0)
|
{
|
result = "";
|
}
|
else
|
{
|
CryptoStream cryptoStream = null;
|
MD5CryptoServiceProvider mD5CryptoServiceProvider = new MD5CryptoServiceProvider();
|
try
|
{
|
Encrypt.RMCrypto = new RijndaelManaged();
|
byte[] buffer = Convert.FromBase64String(str);
|
byte[] rgbKey = mD5CryptoServiceProvider.ComputeHash(Encoding.ASCII.GetBytes(strKey));
|
byte[] rgbIV = mD5CryptoServiceProvider.ComputeHash(Encoding.ASCII.GetBytes(strIV));
|
MemoryStream memoryStream = new MemoryStream(buffer);
|
cryptoStream = new CryptoStream(memoryStream, Encrypt.RMCrypto.CreateDecryptor(rgbKey, rgbIV), CryptoStreamMode.Read);
|
result = Encoding.UTF8.GetString(memoryStream.ToArray());
|
}
|
catch (Exception ex)
|
{
|
throw ex;
|
}
|
finally
|
{
|
if (null != cryptoStream)
|
{
|
cryptoStream = null;
|
}
|
if (null != Encrypt.RMCrypto)
|
{
|
Encrypt.RMCrypto.Clear();
|
Encrypt.RMCrypto = null;
|
}
|
}
|
}
|
return result;
|
}
|
|
public void Add(string key, string value)
|
{
|
if (this.keyList.ContainsKey(key))
|
{
|
this.keyList[key] = value;
|
}
|
else
|
{
|
this.keyList.Add(key, value);
|
}
|
}
|
|
public string GetEncryptXml(string name, string key, string iv)
|
{
|
XmlDocument xmlDocument = new XmlDocument();
|
XmlElement xmlElement = xmlDocument.CreateElement(name);
|
foreach (object current in this.keyList.Keys)
|
{
|
XmlElement xmlElement2 = xmlDocument.CreateElement(Convert.ToString(current));
|
xmlElement2.InnerText = Convert.ToString(this.keyList[current]);
|
xmlElement.AppendChild(xmlElement2);
|
}
|
xmlDocument.AppendChild(xmlElement);
|
return Encrypt.EncryptStr(xmlDocument.OuterXml, key, iv);
|
}
|
|
public Hashtable GetKeyList(string name, string encryptStr, string key, string iv)
|
{
|
string xml = Encrypt.DecryptStr(encryptStr, key, iv);
|
XmlDocument xmlDocument = new XmlDocument();
|
xmlDocument.LoadXml(xml);
|
this.keyList.Clear();
|
XmlNode xmlNode = xmlDocument.SelectSingleNode(name);
|
if (null != xmlNode)
|
{
|
foreach (XmlNode xmlNode2 in xmlNode.ChildNodes)
|
{
|
this.keyList.Add(xmlNode2.Name, xmlNode2.InnerText);
|
}
|
}
|
return this.keyList;
|
}
|
}
|
}
|