pkey加密使用的秘钥
bIV加密使用的偏移向量
using System.Security.Cryptography;
static string pkey = "WELCOMEGUY";
static byte[] bIV = { 0x00, 0x00, 0x00, 0x00, (byte)0x00, (byte)0xXX, (byte)0xOO, (byte)0xXO };
/// <summary>
/// DES加密
/// </summary>
/// <param name="str">需要加密的</param>
/// <returns></returns>
private static string Encrypt(string str)
{
byte[] inputByteArray = Encoding.UTF8.GetBytes(str);
DESCryptoServiceProvider des = new DESCryptoServiceProvider();
MemoryStream ms = new MemoryStream();
CryptoStream cs=new CryptoStream(
ms,
des.CreateEncryptor(Encoding.UTF8.GetBytes(pkey), bIV),
CryptoStreamMode.Write
);
cs.Write(inputByteArray, 0, inputByteArray.Length);
cs.FlushFinalBlock();
return Convert.ToBase64String(ms.ToArray());
}
/// <summary>
/// DES解密
/// </summary>
/// <param name="pToDecrypt">需要解密的</param>
/// <returns></returns>
private static string Decrypt(string pToDecrypt)
{
DESCryptoServiceProvider des = new DESCryptoServiceProvider();
byte[] inputByteArray = Convert.FromBase64String(pToDecrypt);
MemoryStream ms = new MemoryStream();
CryptoStream cs = new CryptoStream(
ms,
des.CreateDecryptor(Encoding.UTF8.GetBytes(pkey), bIV),
CryptoStreamMode.Write
);
cs.Write(inputByteArray, 0, inputByteArray.Length);
cs.FlushFinalBlock();
return Encoding.UTF8.GetString(ms.ToArray());
}