




已閱讀5頁,還剩2頁未讀, 繼續(xù)免費(fèi)閱讀
版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進(jìn)行舉報(bào)或認(rèn)領(lǐng)
文檔簡介
首先,這是個(gè)winform的程序,部署在市場部同事的筆記本上面,基于.Net 2.0做的,它的功能大致如下: 加密合同文檔(*.doc) 在客戶處解密此合同文檔,進(jìn)行編輯,再次加密 回到公司可以通過密碼打開它由于時(shí)間關(guān)系,只是很粗略的做了個(gè)大概。打開vs2008,建立一個(gè)winform項(xiàng)目,設(shè)計(jì)程序界面如下:很簡單的,待會(huì)兒在文章最后會(huì)有下載地址。我們可以把任意文件,拖入程序界面,即可進(jìn)行加密,在此暫不作說明,大家可以待會(huì)兒下載回去試試,很簡單的。下面分享一下加密代碼吧:代碼 using System;using System.Collections.Generic;using System.Text;using System.Security.Cryptography;using System.IO;namespace FileLock / / 異常處理類 / public class CryptoHelpException : ApplicationException public CryptoHelpException(string msg) : base(msg) public class CryptoHelp private const ulong FC_TAG = 0xFC010203040506CF; private const int BUFFER_SIZE = 128 * 1024; / / 檢驗(yàn)兩個(gè)Byte數(shù)組是否相同 / / Byte數(shù)組 / Byte數(shù)組 / true相等 private static bool CheckByteArrays(byte b1, byte b2) if (b1.Length = b2.Length) for (int i = 0; i b1.Length; +i) if (b1i != b2i) return false; return true; return false; / / 創(chuàng)建Rijndael SymmetricAlgorithm / / 密碼 / / 加密對象 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; / / 加密文件隨機(jī)數(shù)生成 / private static RandomNumberGenerator rand = new RNGCryptoServiceProvider(); / / 生成指定長度的隨機(jī)Byte數(shù)組 / / Byte數(shù)組長度 / 隨機(jī)Byte數(shù)組 private static byte GenerateRandomBytes(int count) byte bytes = new bytecount; rand.GetBytes(bytes); return bytes; / / 加密文件 / / 待加密文件 / 加密后輸入文件 / 加密密碼 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 byteBUFFER_SIZE; / 緩存 int read = -1; / 輸入文件讀取數(shù)量 int value = 0; / 獲取IV和salt byte IV = GenerateRandomBytes(16); byte salt = GenerateRandomBytes(16); / 創(chuàng)建加密對象 SymmetricAlgorithm sma = CryptoHelp.CreateRijndael(password, salt); sma.IV = IV; / 在輸出文件開始部分寫入IV和salt fout.Write(IV, 0, IV.Length); fout.Write(salt, 0, salt.Length); / 創(chuàng)建散列加密 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); / 讀寫字節(jié)塊到加密流緩沖區(qū) while (read = fin.Read(bytes, 0, bytes.Length) != 0) cout.Write(bytes, 0, read); chash.Write(bytes, 0, read); value += read; / 關(guān)閉加密流 chash.Flush(); chash.Close(); / 讀取散列 byte hash = hasher.Hash; / 輸入文件寫入散列 cout.Write(hash, 0, hash.Length); / 關(guān)閉文件流 cout.Flush(); cout.Close(); / / 解密文件 / / 待解密文件 / 解密后輸出文件 / 解密密碼 public static void DecryptFile(string inFile, string outFile, string password) / 創(chuàng)建打開文件流 using (FileStream fin = File.OpenRead(inFile), fout = File.OpenWrite(outFile) int size = (int)fin.Length; byte bytes = new byteBUFFER_SIZE; int read = -1; int value = 0; int outValue = 0; byte IV = new byte16; fin.Read(IV, 0, 16); byte salt = new byte16; fin.Read(salt, 0, 16); SymmetricAlgorithm sma = CryptoHelp.CreateRijndael(password, salt); sma.IV = IV; value = 32; long lSize = -1; / 創(chuàng)建散列對象, 校驗(yàn)文件 HashAlgorithm hasher = SHA256.Create(); /using (CryptoStream cin = new CryptoStream(fin, sma.CreateDecryptor(), CryptoStreamMode.Read), / chash = new CryptoStream(Stream.Null, hasher, CryptoStreamMode.Write) / CryptoStream cin = new CryptoStream(fin, sma.CreateDecryptor(), CryptoStreamMode.Read), chash = new CryptoStream(Stream.Null, hasher, CryptoStreamMode.Write); / 讀取文件長度 BinaryReader br = new BinaryReader(cin); lSize = br.ReadInt64(); ulong tag = br.ReadUInt64(); if (FC_TAG != tag) throw new CryptoHelpException(文件被破壞或者密碼不正確); long numReads = lSize / BUFFER_SIZE; long slack = (long)lSize % BUFFER_SIZE; for (int i = 0; i 0) read = cin.Read(bytes, 0, (int)slack); fout.Write(bytes, 0, read); chash.Write(bytes, 0, read); value += read; outValue += read; chash.Flush(); chash.Close(); fout.Flush(); fout.Close(); byte curHash = hasher.Hash; / 獲取比較和舊的散列對象 byte oldHash = new bytehasher.HashSize / 8; read = cin.Read(oldHash, 0, oldHash.Length); if (oldHash.Length != read) | (!CheckByteArrays(oldHash, curHash) throw new CryptoHelpException(文件被破壞); cin.Flush(); cin.Close(); / if (outValue != lSize) throw new CryptoHelpException(文件大小不匹配); 是不是很明了呢?當(dāng)然了,網(wǎng)上一搜一大把,我這個(gè)也是不小心搜到的,感覺比較不錯(cuò)的代碼,后來經(jīng)過我少許加工,即可使用了。當(dāng)然,在這里,還要提一句,這個(gè)小程序,為了讓它更方便,直接拖拽文件到其圖標(biāo)之上,就默認(rèn)打開程序,加載該文件,并進(jìn)行加密操作,聽起來是不是很cool!?呵呵,下面再和大家分享一下如何實(shí)現(xiàn)吧:在窗體的DragDrop和DragEnter事件下,添加如下代碼:代碼 private void MainForm_DragEnter(object sender, DragEventArgs e) if (e.Data.GetDataPresent(DataFormats.FileDrop) e.Effect = DragDropEffects.Move; else e.Effect = DragDropEffects.None; private void MainForm_DragDrop(object sender, DragEventArgs e) string path = (System.Array)e.Data.GetData(DataFormats.FileDrop).GetValue(0).ToString(); extension = System.IO.Path.GetExtension(path);/擴(kuò)展名 if (e
溫馨提示
- 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫網(wǎng)僅提供信息存儲(chǔ)空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負(fù)責(zé)。
- 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 我的偶像人物描寫(9篇)
- 描述公園美景寫景作文10篇
- 某商超宣傳策略規(guī)定
- 雨中的回憶描寫一場雨的作文(15篇)
- 2025年初中化學(xué)九年級上冊期中測試卷重點(diǎn)難點(diǎn)實(shí)戰(zhàn)演練解析
- 2025年監(jiān)理工程師職業(yè)能力測試卷:監(jiān)理現(xiàn)場巡查與記錄技巧試題集錦
- 2025年小學(xué)語文畢業(yè)升學(xué)考試全真模擬卷(文學(xué)名著閱讀)之《西游記》人物關(guān)系分析試題
- 2025年建筑行業(yè)安全生產(chǎn)事故調(diào)查與分析試題庫試卷
- 2025年激光治療機(jī)項(xiàng)目提案報(bào)告
- 電力系統(tǒng)故障分析與排除試題及答案
- 危重患者的監(jiān)測與管理
- 基于數(shù)據(jù)的女性健康問題研究及解決方案探討
- 閩教版(2024)三年級英語下冊全冊大單元整體教學(xué)設(shè)計(jì) 教案
- 股東會(huì)議程及決議草案
- TLYCY 3071-2024 森林草原防火無人機(jī)監(jiān)測技術(shù)規(guī)范
- 工廠生產(chǎn)管理制度流程
- 酒店 入股合同范本
- GB/T 45204-2025寵物經(jīng)營場所環(huán)境清潔與消毒指南
- 《弟子規(guī)之信篇》課件
- 電力設(shè)施的定期檢查與維修記錄管理
- 模切品質(zhì)培訓(xùn)
評論
0/150
提交評論