You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

65 lines
2.2 KiB

using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
namespace Tiobon.Core.OPS.Tool.OPS.Tool.Helper
{
/// <summary>
/// DES加密解密
/// </summary>
public class DESHelper
{
#region 缺省密钥
private static readonly byte[] key = new byte[] { 0x63, 0x45, 0x87, 0x78, 0x68, 0x12, 0x34, 0x23 };
private static readonly byte[] iv = new byte[] { 0x63, 0x45, 0x87, 0x78, 0x68, 0x12, 0x34, 0x23 };
#endregion
#region 使用缺省密钥加密、解密字符串,并返回字符串
/// <summary>
/// 使用缺省密钥加密字符串
/// </summary>
/// <param name="sInputString">需要加密的字符串</param>
/// <returns>加密后字符串</returns>
public static string EncryptString(string sInputString)
{
byte[] data = Encoding.UTF8.GetBytes(sInputString);
DESCryptoServiceProvider DES = new DESCryptoServiceProvider
{
Key = key,
IV = iv
};
ICryptoTransform desencrypt = DES.CreateEncryptor();
byte[] result = desencrypt.TransformFinalBlock(data, 0, data.Length);
return BitConverter.ToString(result);
}
/// <summary>
/// 使用缺省密钥解密字符串
/// </summary>
/// <param name="sInputString">需要解密字符串</param>
/// <returns>解密后字符串</returns>
public static string DecryptString(string sInputString)
{
string[] sInput = sInputString.Split('-');
byte[] data = new byte[sInput.Length];
for (int i = 0; i < sInput.Length; i++)
{
data[i] = byte.Parse(sInput[i], NumberStyles.HexNumber);
}
DESCryptoServiceProvider DES = new DESCryptoServiceProvider
{
Key = key,
IV = iv
};
ICryptoTransform desencrypt = DES.CreateDecryptor();
byte[] result = desencrypt.TransformFinalBlock(data, 0, data.Length);
return Encoding.UTF8.GetString(result);
}
#endregion
}
}