




已閱讀5頁,還剩2頁未讀, 繼續(xù)免費(fèi)閱讀
版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請(qǐng)進(jìn)行舉報(bào)或認(rèn)領(lǐng)
文檔簡(jiǎn)介
首先,這是個(gè)winform的程序,部署在市場(chǎng)部同事的筆記本上面,基于.Net 2.0做的,它的功能大致如下: 加密合同文檔(*.doc) 在客戶處解密此合同文檔,進(jìn)行編輯,再次加密 回到公司可以通過密碼打開它由于時(shí)間關(guān)系,只是很粗略的做了個(gè)大概。打開vs2008,建立一個(gè)winform項(xiàng)目,設(shè)計(jì)程序界面如下:很簡(jiǎn)單的,待會(huì)兒在文章最后會(huì)有下載地址。我們可以把任意文件,拖入程序界面,即可進(jìn)行加密,在此暫不作說明,大家可以待會(huì)兒下載回去試試,很簡(jiǎn)單的。下面分享一下加密代碼吧:代碼 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 / / 密碼 / / 加密對(duì)象 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(); / / 生成指定長(zhǎng)度的隨機(jī)Byte數(shù)組 / / Byte數(shù)組長(zhǎng)度 / 隨機(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; / 輸入文件長(zhǎng)度 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)建加密對(duì)象 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)建散列對(duì)象, 校驗(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); / 讀取文件長(zhǎng)度 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; / 獲取比較和舊的散列對(duì)象 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等.壓縮文件請(qǐng)下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請(qǐng)聯(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ǔ)空間,僅對(duì)用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對(duì)用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對(duì)任何下載內(nèi)容負(fù)責(zé)。
- 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請(qǐng)與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對(duì)自己和他人造成任何形式的傷害或損失。
最新文檔
- 銅冶煉行業(yè)標(biāo)準(zhǔn)化與質(zhì)量控制研究-洞察闡釋
- 電池制造技術(shù)革新-洞察闡釋
- 預(yù)測(cè)性維護(hù)在食品工業(yè)中的應(yīng)用與數(shù)據(jù)分析-洞察闡釋
- 高鐵網(wǎng)絡(luò)對(duì)城市經(jīng)濟(jì)發(fā)展的推動(dòng)作用
- 高鐵等高速列車的運(yùn)行與服務(wù)模式研究
- 金融風(fēng)險(xiǎn)預(yù)警大數(shù)據(jù)分析的實(shí)踐與展望
- 公寓住戶安全管理制度
- 國(guó)企單位后勤管理制度
- 醫(yī)藥公司自提管理制度
- 職業(yè)倫理宣誓協(xié)議
- 《煤礦安全生產(chǎn)責(zé)任制》培訓(xùn)課件2025
- 2025年管理類聯(lián)考《英語二》真題復(fù)盤卷(帶解析)
- 極地科考裝備智能化設(shè)計(jì)-深度研究
- 中職高教版(2023)語文職業(yè)模塊-第七單元語文綜合實(shí)踐-走進(jìn)傳統(tǒng)節(jié)日-探尋文化根脈【課件】
- 扶貧工作考勤管理制度
- 110kV鋼管桿技術(shù)規(guī)范書
- 水泥道路路面修復(fù)施工方案
- 占道施工安全培訓(xùn)
- 信息技術(shù)在旅游行業(yè)的變革與升級(jí)考核試卷
- PROJECT項(xiàng)目管理軟件使用教程
- 全國(guó)教育科學(xué)規(guī)劃課題立項(xiàng)申請(qǐng)書范文
評(píng)論
0/150
提交評(píng)論