国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > .NET > 正文

.NET下對二進制文件進行加密解密(C#)

2024-07-10 13:04:21
字體:
來源:轉載
供稿:網友

下面的類實現了文件的加密和解密操作,試驗了幾種文件類型均沒有問題,現在和大家共享一下。

namespace mycryptohelp
{
 /// <summary>
 /// 異常處理類
 /// </summary>
 public class cryptohelpexception : applicationexception
 {
  public cryptohelpexception(string msg):base(msg){}
 }

 /// <summary>
 /// crypthelp
 /// </summary>
 public class cryptohelp
 {
  private const ulong fc_tag = 0xfc010203040506cf;

  private const int buffer_size = 128*1024;
  
  /// <summary>
  /// 檢驗兩個byte數組是否相同
  /// </summary>
  /// <param name="b1">byte數組</param>
  /// <param name="b2">byte數組</param>
  /// <returns>true-相等</returns>
  private static bool checkbytearrays(byte[] b1, byte[] b2)
  {
   if(b1.length == b2.length)
   {
    for(int i = 0; i < b1.length; ++i)
    {
     if(b1[i] != b2[i])
      return false;
    }
    return true;
   }
   return false;
  }

  /// <summary>
  /// 創建rijndael symmetricalgorithm
  /// </summary>
  /// <param name="password">密碼</param>
  /// <param name="salt"></param>
  /// <returns>加密對象</returns>
  private static symmetricalgorithm createrijndael(string password, byte[] salt)
  {
   passwordderivebytes pdb = new passwordderivebytes(password,salt,"sha256",1000);
   
   symmetricalgorithm sma = rijndael.create();
   sma.keysize = 256;
   sma.key = pdb.getbytes(32);
   sma.padding = paddingmode.pkcs7;
   return sma;
  }

  /// <summary>
  /// 加密文件隨機數生成
  /// </summary>
  private static randomnumbergenerator rand = new rngcryptoserviceprovider();

  /// <summary>
  /// 生成指定長度的隨機byte數組
  /// </summary>
  /// <param name="count">byte數組長度</param>
  /// <returns>隨機byte數組</returns>
  private static byte[] generaterandombytes(int count)
  {
   byte[] bytes = new byte[count];
   rand.getbytes(bytes);
   return bytes;
  }

  /// <summary>
  /// 加密文件
  /// </summary>
  /// <param name="infile">待加密文件</param>
  /// <param name="outfile">加密后輸入文件</param>
  /// <param name="password">加密密碼</param>
  public static void encryptfile(string infile, string outfile, string password)
  {
   using(filestream fin = file.openread(infile),
       fout = file.openwrite(outfile))
   {
    long lsize = fin.length; // 輸入文件長度
    int size = (int)lsize;
    byte[] bytes = new byte[buffer_size]; // 緩存
    int read = -1; // 輸入文件讀取數量
    int value = 0;
    
    // 獲取iv和salt
    byte[] iv = generaterandombytes(16);
    byte[] salt = generaterandombytes(16);
    
    // 創建加密對象
    symmetricalgorithm sma = cryptohelp.createrijndael(password, salt);
    sma.iv = iv;   
    
    // 在輸出文件開始部分寫入iv和salt
    fout.write(iv,0,iv.length);
    fout.write(salt,0,salt.length);
    
    // 創建散列加密
    hashalgorithm hasher = sha256.create();
    using(cryptostream cout = new cryptostream(fout,sma.createencryptor(),cryptostreammode.write),
        chash = new cryptostream(stream.null,hasher,cryptostreammode.write))
    {
     binarywriter bw = new binarywriter(cout);
     bw.write(lsize);
     
     bw.write(fc_tag);

     // 讀寫字節塊到加密流緩沖區
     while( (read = fin.read(bytes,0,bytes.length)) != 0 )
     {
      cout.write(bytes,0,read);
      chash.write(bytes,0,read); 
      value += read;
     }
     // 關閉加密流
     chash.flush();
     chash.close();

     // 讀取散列
     byte[] hash = hasher.hash;
     
     // 輸入文件寫入散列
     cout.write(hash,0,hash.length);

     // 關閉文件流
     cout.flush();
     cout.close();
    }
   }
  }


發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 无锡市| 时尚| 鄂温| 格尔木市| 泊头市| 扶沟县| 内黄县| 澜沧| 二连浩特市| 巴林右旗| 霍山县| 乐山市| 莲花县| 社旗县| 南川市| 松潘县| 张家川| 新乡市| 罗山县| 苍梧县| 白城市| 廊坊市| 郑州市| 鹰潭市| 临沭县| 宜宾县| 承德市| 恩平市| 蕉岭县| 镇原县| 灵宝市| 太原市| 湘乡市| 蓝田县| 弥渡县| 江达县| 达孜县| 门源| 汝城县| 龙南县| 孝昌县|