工作中拓展的加密解密传输方式. DES对称加密传输.
系统间通过xml传输, 不能采用明文, 就加密传输. 秘钥(真正有效的是前8位)存储于配置中.
public static string EncryptStr(this string content, string desKey)
{
string result;
try
{
if (string.IsNullOrEmpty(desKey))
{
result = content;
}
else
{
DESCryptoServiceProvider dESCryptoServiceProvider = new DESCryptoServiceProvider();
dESCryptoServiceProvider.Mode = CipherMode.ECB;
byte[] array = new byte[8];
if (desKey.Length < 8)
{
byte[] bytes = Encoding.UTF8.GetBytes(desKey);
for (int i = 0; i < array.Length; i++)
{
if (bytes.Length > i)
{
array[i] = bytes[i];
}
else
{
array[i] = 0;
}
}
}
else
{
array = Encoding.UTF8.GetBytes(desKey.Substring(0, 8));
}
dESCryptoServiceProvider.Key = array;
dESCryptoServiceProvider.IV = dESCryptoServiceProvider.Key;
byte[] bytes2 = Encoding.UTF8.GetBytes(content);
MemoryStream memoryStream = new MemoryStream();
CryptoStream cryptoStream = new CryptoStream(memoryStream, dESCryptoServiceProvider.CreateEncryptor(), CryptoStreamMode.Write);
cryptoStream.Write(bytes2, 0, bytes2.Length);
cryptoStream.FlushFinalBlock();
StringBuilder stringBuilder = new StringBuilder();
byte[] array2 = memoryStream.ToArray();
for (int j = 0; j < array2.Length; j++)
{
byte b = array2[j];
stringBuilder.Append(b.ToString());
stringBuilder.Append("_");
}
if (stringBuilder.Length > 0)
{
stringBuilder = stringBuilder.Remove(stringBuilder.Length - 1, 1);
}
result = stringBuilder.ToString();
}
}
catch (Exception)
{
result = content;
}
return result;
}
--- 如下解密方法:
public static string DecryptStr(this string content, string desKey)
{
string result;
try
{
if (string.IsNullOrEmpty(desKey))
{
result = content;
}
else
{
DESCryptoServiceProvider dESCryptoServiceProvider = new DESCryptoServiceProvider();
dESCryptoServiceProvider.Mode = CipherMode.ECB;
byte[] array = new byte[8];
if (desKey.Length < 8)
{
byte[] bytes = Encoding.UTF8.GetBytes(desKey);
for (int i = 0; i < array.Length; i++)
{
if (bytes.Length > i)
{
array[i] = bytes[i];
}
else
{
array[i] = 0;
}
}
}
else
{
array = Encoding.UTF8.GetBytes(desKey.Substring(0, 8));
}
dESCryptoServiceProvider.Key = array;
dESCryptoServiceProvider.IV = dESCryptoServiceProvider.Key;
string[] array2 = content.Split(new char[]
{
'_'
});
byte[] array3 = new byte[array2.Length];
for (int j = 0; j < array2.Length; j++)
{
array3[j] = Convert.ToByte(array2[j]);
}
MemoryStream memoryStream = new MemoryStream();
CryptoStream cryptoStream = new CryptoStream(memoryStream, dESCryptoServiceProvider.CreateDecryptor(), CryptoStreamMode.Write);
cryptoStream.Write(array3, 0, array3.Length);
cryptoStream.FlushFinalBlock();
Encoding encoding = new UTF8Encoding();
result = encoding.GetString(memoryStream.ToArray());
}
}
catch (Exception)
{
result = "";
}
return result;
}
相应的调用发送WebRequest 方法:
//<add key="DataPushEncryptKey" value="XXXXXXXXXX"/>
//POST 方式, 为了兼容可能的Get传输. 接收XML参数.
if (!string.IsNullOrEmpty(XML))
XML = Request.Params["XML"];
//如果设置了加密key则加密数据
if (!string.IsNullOrEmpty(XXX.DataPushEncryptKey))
XML = XML.DecryptStr(XXX.DataPushEncryptKey);
//<add key="pushDataToXXX" value="XXX/XXX/XXX/XXXXX"/>
// <add key="EncryptKey" value="xxxxxxxxxxx"/>
--如下同步发送方法:
public static string CreatePostXXXHttpResponse(string data)
{
string backstr = string.Empty; ;
try
{
//获取发送地址
string url = ConfigurationManager.AppSettings["pushDataToXXX"];
string allStr = System.Web.HttpUtility.UrlEncode(data, System.Text.Encoding.UTF8);
//如果设置了加密key则加密数据
if (!string.IsNullOrEmpty(XXXEncryptKey))
allStr = allStr.EncryptStr(XXXEncryptKey);
//把参数转成byte
byte[] bufferB = Encoding.UTF8.GetBytes(allStr);
//构建HttpWebRequest,并发送
System.Net.ServicePointManager.DefaultConnectionLimit = 200;
HttpWebRequest WebReq = (HttpWebRequest)WebRequest.Create(url);
WebReq.Method = "POST";
WebReq.ContentType = "application/x-www-form-urlencoded";
WebReq.ContentLength = allStr.Length;
Stream PostData = WebReq.GetRequestStream();
PostData.Write(bufferB, 0, bufferB.Length);
PostData.Close();
//接收返回结果
HttpWebResponse WebResp = (HttpWebResponse)WebReq.GetResponse();
StreamReader sr = new StreamReader(WebResp.GetResponseStream(), System.Text.Encoding.UTF8);
backstr = sr.ReadToEnd();
backstr = HttpUtility.UrlDecode(backstr);
}
catch (Exception e)
{
return backstr + "</br>" + e.Message;
}
return backstr;
}
在未知中徜徉,
求心之荡漾。