using System;
using System.Text;
*****
namespace namespace.Utils.Security
{
public class PackageKey
{
private const string basekey = "%$";
#region DES加密
/// <summary>
/// DES加密
/// </summary>
/// <param name="expressly">明文</param>
/// <param name="keySuffix">密钥后缀</param>
/// <returns>密文字节数组,如为空则加密失败</returns>
public static byte[] EncryptDesToken(string expressly, string keySuffix)
{
string desKey = basekey+ keySuffix;
byte[] keyBytes = Encoding.UTF8.GetBytes(desKey);
byte[] expresslyBytes = Encoding.UTF8.GetBytes(expressly);
try
{
return DesHelper.Encrypt(expresslyBytes, keyBytes);
}
catch (Exception)
{
return null;
}
}
public static byte[] EncryptDesToken(string expressly, string keySuffix,string keyPrefix)
{
string desKey = keyPrefix + keySuffix;
byte [] keyBytes = Encoding.UTF8.GetBytes(desKey);
byte [] expresslyBytes = Encoding.UTF8.GetBytes(expressly);
try
{
return DesHelper.Encrypt(expresslyBytes, keyBytes);
}
catch ( Exception )
{
return null;
}
}
public static byte[] EncryptDesToken(string expressly, string keySuffix,Encoding encode)
{
if(encode==Encoding.UTF8)
{
return EncryptDesToken(expressly, keySuffix);
}
else
{
string desKey = basekey + keySuffix;
byte[] keyBytes = Encoding.UTF8.GetBytes(desKey);
byte[] expresslyBytes;
expresslyBytes = Encoding.Convert(encode, Encoding.UTF8, encode.GetBytes(expressly));
try
{
return DesHelper.Encrypt(expresslyBytes, keyBytes);
}
catch (Exception)
{
return null;
}
}
}
#endregion
#region DES解密
/// <summary>
/// DES解密
/// </summary>
/// <param name="ciphertextBytes">密文字节数组</param>
/// <param name="keySuffix"></param>
/// <returns>明文字节数组,如为空则解密失败</returns>
public static byte[] DecryptDesToken(byte[] ciphertextBytes, string keySuffix)
{
string desKey = basekey+keySuffix;
byte[] keyBytes = Encoding.UTF8.GetBytes(desKey);
try
{
return DesHelper.Decrypt(ciphertextBytes, keyBytes);
}
catch (Exception ex)
{
LoggerHelper.WriteRequestLog(string.Format("解密失败密钥是:" + desKey + ". ex:" + ex.ToString()));
return null;
}
}
#endregion
}
}
using System.Text;
*****
namespace namespace.Utils.Security
{
public class PackageKey
{
private const string basekey = "%$";
#region DES加密
/// <summary>
/// DES加密
/// </summary>
/// <param name="expressly">明文</param>
/// <param name="keySuffix">密钥后缀</param>
/// <returns>密文字节数组,如为空则加密失败</returns>
public static byte[] EncryptDesToken(string expressly, string keySuffix)
{
string desKey = basekey+ keySuffix;
byte[] keyBytes = Encoding.UTF8.GetBytes(desKey);
byte[] expresslyBytes = Encoding.UTF8.GetBytes(expressly);
try
{
return DesHelper.Encrypt(expresslyBytes, keyBytes);
}
catch (Exception)
{
return null;
}
}
public static byte[] EncryptDesToken(string expressly, string keySuffix,string keyPrefix)
{
string desKey = keyPrefix + keySuffix;
byte [] keyBytes = Encoding.UTF8.GetBytes(desKey);
byte [] expresslyBytes = Encoding.UTF8.GetBytes(expressly);
try
{
return DesHelper.Encrypt(expresslyBytes, keyBytes);
}
catch ( Exception )
{
return null;
}
}
public static byte[] EncryptDesToken(string expressly, string keySuffix,Encoding encode)
{
if(encode==Encoding.UTF8)
{
return EncryptDesToken(expressly, keySuffix);
}
else
{
string desKey = basekey + keySuffix;
byte[] keyBytes = Encoding.UTF8.GetBytes(desKey);
byte[] expresslyBytes;
expresslyBytes = Encoding.Convert(encode, Encoding.UTF8, encode.GetBytes(expressly));
try
{
return DesHelper.Encrypt(expresslyBytes, keyBytes);
}
catch (Exception)
{
return null;
}
}
}
#endregion
#region DES解密
/// <summary>
/// DES解密
/// </summary>
/// <param name="ciphertextBytes">密文字节数组</param>
/// <param name="keySuffix"></param>
/// <returns>明文字节数组,如为空则解密失败</returns>
public static byte[] DecryptDesToken(byte[] ciphertextBytes, string keySuffix)
{
string desKey = basekey+keySuffix;
byte[] keyBytes = Encoding.UTF8.GetBytes(desKey);
try
{
return DesHelper.Decrypt(ciphertextBytes, keyBytes);
}
catch (Exception ex)
{
LoggerHelper.WriteRequestLog(string.Format("解密失败密钥是:" + desKey + ". ex:" + ex.ToString()));
return null;
}
}
#endregion
}
}