代码整理:字符串编码解码类
using System;
using System.IO;
using System.Text;
using System.Security.Cryptography;
namespace BTTech.WebModules
{
/// <summary>
/// 字符串加密
/// </summary>
public class SecurityUtility
{
#region 静态属性
private static string m_iv = " ";
private static string m_key = " ";
#endregion
#region RC2Encrypt
public static string RC2Encrypt( string source , string key, string iv)
{
byte[] orgText = Encoding.UTF8.GetBytes(source);
byte[] encryptText;
RC2CryptoServiceProvider myRC2=new RC2CryptoServiceProvider();
ICryptoTransform myCryptoTrans=myRC2.CreateEncryptor(
Encoding.UTF8.GetBytes( (key + m_key).Substring(0,8) ),
Encoding.UTF8.GetBytes( (iv + m_iv).Substring(0,8) )
);
MemoryStream MStream=new MemoryStream();
CryptoStream CStream=new CryptoStream(MStream,myCryptoTrans,CryptoStreamMode.Write);
CStream.Write(orgText,0,orgText.Length);
CStream.FlushFinalBlock();
StringBuilder EnText=new StringBuilder();
encryptText=MStream.ToArray();
foreach(byte Byte in encryptText)
{
EnText.AppendFormat("{0:x2}",Byte);
}
CStream.Close();
return EnText.ToString();
}
#endregion
#region RC2Decrypt
public static string RC2Decrypt( string source , string key ,string iv)
{
byte[] encryptText = new byte[source.Length / 2];
for(int Count = 0; Count < source.Length; Count+=2)
{
encryptText[Count/2] = (byte)(Convert.ToInt32(source.Substring(Count , 2), 16));
}
byte[] decryptText;
RC2CryptoServiceProvider myRC2=new RC2CryptoServiceProvider();
ICryptoTransform myCryptoTrans=myRC2.CreateDecryptor(
Encoding.UTF8.GetBytes( (key + m_key).Substring(0,8) ),
Encoding.UTF8.GetBytes( (iv + m_iv).Substring(0,8) )
);
MemoryStream MStream=new MemoryStream(encryptText);
CryptoStream CStream=new CryptoStream(MStream,myCryptoTrans,CryptoStreamMode.Read);
decryptText=new byte[encryptText.Length];
CStream.Read(decryptText,0,decryptText.Length);
//StringBuilder EnText=new StringBuilder();
CStream.Close();
return Encoding.UTF8.GetString(decryptText);
}
#endregion
#region DESEncrypt
public static string DESEncrypt(string CryptText, string CryptKey,string CryptIV)
{
DESCryptoServiceProvider des = new DESCryptoServiceProvider();
byte[] textOut = Encoding.UTF8.GetBytes(CryptText);
byte[] DESKey=ASCIIEncoding.UTF8.GetBytes( (CryptKey + m_key).Substring(0,8) );
byte[] DESIV=ASCIIEncoding.UTF8.GetBytes( (CryptIV + m_iv).Substring(0,8) );
MemoryStream MStream = new MemoryStream();
CryptoStream CStream = new CryptoStream(MStream, des.CreateEncryptor(DESKey,DESIV),CryptoStreamMode.Write);
CStream.Write(textOut, 0, textOut.Length);
CStream.FlushFinalBlock();
StringBuilder StrRes = new StringBuilder();
foreach(byte Byte in MStream.ToArray())
{
StrRes.AppendFormat("{0:x2}", Byte);
}
return StrRes.ToString();
}
#endregion
#region DESDecrypt
public static string DESDecrypt(string CryptText, string CryptKey,string CryptIV)
{
DESCryptoServiceProvider des = new DESCryptoServiceProvider();
byte[] textOut = new byte[CryptText.Length / 2];
for(int Count = 0; Count < CryptText.Length; Count+=2)
{
textOut[Count/2] = (byte)(Convert.ToInt32(CryptText.Substring(Count , 2), 16));
}
byte[] DESKey=ASCIIEncoding.UTF8.GetBytes( (CryptKey + m_key).Substring(0,8) );
byte[] DESIV=ASCIIEncoding.UTF8.GetBytes( (CryptIV + m_iv).Substring(0,8) );
MemoryStream MStream = new MemoryStream();
CryptoStream CStream = new CryptoStream(MStream, des.CreateDecryptor(DESKey,DESIV),CryptoStreamMode.Write);
CStream.Write(textOut, 0, textOut.Length);
CStream.FlushFinalBlock();
return System.Text.Encoding.UTF8.GetString(MStream.ToArray());
}
#endregion
public static string SHA1Encrypt(string EncryptText)
{
byte[] StrRes=Encoding.Default.GetBytes(EncryptText);
HashAlgorithm mySHA=new SHA1CryptoServiceProvider();
StrRes=mySHA.ComputeHash(StrRes);
StringBuilder EnText=new StringBuilder();
foreach(byte Byte in StrRes)
{
EnText.AppendFormat("{0:x2}",Byte);
}
return EnText.ToString();
}
public static string HMACSHA1Encrypt(string EncryptText,string EncryptKey)
{
byte[] StrRes=Encoding.UTF8.GetBytes(EncryptText);
HMACSHA1 myHMACSHA1=new HMACSHA1(Encoding.UTF8.GetBytes( EncryptKey ));
CryptoStream CStream = new CryptoStream( Stream.Null , myHMACSHA1 , CryptoStreamMode.Write );
CStream.Write( StrRes,0,StrRes.Length );
CStream.Close();
StringBuilder EnText=new StringBuilder();
foreach(byte Byte in myHMACSHA1.Hash)
{
EnText.AppendFormat("{0:x2}",Byte);
}
return EnText.ToString();
}
public static string MD5Encrypt(string CryptText)
{
MD5 myMD5=new MD5CryptoServiceProvider();
byte[] HashCode;
HashCode=Encoding.Default.GetBytes(CryptText);
HashCode=myMD5.ComputeHash(HashCode);
StringBuilder EnText=new StringBuilder();
foreach(byte Byte in HashCode)
{
EnText.AppendFormat("{0:x2}",Byte);
}
return EnText.ToString();
}
}
}
using System.IO;
using System.Text;
using System.Security.Cryptography;
namespace BTTech.WebModules
{
/// <summary>
/// 字符串加密
/// </summary>
public class SecurityUtility
{
#region 静态属性
private static string m_iv = " ";
private static string m_key = " ";
#endregion
#region RC2Encrypt
public static string RC2Encrypt( string source , string key, string iv)
{
byte[] orgText = Encoding.UTF8.GetBytes(source);
byte[] encryptText;
RC2CryptoServiceProvider myRC2=new RC2CryptoServiceProvider();
ICryptoTransform myCryptoTrans=myRC2.CreateEncryptor(
Encoding.UTF8.GetBytes( (key + m_key).Substring(0,8) ),
Encoding.UTF8.GetBytes( (iv + m_iv).Substring(0,8) )
);
MemoryStream MStream=new MemoryStream();
CryptoStream CStream=new CryptoStream(MStream,myCryptoTrans,CryptoStreamMode.Write);
CStream.Write(orgText,0,orgText.Length);
CStream.FlushFinalBlock();
StringBuilder EnText=new StringBuilder();
encryptText=MStream.ToArray();
foreach(byte Byte in encryptText)
{
EnText.AppendFormat("{0:x2}",Byte);
}
CStream.Close();
return EnText.ToString();
}
#endregion
#region RC2Decrypt
public static string RC2Decrypt( string source , string key ,string iv)
{
byte[] encryptText = new byte[source.Length / 2];
for(int Count = 0; Count < source.Length; Count+=2)
{
encryptText[Count/2] = (byte)(Convert.ToInt32(source.Substring(Count , 2), 16));
}
byte[] decryptText;
RC2CryptoServiceProvider myRC2=new RC2CryptoServiceProvider();
ICryptoTransform myCryptoTrans=myRC2.CreateDecryptor(
Encoding.UTF8.GetBytes( (key + m_key).Substring(0,8) ),
Encoding.UTF8.GetBytes( (iv + m_iv).Substring(0,8) )
);
MemoryStream MStream=new MemoryStream(encryptText);
CryptoStream CStream=new CryptoStream(MStream,myCryptoTrans,CryptoStreamMode.Read);
decryptText=new byte[encryptText.Length];
CStream.Read(decryptText,0,decryptText.Length);
//StringBuilder EnText=new StringBuilder();
CStream.Close();
return Encoding.UTF8.GetString(decryptText);
}
#endregion
#region DESEncrypt
public static string DESEncrypt(string CryptText, string CryptKey,string CryptIV)
{
DESCryptoServiceProvider des = new DESCryptoServiceProvider();
byte[] textOut = Encoding.UTF8.GetBytes(CryptText);
byte[] DESKey=ASCIIEncoding.UTF8.GetBytes( (CryptKey + m_key).Substring(0,8) );
byte[] DESIV=ASCIIEncoding.UTF8.GetBytes( (CryptIV + m_iv).Substring(0,8) );
MemoryStream MStream = new MemoryStream();
CryptoStream CStream = new CryptoStream(MStream, des.CreateEncryptor(DESKey,DESIV),CryptoStreamMode.Write);
CStream.Write(textOut, 0, textOut.Length);
CStream.FlushFinalBlock();
StringBuilder StrRes = new StringBuilder();
foreach(byte Byte in MStream.ToArray())
{
StrRes.AppendFormat("{0:x2}", Byte);
}
return StrRes.ToString();
}
#endregion
#region DESDecrypt
public static string DESDecrypt(string CryptText, string CryptKey,string CryptIV)
{
DESCryptoServiceProvider des = new DESCryptoServiceProvider();
byte[] textOut = new byte[CryptText.Length / 2];
for(int Count = 0; Count < CryptText.Length; Count+=2)
{
textOut[Count/2] = (byte)(Convert.ToInt32(CryptText.Substring(Count , 2), 16));
}
byte[] DESKey=ASCIIEncoding.UTF8.GetBytes( (CryptKey + m_key).Substring(0,8) );
byte[] DESIV=ASCIIEncoding.UTF8.GetBytes( (CryptIV + m_iv).Substring(0,8) );
MemoryStream MStream = new MemoryStream();
CryptoStream CStream = new CryptoStream(MStream, des.CreateDecryptor(DESKey,DESIV),CryptoStreamMode.Write);
CStream.Write(textOut, 0, textOut.Length);
CStream.FlushFinalBlock();
return System.Text.Encoding.UTF8.GetString(MStream.ToArray());
}
#endregion
public static string SHA1Encrypt(string EncryptText)
{
byte[] StrRes=Encoding.Default.GetBytes(EncryptText);
HashAlgorithm mySHA=new SHA1CryptoServiceProvider();
StrRes=mySHA.ComputeHash(StrRes);
StringBuilder EnText=new StringBuilder();
foreach(byte Byte in StrRes)
{
EnText.AppendFormat("{0:x2}",Byte);
}
return EnText.ToString();
}
public static string HMACSHA1Encrypt(string EncryptText,string EncryptKey)
{
byte[] StrRes=Encoding.UTF8.GetBytes(EncryptText);
HMACSHA1 myHMACSHA1=new HMACSHA1(Encoding.UTF8.GetBytes( EncryptKey ));
CryptoStream CStream = new CryptoStream( Stream.Null , myHMACSHA1 , CryptoStreamMode.Write );
CStream.Write( StrRes,0,StrRes.Length );
CStream.Close();
StringBuilder EnText=new StringBuilder();
foreach(byte Byte in myHMACSHA1.Hash)
{
EnText.AppendFormat("{0:x2}",Byte);
}
return EnText.ToString();
}
public static string MD5Encrypt(string CryptText)
{
MD5 myMD5=new MD5CryptoServiceProvider();
byte[] HashCode;
HashCode=Encoding.Default.GetBytes(CryptText);
HashCode=myMD5.ComputeHash(HashCode);
StringBuilder EnText=new StringBuilder();
foreach(byte Byte in HashCode)
{
EnText.AppendFormat("{0:x2}",Byte);
}
return EnText.ToString();
}
}
}
DES编码解码器
浙公网安备 33010602011771号