using
System;
using System.IO;
using System.Data;
using System.Text;
using System.Diagnostics;
using System.Security;
using System.Security.Cryptography;
/// <summary>
/// statics 的摘要说明
/// </summary>
public class statics
{
public statics()
{
//
// TODO: 在此处添加构造函数逻辑
//
}
public static byte [] Gytes( string txt) // 转换字节
{
UTF8Encoding utf = new UTF8Encoding();
byte [] bytes = utf.GetBytes(txt);
return bytes;
}
public static string Gpass( byte [] bt) // 转换字符串
{
StringBuilder sb = new StringBuilder();
foreach ( int p in bt)
{
sb.Append(Convert.ToString(p, 16 ).PadLeft( 2 , ' 0 ' ));
}
return sb.ToString();
}
public static string Gutf8( byte [] bt)
{
UTF8Encoding utf = new UTF8Encoding();
return utf.GetString(bt).ToString();
}
public static byte [] Gbase( string bs)
{
return Convert.FromBase64String(bs);
}
public static string Sha1EN( string txt) // sha1散列
{
try
{
SHA1 s = new SHA1CryptoServiceProvider();
byte [] b2 = s.ComputeHash(Gytes(txt));
s.Clear();
return Gpass(b2);
}
catch { return string .Empty; }
}
public static String MD5EN( string txt)
{
try
{
MD5 m = new MD5CryptoServiceProvider();
byte [] b2 = m.ComputeHash(Gytes(txt));
m.Clear();
return Gpass(b2);
}
catch { return string .Empty; }
}
/// 使用DES加密
/// <param name="value"> 待加密的字符串 </param>
/// <param name="key"> 密钥(最大长度8) </param>
/// <param name="IV"> 初始化向量(最大长度8) </param>
/// <returns> 加密后的字符串 </returns>
public static string Des3EN( string value, string key, string iv)
{
try
{
key = key.PadRight( 8 , ' 0 ' ).Substring( 0 , 8 );
iv = iv.PadRight( 8 , ' 0 ' ).Substring( 0 , 8 );
MemoryStream ms = new MemoryStream();
DES des = new DESCryptoServiceProvider();
CryptoStream cs = new CryptoStream(ms, des.CreateEncryptor(Gytes(key), Gytes(iv)), CryptoStreamMode.Write);
des.Clear();
cs.Write(Gytes(value), 0 , Gytes(value).Length);
cs.FlushFinalBlock();
cs.Clear();
return Convert.ToBase64String(ms.ToArray());
}
catch { return string .Empty; }
}
public static string Des3EN( string value, string key)
{
return Des3EN(value, key, key);
}
public static string Des3DE( string value, string key, string iv)
{
try
{
key = key.PadRight( 8 , ' 0 ' ).Substring( 0 , 8 );
iv = iv.PadRight( 8 , ' 0 ' ).Substring( 0 , 8 );
MemoryStream ms = new MemoryStream();
DES des = new DESCryptoServiceProvider();
CryptoStream cs = new CryptoStream(ms, des.CreateDecryptor(Gytes(key), Gytes(iv)), CryptoStreamMode.Write);
des.Clear();
cs.Write(Gbase(value), 0 , Gbase(value).Length);
cs.FlushFinalBlock();
cs.Clear();
return Gutf8(ms.ToArray()).ToString();
}
catch { return string .Empty; }
}
public static string Des3DE( string value, string key)
{
return Des3DE(value, key, key);
}
}
using System.IO;
using System.Data;
using System.Text;
using System.Diagnostics;
using System.Security;
using System.Security.Cryptography;
/// <summary>
/// statics 的摘要说明
/// </summary>
public class statics
{
public statics()
{
//
// TODO: 在此处添加构造函数逻辑
//
}
public static byte [] Gytes( string txt) // 转换字节
{
UTF8Encoding utf = new UTF8Encoding();
byte [] bytes = utf.GetBytes(txt);
return bytes;
}
public static string Gpass( byte [] bt) // 转换字符串
{
StringBuilder sb = new StringBuilder();
foreach ( int p in bt)
{
sb.Append(Convert.ToString(p, 16 ).PadLeft( 2 , ' 0 ' ));
}
return sb.ToString();
}
public static string Gutf8( byte [] bt)
{
UTF8Encoding utf = new UTF8Encoding();
return utf.GetString(bt).ToString();
}
public static byte [] Gbase( string bs)
{
return Convert.FromBase64String(bs);
}
public static string Sha1EN( string txt) // sha1散列
{
try
{
SHA1 s = new SHA1CryptoServiceProvider();
byte [] b2 = s.ComputeHash(Gytes(txt));
s.Clear();
return Gpass(b2);
}
catch { return string .Empty; }
}
public static String MD5EN( string txt)
{
try
{
MD5 m = new MD5CryptoServiceProvider();
byte [] b2 = m.ComputeHash(Gytes(txt));
m.Clear();
return Gpass(b2);
}
catch { return string .Empty; }
}
/// 使用DES加密
/// <param name="value"> 待加密的字符串 </param>
/// <param name="key"> 密钥(最大长度8) </param>
/// <param name="IV"> 初始化向量(最大长度8) </param>
/// <returns> 加密后的字符串 </returns>
public static string Des3EN( string value, string key, string iv)
{
try
{
key = key.PadRight( 8 , ' 0 ' ).Substring( 0 , 8 );
iv = iv.PadRight( 8 , ' 0 ' ).Substring( 0 , 8 );
MemoryStream ms = new MemoryStream();
DES des = new DESCryptoServiceProvider();
CryptoStream cs = new CryptoStream(ms, des.CreateEncryptor(Gytes(key), Gytes(iv)), CryptoStreamMode.Write);
des.Clear();
cs.Write(Gytes(value), 0 , Gytes(value).Length);
cs.FlushFinalBlock();
cs.Clear();
return Convert.ToBase64String(ms.ToArray());
}
catch { return string .Empty; }
}
public static string Des3EN( string value, string key)
{
return Des3EN(value, key, key);
}
public static string Des3DE( string value, string key, string iv)
{
try
{
key = key.PadRight( 8 , ' 0 ' ).Substring( 0 , 8 );
iv = iv.PadRight( 8 , ' 0 ' ).Substring( 0 , 8 );
MemoryStream ms = new MemoryStream();
DES des = new DESCryptoServiceProvider();
CryptoStream cs = new CryptoStream(ms, des.CreateDecryptor(Gytes(key), Gytes(iv)), CryptoStreamMode.Write);
des.Clear();
cs.Write(Gbase(value), 0 , Gbase(value).Length);
cs.FlushFinalBlock();
cs.Clear();
return Gutf8(ms.ToArray()).ToString();
}
catch { return string .Empty; }
}
public static string Des3DE( string value, string key)
{
return Des3DE(value, key, key);
}
}