using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Security.Cryptography;
using System.IO;
using System.Web;
namespace Thewolfs.Framework.Text
{
/// <summary>
/// 加密类
/// </summary>
public class TwEncode
{
#region MD5加密类型,0为32位md5加密,1为64位md5加密
/// <summary>
/// 密码加密函数
/// </summary>
/// <param name="encrypType">加密类型,0为32位md5加密,1为64位md5加密</param>
/// <param name="originalStr">加密前字符串</param>
/// <returns>md5加密后的字符串</returns>
private static string EncryptPass(byte encrypType, string originalStr)
{
string strReturn = string.Empty;
switch (encrypType)
{
case 0:
strReturn = System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(originalStr, "MD5");
break;
default:
strReturn = System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(originalStr, "MD5");
break;
}
return strReturn;
//throw new System.NotImplementedException();
}
/// <summary>
/// 32位的MD5加密
/// </summary>
/// <param name="toCryString"></param>
/// <returns></returns>
public static string EncodeByMD5_32(string toCryString)
{
return TwEncode.EncryptPass(0, toCryString);
}
/// <summary>
/// 64位的MD5加密
/// </summary>
/// <param name="toCryString"></param>
/// <returns></returns>
public static string EncodeByMD5_64(string toCryString)
{
return TwEncode.EncryptPass(1, toCryString);
}
#endregion
#region Base64加密码 / 解密
/// <summary>
/// 用Base64加密
/// </summary>
/// <param name="strText"></param>
/// <returns></returns>
public static string EncodeByBase64(String strText)
{
return Encrypt(strText, "&%#@?,:*");
}
/// <summary>
/// 解密Base64加密的密码
/// </summary>
/// <param name="strText"></param>
/// <returns></returns>
public static String DecodeByBase64(String strText)
{
return Decrypt(strText, "&%#@?,:*");
}
/// <summary>
/// Base64加密函数
/// </summary>
/// <param name="strText"></param>
/// <param name="strEncrKey"></param>
/// <returns></returns>
private static String Encrypt(String strText, String strEncrKey)
{
Byte[] byKey = { };
Byte[] IV = { 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF };
try
{
byKey = System.Text.Encoding.UTF8.GetBytes(strEncrKey.Substring(0, 8));
DESCryptoServiceProvider des = new DESCryptoServiceProvider();
Byte[] inputByteArray = Encoding.UTF8.GetBytes(strText);
MemoryStream ms = new MemoryStream();
CryptoStream cs = new CryptoStream(ms, des.CreateEncryptor(byKey, IV), CryptoStreamMode.Write);
cs.Write(inputByteArray, 0, inputByteArray.Length);
cs.FlushFinalBlock();
return Convert.ToBase64String(ms.ToArray());
}
catch (Exception ex)
{
return ex.Message + ex.ToString() + ex.Source;
}
}
/// <summary>
/// Base64解密函数
/// </summary>
/// <param name="strText"></param>
/// <param name="sDecrKey"></param>
/// <returns></returns>
private static String Decrypt(String strText, String sDecrKey)
{
Byte[] byKey = { };
Byte[] IV = { 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF };
Byte[] inputByteArray = new byte[strText.Length];
try
{
byKey = System.Text.Encoding.UTF8.GetBytes(sDecrKey.Substring(0, 8));
DESCryptoServiceProvider des = new DESCryptoServiceProvider();
inputByteArray = Convert.FromBase64String(strText);
MemoryStream ms = new MemoryStream();
CryptoStream cs = new CryptoStream(ms, des.CreateDecryptor(byKey, IV), CryptoStreamMode.Write);
cs.Write(inputByteArray, 0, inputByteArray.Length);
cs.FlushFinalBlock();
System.Text.Encoding encoding = System.Text.Encoding.UTF8;
return encoding.GetString(ms.ToArray());
}
catch (Exception ex)
{
return ex.Message + ex.ToString() + ex.Source;
}
}
#endregion
#region SHA1 加密 40位
/// <summary>
/// 使用SHA1加密字符串。 40位
/// </summary>
/// <param name="inputString">输入字符串。</param>
/// <returns>加密后的字符串。(40个字符)</returns>
public static string EncodeBySHA1(string inputString)
{
SHA1CryptoServiceProvider sha1 = new SHA1CryptoServiceProvider();
byte[] encryptedBytes = sha1.ComputeHash(Encoding.ASCII.GetBytes(inputString));
StringBuilder sb = new StringBuilder();
for (int i = 0; i < encryptedBytes.Length; i++)
{
sb.AppendFormat("{0:x2}", encryptedBytes[i]);
}
return sb.ToString();
}
#endregion
#region DES 加密/解密
private static byte[] key = ASCIIEncoding.ASCII.GetBytes("caikelun");
private static byte[] iv = ASCIIEncoding.ASCII.GetBytes("12345678");
/// <summary>
/// DES加密。
/// </summary>
/// <param name="inputString">输入字符串。</param>
/// <returns>加密后的字符串。</returns>
public static string EncodeByDES(string inputString)
{
MemoryStream ms = null;
CryptoStream cs = null;
StreamWriter sw = null;
DESCryptoServiceProvider des = new DESCryptoServiceProvider();
try
{
ms = new MemoryStream();
cs = new CryptoStream(ms, des.CreateEncryptor(key, iv), CryptoStreamMode.Write);
sw = new StreamWriter(cs);
sw.Write(inputString);
sw.Flush();
cs.FlushFinalBlock();
return Convert.ToBase64String(ms.GetBuffer(), 0, (int)ms.Length);
}
finally
{
if (sw != null) sw.Close();
if (cs != null) cs.Close();
if (ms != null) ms.Close();
}
}
/// <summary>
/// DES解密。
/// </summary>
/// <param name="inputString">输入字符串。</param>
/// <returns>解密后的字符串。</returns>
public static string DecodeByDES(string inputString)
{
MemoryStream ms = null;
CryptoStream cs = null;
StreamReader sr = null;
DESCryptoServiceProvider des = new DESCryptoServiceProvider();
try
{
ms = new MemoryStream(Convert.FromBase64String(inputString));
cs = new CryptoStream(ms, des.CreateDecryptor(key, iv), CryptoStreamMode.Read);
sr = new StreamReader(cs);
return sr.ReadToEnd();
}
finally
{
if (sr != null) sr.Close();
if (cs != null) cs.Close();
if (ms != null) ms.Close();
}
}
#endregion
#region HTML & URL地址加密 - .NET的方法
/// <summary>
/// URL地址加密 - .NET的方法
/// </summary>
/// <param name="strUrl">URL地址</param>
/// <returns></returns>
public static string UrlEncode(string strUrl)
{
if (string.IsNullOrEmpty(strUrl))
{
return strUrl;
}
return System.Web.HttpUtility.UrlEncode(strUrl);
}
/// <summary>
/// URL地址解密 - .NET的方法
/// </summary>
/// <param name="strURL">加密过的URL地址</param>
/// <returns></returns>
public static string UrlDecode(string strURL)
{
if (string.IsNullOrEmpty(strURL))
{
return strURL;
}
return System.Web.HttpUtility.UrlEncode(strURL);
}
/// <summary>
/// 返回 HTML 字符串的编码结果
/// </summary>
/// <param name="str">字符串</param>
/// <returns>编码结果</returns>
public static string HtmlEncode(string str)
{
return HttpUtility.HtmlEncode(str);
}
/// <summary>
/// 返回 HTML 字符串的解码结果
/// </summary>
/// <param name="str">字符串</param>
/// <returns>解码结果</returns>
public static string HtmlDecode(string str)
{
return HttpUtility.HtmlDecode(str);
}
#endregion
#region SHA256函数
/// <summary>
/// SHA256函数
/// </summary>
/// <param name="str">原始字符串</param>
/// <returns>SHA256结果</returns>
public static string SHA256(string str)
{
byte[] SHA256Data = Encoding.UTF8.GetBytes(str);
SHA256Managed Sha256 = new SHA256Managed();
byte[] Result = Sha256.ComputeHash(SHA256Data);
return Convert.ToBase64String(Result); //返回长度为44字节的字符串
}
#endregion
}
}
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Security.Cryptography;
using System.IO;
using System.Web;
namespace Thewolfs.Framework.Text
{
/// <summary>
/// 加密类
/// </summary>
public class TwEncode
{
#region MD5加密类型,0为32位md5加密,1为64位md5加密
/// <summary>
/// 密码加密函数
/// </summary>
/// <param name="encrypType">加密类型,0为32位md5加密,1为64位md5加密</param>
/// <param name="originalStr">加密前字符串</param>
/// <returns>md5加密后的字符串</returns>
private static string EncryptPass(byte encrypType, string originalStr)
{
string strReturn = string.Empty;
switch (encrypType)
{
case 0:
strReturn = System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(originalStr, "MD5");
break;
default:
strReturn = System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(originalStr, "MD5");
break;
}
return strReturn;
//throw new System.NotImplementedException();
}
/// <summary>
/// 32位的MD5加密
/// </summary>
/// <param name="toCryString"></param>
/// <returns></returns>
public static string EncodeByMD5_32(string toCryString)
{
return TwEncode.EncryptPass(0, toCryString);
}
/// <summary>
/// 64位的MD5加密
/// </summary>
/// <param name="toCryString"></param>
/// <returns></returns>
public static string EncodeByMD5_64(string toCryString)
{
return TwEncode.EncryptPass(1, toCryString);
}
#endregion
#region Base64加密码 / 解密
/// <summary>
/// 用Base64加密
/// </summary>
/// <param name="strText"></param>
/// <returns></returns>
public static string EncodeByBase64(String strText)
{
return Encrypt(strText, "&%#@?,:*");
}
/// <summary>
/// 解密Base64加密的密码
/// </summary>
/// <param name="strText"></param>
/// <returns></returns>
public static String DecodeByBase64(String strText)
{
return Decrypt(strText, "&%#@?,:*");
}
/// <summary>
/// Base64加密函数
/// </summary>
/// <param name="strText"></param>
/// <param name="strEncrKey"></param>
/// <returns></returns>
private static String Encrypt(String strText, String strEncrKey)
{
Byte[] byKey = { };
Byte[] IV = { 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF };
try
{
byKey = System.Text.Encoding.UTF8.GetBytes(strEncrKey.Substring(0, 8));
DESCryptoServiceProvider des = new DESCryptoServiceProvider();
Byte[] inputByteArray = Encoding.UTF8.GetBytes(strText);
MemoryStream ms = new MemoryStream();
CryptoStream cs = new CryptoStream(ms, des.CreateEncryptor(byKey, IV), CryptoStreamMode.Write);
cs.Write(inputByteArray, 0, inputByteArray.Length);
cs.FlushFinalBlock();
return Convert.ToBase64String(ms.ToArray());
}
catch (Exception ex)
{
return ex.Message + ex.ToString() + ex.Source;
}
}
/// <summary>
/// Base64解密函数
/// </summary>
/// <param name="strText"></param>
/// <param name="sDecrKey"></param>
/// <returns></returns>
private static String Decrypt(String strText, String sDecrKey)
{
Byte[] byKey = { };
Byte[] IV = { 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF };
Byte[] inputByteArray = new byte[strText.Length];
try
{
byKey = System.Text.Encoding.UTF8.GetBytes(sDecrKey.Substring(0, 8));
DESCryptoServiceProvider des = new DESCryptoServiceProvider();
inputByteArray = Convert.FromBase64String(strText);
MemoryStream ms = new MemoryStream();
CryptoStream cs = new CryptoStream(ms, des.CreateDecryptor(byKey, IV), CryptoStreamMode.Write);
cs.Write(inputByteArray, 0, inputByteArray.Length);
cs.FlushFinalBlock();
System.Text.Encoding encoding = System.Text.Encoding.UTF8;
return encoding.GetString(ms.ToArray());
}
catch (Exception ex)
{
return ex.Message + ex.ToString() + ex.Source;
}
}
#endregion
#region SHA1 加密 40位
/// <summary>
/// 使用SHA1加密字符串。 40位
/// </summary>
/// <param name="inputString">输入字符串。</param>
/// <returns>加密后的字符串。(40个字符)</returns>
public static string EncodeBySHA1(string inputString)
{
SHA1CryptoServiceProvider sha1 = new SHA1CryptoServiceProvider();
byte[] encryptedBytes = sha1.ComputeHash(Encoding.ASCII.GetBytes(inputString));
StringBuilder sb = new StringBuilder();
for (int i = 0; i < encryptedBytes.Length; i++)
{
sb.AppendFormat("{0:x2}", encryptedBytes[i]);
}
return sb.ToString();
}
#endregion
#region DES 加密/解密
private static byte[] key = ASCIIEncoding.ASCII.GetBytes("caikelun");
private static byte[] iv = ASCIIEncoding.ASCII.GetBytes("12345678");
/// <summary>
/// DES加密。
/// </summary>
/// <param name="inputString">输入字符串。</param>
/// <returns>加密后的字符串。</returns>
public static string EncodeByDES(string inputString)
{
MemoryStream ms = null;
CryptoStream cs = null;
StreamWriter sw = null;
DESCryptoServiceProvider des = new DESCryptoServiceProvider();
try
{
ms = new MemoryStream();
cs = new CryptoStream(ms, des.CreateEncryptor(key, iv), CryptoStreamMode.Write);
sw = new StreamWriter(cs);
sw.Write(inputString);
sw.Flush();
cs.FlushFinalBlock();
return Convert.ToBase64String(ms.GetBuffer(), 0, (int)ms.Length);
}
finally
{
if (sw != null) sw.Close();
if (cs != null) cs.Close();
if (ms != null) ms.Close();
}
}
/// <summary>
/// DES解密。
/// </summary>
/// <param name="inputString">输入字符串。</param>
/// <returns>解密后的字符串。</returns>
public static string DecodeByDES(string inputString)
{
MemoryStream ms = null;
CryptoStream cs = null;
StreamReader sr = null;
DESCryptoServiceProvider des = new DESCryptoServiceProvider();
try
{
ms = new MemoryStream(Convert.FromBase64String(inputString));
cs = new CryptoStream(ms, des.CreateDecryptor(key, iv), CryptoStreamMode.Read);
sr = new StreamReader(cs);
return sr.ReadToEnd();
}
finally
{
if (sr != null) sr.Close();
if (cs != null) cs.Close();
if (ms != null) ms.Close();
}
}
#endregion
#region HTML & URL地址加密 - .NET的方法
/// <summary>
/// URL地址加密 - .NET的方法
/// </summary>
/// <param name="strUrl">URL地址</param>
/// <returns></returns>
public static string UrlEncode(string strUrl)
{
if (string.IsNullOrEmpty(strUrl))
{
return strUrl;
}
return System.Web.HttpUtility.UrlEncode(strUrl);
}
/// <summary>
/// URL地址解密 - .NET的方法
/// </summary>
/// <param name="strURL">加密过的URL地址</param>
/// <returns></returns>
public static string UrlDecode(string strURL)
{
if (string.IsNullOrEmpty(strURL))
{
return strURL;
}
return System.Web.HttpUtility.UrlEncode(strURL);
}
/// <summary>
/// 返回 HTML 字符串的编码结果
/// </summary>
/// <param name="str">字符串</param>
/// <returns>编码结果</returns>
public static string HtmlEncode(string str)
{
return HttpUtility.HtmlEncode(str);
}
/// <summary>
/// 返回 HTML 字符串的解码结果
/// </summary>
/// <param name="str">字符串</param>
/// <returns>解码结果</returns>
public static string HtmlDecode(string str)
{
return HttpUtility.HtmlDecode(str);
}
#endregion
#region SHA256函数
/// <summary>
/// SHA256函数
/// </summary>
/// <param name="str">原始字符串</param>
/// <returns>SHA256结果</returns>
public static string SHA256(string str)
{
byte[] SHA256Data = Encoding.UTF8.GetBytes(str);
SHA256Managed Sha256 = new SHA256Managed();
byte[] Result = Sha256.ComputeHash(SHA256Data);
return Convert.ToBase64String(Result); //返回长度为44字节的字符串
}
#endregion
}
}