一个通用的字符串加密和编码工具类
来源:互联网 发布:小超市销售软件 编辑:程序博客网 时间:2024/06/05 17:31
public static class CryptoHelper
{
#region Private field
/// <summary>
/// Hash algorithm
/// </summary>
private static SHA1 sha;
/// <summary>
/// Crypto transformer of encryption
/// </summary>
private static ICryptoTransform et;
/// <summary>
/// Crypto transformer of decryption
/// </summary>
private static ICryptoTransform dt;
#endregion
#region Constructor
/// <summary>
/// ¹¹Ô캯Êý¡£
/// </summary>
/// <remarks>ÎÞ²ÎÊýµÄ¹¹Ô캯Êý£¬ÓÃĬÈϵķ½Ê½³õʼ»¯¸÷¸öÊôÐÔ¡£
/// </remarks>
static CryptoHelper()
{
CryptoHelper.sha = new SHA1CryptoServiceProvider();
DESCryptoServiceProvider cryptoService = new DESCryptoServiceProvider();
byte[] cryptoKey = { 136, 183, 142, 217, 175, 71, 90, 239 };
byte[] cryptoIV = { 227, 105, 5, 40, 162, 158, 143, 156 };
cryptoService.Key = cryptoKey;
cryptoService.IV = cryptoIV;
CryptoHelper.et = cryptoService.CreateEncryptor();
CryptoHelper.dt = cryptoService.CreateDecryptor();
}
#endregion
#region hash / comparehash
private static byte[] CreateHash(byte[] plaintext)
{
return CryptoHelper.sha.ComputeHash(plaintext);
}
public static string CreateHash(string plaintext)
{
byte[] plainTextBytes = UnicodeEncoding.Unicode.GetBytes(plaintext);
byte[] resultBytes = CreateHash(plainTextBytes);
return Convert.ToBase64String(resultBytes);
}
private static bool HashCheck(byte[] plaintext, byte[] hashedText)
{
if ((hashedText == null) || (hashedText.Length <= 0))
return false;
byte[] hashedResult = CryptoHelper.sha.ComputeHash(plaintext);
if (hashedText.Length != hashedResult.Length)
return false;
for (int i = 0; i < hashedResult.Length; i++)
{
if (hashedText[i] != hashedResult[i])
return false;
}
return true;
}
public static bool HashCheck(string plaintext, string hashedText)
{
byte[] plainTextBytes = UnicodeEncoding.Unicode.GetBytes(plaintext);
byte[] hashedTextBytes = Convert.FromBase64String(hashedText);
bool result = HashCheck(plainTextBytes, hashedTextBytes);
return result;
}
#endregion
#region nvative key encrypt / decrypt
public static string Decrypt(string source)
{
byte[] buff = Convert.FromBase64String(source);
using (MemoryStream mem = new MemoryStream())
{
using (CryptoStream stream = new CryptoStream(mem, CryptoHelper.dt, CryptoStreamMode.Write))
{
stream.Write(buff, 0, buff.Length);
stream.Close();
}
return Encoding.Unicode.GetString(mem.ToArray());
}
}
public static string Encrypt(string source)
{
byte[] buff = Encoding.Unicode.GetBytes(source);
MemoryStream mem = new MemoryStream();
CryptoStream stream = new CryptoStream(mem, CryptoHelper.et, CryptoStreamMode.Write);
stream.Write(buff, 0, buff.Length);
stream.FlushFinalBlock();
stream.Clear();
return Convert.ToBase64String(mem.ToArray());
}
#endregion
#region encode / decode
private const string CodePageName = "utf-32";
public static string Encode(string source)
{
byte[] buffer = Encoding.GetEncoding(CodePageName).GetBytes(source);
return Convert.ToBase64String(buffer);
}
public static string Decode(string source)
{
byte[] buffer = Convert.FromBase64String(source);
return Encoding.GetEncoding(CodePageName).GetString(buffer);
}
#endregion
}
- 一个通用的字符串加密和编码工具类
- Java 和 C#通用的DES加密工具类
- 一个通用的加密、解密类
- 文件和字符串的加密工具类md5
- 通用工具类-Base64Encoder加密
- 处理编码和日期的通用字符串转换方法
- Java 通用的DES加密工具类的实现
- 一个在DotNet下和Java下都通用的加密类!
- 常用的字符串加密解密工具类
- 用Java提供的工具类对一个字符串进行加密
- AppEncrypts helper.提供加密解密的一个通用类
- 字符串加密,解密,利用 sun.misc.BASE64Decode类的 encode(byte)编码和decodeBuffer(str)解码
- 一个通用的java集合和json互转的工具类
- C#PDU编码8BIT加密解密函数和短信GSM加密解密用到的不错的一个类
- C#PDU编码8BIT加密解密函数和短信GSM加密解密用到的不错的一个类
- java 中字符串加密解密 字符编码转换 有利于url 的加密和解密
- 字符串加密解密工具类
- 一个加密图像的工具
- 小弟求助:关于自定义web控件的属性问题。。大虾请进。。
- 第四章 输出文字part2
- Java Bean原理与应用
- 悠远的道路往前伸
- fstab的理解与e2label
- 一个通用的字符串加密和编码工具类
- 获取程序运行路径的几种方法
- ASP.net中的AJAX学习记录--体会UpdateMode属性及使用UpdateTriggers
- 前台控制提交按钮事件(防止多次提交)
- 奇怪的错误!
- asp.net 当前页 关闭 父类页
- auto_ptr 基本用法
- 程序的链接和装入及Linux下动态链接的实现 收藏
- WinForm控件开发总结目录