加密類(lèi)代碼
 /**//**//**//**********************created by chen**************************
*如果你覺(jué)得本人的文章好,要引用請(qǐng)尊重著作人的勞動(dòng)果實(shí),說(shuō)明
*出處以及原創(chuàng)作者,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加密,為了便于今后的管理和維護(hù)
 /// 請(qǐng)不要隨便改動(dòng)密碼,或者改變了密碼后請(qǐng)一定要
 /// 牢記先前的密碼,否則將會(huì)照成不可預(yù)料的損失
 /// </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>
  /// 構(gòu)造函數(shù)
  /// </summary>
  public desencrypt()
  ...{
   des=new descryptoserviceprovider();
  }
  "propertys""propertys"#region "propertys"
  /**//**//**//// <summary>
  /// 設(shè)置加密密鑰
  /// </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>
  /// 加密字符串并返回加密后的結(jié)果
  /// </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);//得到要加密的內(nèi)容
   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);
    //得到要加密文件的字節(jié)流
    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
    ...{
     //加密得到的文件字節(jié)流
     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("沒(méi)有找到指定的文件");
   }
  }
  /**//**//**//// <summary>
  /// 文件加密函數(shù)的重載版本,如果不指定輸出路徑,
  /// 那么原來(lái)的文件將被加密后的文件覆蓋
  /// </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];
    //得到要解密文件的字節(jié)流
    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("指定的解密文件沒(méi)有找到");
   }
  }
  /**//**//**//// <summary>
  /// 解密文件的重載版本,如果沒(méi)有給出解密后文件的輸出路徑,
  /// 則解密后的文件將覆蓋先前的文件
  /// </summary>
  /// <param name="filepath"></param>
  public void decryptfile(string filepath)
  ...{
   this.decryptfile(filepath,filepath);
  }
  #endregion
 }
 /**//**//**//// <summary>
 /// md5加密類(lèi),注意經(jīng)md5加密過(guò)的信息是不能轉(zhuǎn)換回原始數(shù)據(jù)的
 /// ,請(qǐng)不要在用戶(hù)敏感的信息中使用此加密技術(shù),比如用戶(hù)的密碼,
 /// 請(qǐng)盡量使用對(duì)稱(chēng)加密
 /// </summary>
 public class md5encrypt
 ...{
  private md5 md5;
  public md5encrypt()
  ...{
   md5=new md5cryptoserviceprovider();
  }
  /**//**//**//// <summary>
  /// 從字符串中獲取散列值
  /// </summary>
  /// <param name="str">要計(jì)算散列值的字符串</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>
  /// 根據(jù)文件來(lái)計(jì)算散列值
  /// </summary>
  /// <param name="filepath">要計(jì)算散列值的文件路徑</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("指定的文件沒(méi)有找到");
   }
  }
 }
 /**//**//**//// <summary>
 /// 用于數(shù)字簽名的hash類(lèi)
 /// </summary>
 public class mactripledesencrypt
 ...{
  private mactripledes mact;
  private string __key="ksn168ch";
  private byte[] __data=null;
  public mactripledesencrypt()
  ...{
   mact=new mactripledes();
  }
  /**//**//**//// <summary>
  /// 獲取或設(shè)置用于數(shù)字簽名的密鑰
  /// </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("用于數(shù)字簽名的密鑰長(zhǎng)度必須是8,16,24值之一");
    else
     this.__key=value;
   }
  }
  /**//**//**//// <summary>
  /// 獲取或設(shè)置用于數(shù)字簽名的用戶(hù)數(shù)據(jù)
  /// </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("沒(méi)有設(shè)置要進(jìn)行數(shù)字簽名的用戶(hù)"+
                                         "數(shù)據(jù)(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);
  }
 }
}
 
最大的網(wǎng)站源碼資源下載站,
新聞熱點(diǎn)
疑難解答
圖片精選