using System;
using System
using System
using System
using System
namespace EncryptClasses
{
/// <summary>
/// 此處定義的是DES加密
/// 請不要隨便改動密碼
/// 牢記先前的密碼
/// </summary>
public class DESEncrypt
{
#region
private string iv=
private string key=
private Encoding encoding=new UnicodeEncoding();
private DES des;
#endregion
/// <summary>
/// 構造函數
/// </summary>
public DESEncrypt()
{
des=new DESCryptoServiceProvider();
}
#region
/// <summary>
/// 設置加密密鑰
/// </summary>
public string EncryptKey
{
get{return this
set
{
this
}
}
/// <summary>
/// 要加密字符的編碼模式
/// </summary>
public Encoding EncodingMode
{
get{return this
set{this
}
#endregion
#region
/// <summary>
/// 加密字符串並返回加密後的結果
/// </summary>
/// <param name=
/// <returns></returns>
public string EncryptString(string str)
{
byte[] ivb=Encoding
byte[] keyb=Encoding
byte[] toEncrypt=this
byte[] encrypted;
ICryptoTransform encryptor=des
MemoryStream msEncrypt=new MemoryStream();
CryptoStream csEncrypt=new CryptoStream(msEncrypt
csEncrypt
csEncrypt
encrypted=msEncrypt
csEncrypt
msEncrypt
return this
}
/// <summary>
/// 加密指定的文件
/// </summary>
/// <param name=
/// <param name=
public void EncryptFile(string filePath
{
bool isExist=File
if(isExist)//如果存在
{
byte[] ivb=Encoding
byte[] keyb=Encoding
//得到要加密文件的字節流
FileStream fin=new FileStream(filePath
StreamReader reader=new StreamReader(fin
string dataStr=reader
byte[] toEncrypt=this
fin
FileStream fout=new FileStream(outPath
ICryptoTransform encryptor=des
CryptoStream csEncrypt=new CryptoStream(fout
try
{
//加密得到的文件字節流
csEncrypt
csEncrypt
}
catch(Exception err)
{
throw new ApplicationException(err
}
finally
{
try
{
fout
csEncrypt
}
catch
{
;
}
}
}
else
{
throw new FileNotFoundException(
}
}
/// <summary>
/// 文件加密函數的重載版本
/// 那麼原來的文件將被加密後的文件覆蓋
/// </summary>
/// <param name=
public void EncryptFile(string filePath)
{
this
}
/// <summary>
/// 解密給定的字符串
/// </summary>
/// <param name=
/// <returns></returns>
public string DecryptString(string str)
{
byte[] ivb=Encoding
byte[] keyb=Encoding
byte[] toDecrypt=this
byte[] deCrypted=new byte[toDecrypt
ICryptoTransform deCryptor=des
MemoryStream msDecrypt=new MemoryStream(toDecrypt);
CryptoStream csDecrypt=new CryptoStream(msDecrypt
try
{
csDecrypt
}
catch(Exception err)
{
throw new ApplicationException(err
}
finally
{
try
{
msDecrypt
csDecrypt
}
catch{;}
}
return this
}
/// <summary>
/// 解密指定的文件
/// </summary>
/// <param name=
/// <param name=
public void DecryptFile(string filePath
{
bool isExist=File
if(isExist)//如果存在
{
byte[] ivb=Encoding
byte[] keyb=Encoding
FileInfo file=new FileInfo(filePath);
byte[] deCrypted=new byte[file
//得到要解密文件的字節流
FileStream fin=new FileStream(filePath
//解密文件
try
{
ICryptoTransform decryptor=des
CryptoStream csDecrypt=new CryptoStream(fin
csDecrypt
}
catch(Exception err)
{
throw new ApplicationException(err
}
finally
{
try
{
fin
}
catch{;}
}
FileStream fout=new FileStream(outPath
fout
fout
}
else
{
throw new FileNotFoundException(
}
}
/// <summary>
/// 解密文件的重載版本
/// 則解密後的文件將覆蓋先前的文件
/// </summary>
/// <param name=
public void DecryptFile(string filePath)
{
this
}
#endregion
}
/// <summary>
/// MD
///
/// 請盡量使用對稱加密
/// </summary>
public class MD
{
private MD
public MD
{
md
}
/// <summary>
/// 從字符串中獲取散列值
/// </summary>
/// <param name=
/// <returns></returns>
public string GetMD
{
byte[] toCompute=Encoding
byte[] hashed=md
return Encoding
}
/// <summary>
/// 根據文件來計算散列值
/// </summary>
/// <param name=
/// <returns></returns>
public string GetMD
{
bool isExist=File
if(isExist)//如果文件存在
{
FileStream stream=new FileStream(filePath
StreamReader reader=new StreamReader(stream
string str=reader
byte[] toHash=Encoding
byte[] hashed=md
stream
return Encoding
}
else//文件不存在
{
throw new FileNotFoundException(
}
}
}
/// <summary>
/// 用於數字簽名的hash類
/// </summary>
public class MACTripleDESEncrypt
{
private MACTripleDES mact;
private string __key=
private byte[] __data=null;
public MACTripleDESEncrypt()
{
mact=new MACTripleDES();
}
/// <summary>
/// 獲取或設置用於數字簽名的密鑰
/// </summary>
public string Key
{
get{return this
set
{
int keyLength=value
int[] keyAllowLengths=new int[]{
bool isRight=false;
foreach(int i in keyAllowLengths)
{
if(keyLength==keyAllowLengths[i])
{
isRight=true;
break;
}
}
if(!isRight)
throw new ApplicationException(
else
this
}
}
/// <summary>
/// 獲取或設置用於數字簽名的用戶數據
/// </summary>
public byte[] Data
{
get{return this
set{this
}
/// <summary>
/// 得到簽名後的hash值
/// </summary>
/// <returns></returns>
public string GetHashValue()
{
if(this
throw new NotSetSpecialPropertyException(
byte[] key=Encoding
this
byte[] hash_b=this
return Encoding
}
}
}
From:http://tw.wingwit.com/Article/program/net/201311/11612.html