using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Text;
|
|
using System.Security.Cryptography;
|
|
using System.IO;
|
|
|
|
namespace Utils.Security
|
|
{
|
|
/// <summary>
|
|
/// DES加密。方法二
|
|
/// </summary>
|
|
public class DES2
|
|
{
|
|
/// <summary>
|
|
/// 加密字符串
|
|
/// </summary>
|
|
public string EncryptString(string pToEncrypt, string sKey)//加密字符串
|
|
{
|
|
var des = new DESCryptoServiceProvider();
|
|
var inputByteArray = Encoding.Default.GetBytes(pToEncrypt);
|
|
des.Key = ASCIIEncoding.ASCII.GetBytes(sKey);
|
|
des.IV = ASCIIEncoding.ASCII.GetBytes(sKey);
|
|
var ms = new MemoryStream();
|
|
var cs = new CryptoStream(ms, des.CreateEncryptor(), CryptoStreamMode.Write);
|
|
cs.Write(inputByteArray, 0, inputByteArray.Length);
|
|
cs.FlushFinalBlock();
|
|
var ret = new StringBuilder();
|
|
foreach (byte b in ms.ToArray()) {
|
|
ret.AppendFormat("{0:X2}", b);
|
|
}
|
|
return ret.ToString();
|
|
}
|
|
|
|
/// <summary>
|
|
/// 解密字符串
|
|
/// </summary>
|
|
public string DecryptString(string pToDecrypt, string sKey)//解密字符串
|
|
{
|
|
var des = new DESCryptoServiceProvider();
|
|
|
|
var inputByteArray = new byte[pToDecrypt.Length / 2];
|
|
for (int x = 0; x < pToDecrypt.Length / 2; x++) {
|
|
int i = (Convert.ToInt32(pToDecrypt.Substring(x * 2, 2), 16));
|
|
inputByteArray[x] = (byte)i;
|
|
}
|
|
|
|
des.Key = ASCIIEncoding.ASCII.GetBytes(sKey);
|
|
des.IV = ASCIIEncoding.ASCII.GetBytes(sKey);
|
|
var ms = new MemoryStream();
|
|
var cs = new CryptoStream(ms, des.CreateDecryptor(), CryptoStreamMode.Write);
|
|
cs.Write(inputByteArray, 0, inputByteArray.Length);
|
|
cs.FlushFinalBlock();
|
|
|
|
return System.Text.Encoding.Default.GetString(ms.ToArray());
|
|
}
|
|
|
|
|
|
}
|
|
}
|