/**//**********************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"#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"#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"#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);
  }
 }
}
新聞熱點
疑難解答
圖片精選