asp.net|加密
加密類代碼
/**//**//**//**********************Created by Chen**************************
*如果你覺得本人的文章好,要引用請尊重著作人的勞動果實,說明
*出處以及原創作者,Thank you!!! email:aishen944-sohu.com
*******************************************************************/
using System;
using System.Text;
using System.Security;
using System.Security.Cryptography;
using System.IO;
namespace EncryptClasses
...{
/**//**//**//// <summary>
/// 此處定義的是DES加密,為了便於今後的管理和維護
/// 請不要隨便改動密碼,或者改變了密碼後請一定要
/// 牢記先前的密碼,否則將會照成不可預料的損失
/// </summary>
public class DESEncrypt
...{
"member fields""member fields"#region "member fields"
private string iv="12345678";
private string key="12345678";
private Encoding encoding=new UnicodeEncoding();
private DES des;
#endregion
/**//**//**//// <summary>
/// 建構函式
/// </summary>
public DESEncrypt()
...{
des=new DESCryptoServiceProvider();
}
"propertys""propertys"#region "propertys"
/**//**//**//// <summary>
/// 設定加密金鑰
/// </summary>
public string EncryptKey
...{
get...{return this.key;}
set
...{
this.key=value;
}
}
/**//**//**//// <summary>
/// 要加密字元的編碼模式
/// </summary>
public Encoding EncodingMode
...{
get...{return this.encoding;}
set...{this.encoding=value;}
}
#endregion
"methods""methods"#region "methods"
/**//**//**//// <summary>
/// 加密字串並返回加密後的結果
/// </summary>
/// <param name="str"></param>
/// <returns></returns>
public string EncryptString(string str)
...{
byte[] ivb=Encoding.ASCII.GetBytes(this.iv);
byte[] keyb=Encoding.ASCII.GetBytes(this.EncryptKey);//得到加密金鑰
byte[] toEncrypt=this.EncodingMode.GetBytes(str);//得到要加密的內容
byte[] encrypted;
ICryptoTransform encryptor=des.CreateEncryptor(keyb,ivb);
MemoryStream msEncrypt=new MemoryStream();
CryptoStream csEncrypt=new CryptoStream(msEncrypt,encryptor,CryptoStreamMode.Write);
csEncrypt.Write(toEncrypt,0,toEncrypt.Length);
csEncrypt.FlushFinalBlock();
encrypted=msEncrypt.ToArray();
csEncrypt.Close();
msEncrypt.Close();
return this.EncodingMode.GetString(encrypted);
}
/**//**//**//// <summary>
/// 加密指定的檔案,如果成功返回True,否則false
/// </summary>
/// <param name="filePath">要加密的檔案路徑</param>
/// <param name="outPath">加密後的檔案輸出路徑</param>
public void EncryptFile(string filePath,string outPath)
...{
bool isExist=File.Exists(filePath);
if(isExist)//如果存在
...{
byte[] ivb=Encoding.ASCII.GetBytes(this.iv);
byte[] keyb=Encoding.ASCII.GetBytes(this.EncryptKey);
//得到要加密檔案的位元組流
FileStream fin=new FileStream(filePath,FileMode.Open,FileAccess.Read);
StreamReader reader=new StreamReader(fin,this.EncodingMode);
string dataStr=reader.ReadToEnd();
byte[] toEncrypt=this.EncodingMode.GetBytes(dataStr);
fin.Close();
FileStream fout=new FileStream(outPath,FileMode.Create,FileAccess.Write);
ICryptoTransform encryptor=des.CreateEncryptor(keyb,ivb);
CryptoStream csEncrypt=new CryptoStream(fout,encryptor,CryptoStreamMode.Write);
try
...{
//加密得到的檔案位元組流
csEncrypt.Write(toEncrypt,0,toEncrypt.Length);
csEncrypt.FlushFinalBlock();
}
catch(Exception err)
...{
throw new ApplicationException(err.Message);
}
finally
...{
try
...{
fout.Close();
csEncrypt.Close();
}
catch
...{
;
}
}
}
else
...{
throw new FileNotFoundException("沒有找到指定的檔案");
}
}
/**//**//**//// <summary>
/// 檔案加密函數的重載版本,如果不指定輸出路徑,
/// 那麼原來的檔案將被加密後的檔案覆蓋
/// </summary>
/// <param name="filePath"></param>
public void EncryptFile(string filePath)
...{
this.EncryptFile(filePath,filePath);
}
/**//**//**//// <summary>
/// 解密給定的字串
/// </summary>
/// <param name="str">要解密的字元</param>
/// <returns></returns>
public string DecryptString(string str)
...{
byte[] ivb=Encoding.ASCII.GetBytes(this.iv);
byte[] keyb=Encoding.ASCII.GetBytes(this.EncryptKey);
byte[] toDecrypt=this.EncodingMode.GetBytes(str);
byte[] deCrypted=new byte[toDecrypt.Length];
ICryptoTransform deCryptor=des.CreateDecryptor(keyb,ivb);
MemoryStream msDecrypt=new MemoryStream(toDecrypt);
CryptoStream csDecrypt=new CryptoStream(msDecrypt,deCryptor,CryptoStreamMode.Read);
try
...{
csDecrypt.Read(deCrypted,0,deCrypted.Length);
}
catch(Exception err)
...{
throw new ApplicationException(err.Message);
}
finally
...{
try
...{
msDecrypt.Close();
csDecrypt.Close();
}
catch...{;}
}
return this.EncodingMode.GetString(deCrypted);
}
/**//**//**//// <summary>
/// 解密指定的檔案
/// </summary>
/// <param name="filePath">要解密的檔案路徑</param>
/// <param name="outPath">解密後的檔案輸出路徑</param>
public void DecryptFile(string filePath,string outPath)
...{
bool isExist=File.Exists(filePath);
if(isExist)//如果存在
...{
byte[] ivb=Encoding.ASCII.GetBytes(this.iv);
byte[] keyb=Encoding.ASCII.GetBytes(this.EncryptKey);
FileInfo file=new FileInfo(filePath);
byte[] deCrypted=new byte[file.Length];
//得到要解密檔案的位元組流
FileStream fin=new FileStream(filePath,FileMode.Open,FileAccess.Read);
//解密檔案
try
...{
ICryptoTransform decryptor=des.CreateDecryptor(keyb,ivb);
CryptoStream csDecrypt=new CryptoStream(fin,decryptor,CryptoStreamMode.Read);
csDecrypt.Read(deCrypted,0,deCrypted.Length);
}
catch(Exception err)
...{
throw new ApplicationException(err.Message);
}
finally
...{
try
...{
fin.Close();
}
catch...{;}
}
FileStream fout=new FileStream(outPath,FileMode.Create,FileAccess.Write);
fout.Write(deCrypted,0,deCrypted.Length);
fout.Close();
}
else
...{
throw new FileNotFoundException("指定的解密檔案沒有找到");
}
}
/**//**//**//// <summary>
/// 解密檔案的重載版本,如果沒有給出解密後檔案的輸出路徑,
/// 則解密後的檔案將覆蓋先前的檔案
/// </summary>
/// <param name="filePath"></param>
public void DecryptFile(string filePath)
...{
this.DecryptFile(filePath,filePath);
}
#endregion
}
/**//**//**//// <summary>
/// MD5加密類,注意經MD5加密過的資訊是不能轉換回未經處理資料的
/// ,請不要在使用者敏感的資訊中使用此加密技術,比如使用者的密碼,
/// 請盡量使用對稱式加密
/// </summary>
public class MD5Encrypt
...{
private MD5 md5;
public MD5Encrypt()
...{
md5=new MD5CryptoServiceProvider();
}
/**//**//**//// <summary>
/// 從字串中擷取散列值
/// </summary>
/// <param name="str">要計算散列值的字串</param>
/// <returns></returns>
public string GetMD5FromString(string str)
...{
byte[] toCompute=Encoding.Unicode.GetBytes(str);
byte[] hashed=md5.ComputeHash(toCompute,0,toCompute.Length);
return Encoding.ASCII.GetString(hashed);
}
/**//**//**//// <summary>
/// 根據檔案來計算散列值
/// </summary>
/// <param name="filePath">要計算散列值的檔案路徑</param>
/// <returns></returns>
public string GetMD5FromFile(string filePath)
...{
bool isExist=File.Exists(filePath);
if(isExist)//如果檔案存在
...{
FileStream stream=new FileStream(filePath,FileMode.Open,FileAccess.Read);
StreamReader reader=new StreamReader(stream,Encoding.Unicode);
string str=reader.ReadToEnd();
byte[] toHash=Encoding.Unicode.GetBytes(str);
byte[] hashed=md5.ComputeHash(toHash,0,toHash.Length);
stream.Close();
return Encoding.ASCII.GetString(hashed);
}
else//檔案不存在
...{
throw new FileNotFoundException("指定的檔案沒有找到");
}
}
}
/**//**//**//// <summary>
/// 用於數位簽章的hash類
/// </summary>
public class MACTripleDESEncrypt
...{
private MACTripleDES mact;
private string __key="ksn168ch";
private byte[] __data=null;
public MACTripleDESEncrypt()
...{
mact=new MACTripleDES();
}
/**//**//**//// <summary>
/// 擷取或設定用於數位簽章的密鑰
/// </summary>
public string Key
...{
get...{return this.__key;}
set
...{
int keyLength=value.Length;
int[] keyAllowLengths=new int[]...{8,16,24};
bool isRight=false;
foreach(int i in keyAllowLengths)
...{
if(keyLength==keyAllowLengths[i])
...{
isRight=true;
break;
}
}
if(!isRight)
throw new ApplicationException("用於數位簽章的密鑰長度必須是8,16,24值之一");
else
this.__key=value;
}
}
/**//**//**//// <summary>
/// 擷取或設定用於數位簽章的使用者資料
/// </summary>
public byte[] Data
...{
get...{return this.__data;}
set...{this.__data=value;}
}
/**//**//**//// <summary>
/// 得到簽名後的hash值
/// </summary>
/// <returns></returns>
public string GetHashValue()
...{
if(this.Data==null)
throw new NotSetSpecialPropertyException("沒有設定要進行數位簽章的使用者"+
"資料(property:Data)");
byte[] key=Encoding.ASCII.GetBytes(this.Key);
this.mact.Key=key;
byte[] hash_b=this.mact.ComputeHash(this.mact.ComputeHash(this.Data));
return Encoding.ASCII.GetString(hash_b);
}
}
}