




已閱讀5頁,還剩2頁未讀, 繼續(xù)免費閱讀
版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進行舉報或認領(lǐng)
文檔簡介
首先,這是個winform的程序,部署在市場部同事的筆記本上面,基于.Net 2.0做的,它的功能大致如下: 加密合同文檔(*.doc) 在客戶處解密此合同文檔,進行編輯,再次加密 回到公司可以通過密碼打開它由于時間關(guān)系,只是很粗略的做了個大概。打開vs2008,建立一個winform項目,設(shè)計程序界面如下:很簡單的,待會兒在文章最后會有下載地址。我們可以把任意文件,拖入程序界面,即可進行加密,在此暫不作說明,大家可以待會兒下載回去試試,很簡單的。下面分享一下加密代碼吧:代碼 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; / / 檢驗兩個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; / / 加密文件隨機數(shù)生成 / private static RandomNumberGenerator rand = new RNGCryptoServiceProvider(); / / 生成指定長度的隨機Byte數(shù)組 / / Byte數(shù)組長度 / 隨機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)建散列對象, 校驗文件 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)上一搜一大把,我這個也是不小心搜到的,感覺比較不錯的代碼,后來經(jīng)過我少許加工,即可使用了。當(dāng)然,在這里,還要提一句,這個小程序,為了讓它更方便,直接拖拽文件到其圖標之上,就默認打開程序,加載該文件,并進行加密操作,聽起來是不是很cool!?呵呵,下面再和大家分享一下如何實現(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);/擴展名 if (e
溫馨提示
- 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負責(zé)。
- 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔(dān)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 2025年甘肅省稅務(wù)系統(tǒng)遴選面試真題帶詳解含答案
- 海洋新材料研發(fā)與產(chǎn)業(yè)化
- 海洋無人裝備應(yīng)用前景
- 老爸的課件圖片
- 老年防摔傷護理課件
- 老年翻身護理課件
- 海洋經(jīng)濟人才實踐鍛煉
- 老年中醫(yī)養(yǎng)生課件
- 婚姻穩(wěn)定期財產(chǎn)保全及共同子女撫養(yǎng)協(xié)議范本
- 菜鳥驛站品牌便利店全面轉(zhuǎn)讓及代理合同
- 信用社2025年風(fēng)險管理工作計劃
- 語料庫語言學(xué)研究范式的起源與發(fā)展
- 藝術(shù)測評服務(wù)合同協(xié)議
- 非盜搶車輛協(xié)議合同協(xié)議
- 中國空調(diào)設(shè)備行業(yè)發(fā)展趨勢與投資戰(zhàn)略研究報告2025-2028版
- 2024年09月江蘇宿遷市泗陽縣農(nóng)村訂單定向醫(yī)學(xué)畢業(yè)生定向招聘30人筆試歷年專業(yè)考點(難、易錯點)附帶答案詳解
- 航天器遙操作策略-全面剖析
- 養(yǎng)老院火災(zāi)事故防范重點培訓(xùn)課件
- 基于數(shù)據(jù)的女性健康問題研究及解決方案探討
- T-CCPS 0014-2024 國有企業(yè)合規(guī)管理體系有效性評價原則與實施指南
- 閩教版(2024)三年級英語下冊全冊大單元整體教學(xué)設(shè)計 教案
評論
0/150
提交評論