DES加密与机密(C#)
来源:互联网 发布:阿里云短信模板 编辑:程序博客网 时间:2024/05/17 04:43
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
}
}
- DES加密与机密(C#)
- DES 加密 与c#。ios 通用
- DES加密与解密(c语言版)
- DES加密模板,和C#des加密
- C#DES加密解密
- C#DES加密方法
- C#DES加密解密
- C#DES加密
- C#DES加密解密
- C#Des加密&解密
- iOS DES加密与3DES加密
- Des加密与解密
- DES加密与解密
- asp.net(C#) 与 flash 传递参数DES加密解密
- C#DES加密和解密
- C#DES加密和解密
- c语言实现des加密
- DES加密解密C/C++
- 设计模式原则详解
- 1.4 在Ant中编译Java代码
- 解决Ubuntu发热量大的问题
- 黑马程序员————高新技术————内省(了解JavaBean)
- 对于安卓应用开发插入广告的总结
- DES加密与机密(C#)
- protobuf 常用序列化/反序列化 api
- Unix下C语言用于记录日志的方法(自定义)
- Ubuntu下adb在不到Android设备(windows的类似)
- 在排好序的数组中找出给定数字出现的次数
- ireport 合并行
- android 获得root权限解密
- Show IplImage on MFC Window
- Oracle表的一些基本操作