c 如何實現檔案與資料夾加密收藏

2021-04-24 22:15:12 字數 2946 閱讀 6734

c#加密檔案只可以對檔案本身加密,卻不能對資料夾加密,經查詢得知:是系統限制了,不知道真假,大家可在查詢,把結果告訴大家共享。其實系統已經整合了對資料夾和硬碟的加密的功能。下面是對單個檔案的加密**:

引用中主要是新增了兩個:using system.security.cryptography;     using system.io;

原始碼(帶有詳細的注釋):

using system;

using system.collections.generic;

using system.componentmodel;

using system.data;

using system.drawing;

using system.text;

using system.windows.forms;

using system.security.cryptography;

using system.io;

namespace xiaoxia

private void filejiami_load(object sender, eventargs e)

private void button1_click(object sender, eventargs e)

}private void button2_click(object sender, eventargs e)

if (this.txtfileout.text == "")

if (this.txtmima1.text.length < 6 || this.txtmima2.text != this.txtmima1.text)

//獲得被加密檔名

string myinfilename = this.txtfilein.text;

//獲得加密檔名

string myoutfilename = this.txtfileout.text;

//設定初始變數

byte mydesiv=;

byte mydeskey =;

//根據密碼設定金鑰大小

string mykeystring = this.txtmima1.text;

if (mykeystring.length == 6);}

if (mykeystring.length == 7);}

if (mykeystring.length >=8);}

//建立輸入和輸出檔案流

filestream myinfilestream = new filestream(myinfilename, filemode.open, fileaccess.read);

filestream myoutfilestream = new filestream(myoutfilename, filemode.openorcreate, fileaccess.write);

myoutfilestream.setlength(0);

//每次的中間流

byte insertdata = new byte[100];

//代表已經加密流的大小

int completedlength = 0;

//代表要加密檔案總的大小

long infilesize = myinfilestream.length;

//建立des物件

des mydes = new descryptoserviceprovider();

//建立加密流

cryptostream encrytostream = new cryptostream(myoutfilestream, mydes.createencryptor(mydeskey, mydesiv), cryptostreammode.write);

//從輸入檔案中讀取流,然後加密到輸出檔案流

while (completedlength < infilesize)

//關閉流

encrytostream.close();

myinfilestream.close();

myoutfilestream.close();

messagebox.show("檔案加密操作成功!", "小俠提示您:", messageboxbuttons.ok, messageboxicon.information);

}…………

上面是加密的**,解密的**幾乎和上面完全一樣,步驟思路完全一樣。看下面,可對比上面:

主要是在try()裡面有以下實現,其他**相同,略:

//獲得要解密的檔名

//獲得要儲存的檔名

//設定初始向量

//根據密碼算出金鑰

//建立輸入和輸出檔案流

//每次的中間流

//代表已經加密流的大小

//代表要加密檔案總的大小

//建立des物件

//建立解密流

//從輸入檔案中讀取流,然後解密到輸出檔案中

while (mycompletedsize < myfilesize)

//關閉流

這裡和加密的區別之處在於建立des物件和建立解密流上,這裡應該是:

//建立des物件

des mydes = new descryptoserviceprovider();

//建立解密流

cryptostream decrytostream = new cryptostream(myoutfilestream, mydes.createdecryptor(mydeskey, mydesiv), cryptostreammode.write);

和上面解密**對比即可得知,很簡單。

至於系統自帶的資料夾和硬碟加密方法簡單介紹,大家可參閱:http://zhidao.baidu.com/question/11126052.html?si=1

資料夾加密

第一招 命令法 step1 開啟執行視窗,輸入 cmd 開啟命令提示視窗.輸入 md g jm.並按回車鍵,在g盤 根據你的實際情況輸入相應的目錄 下新建乙個名為 jm.的資料夾.這個資料夾就是偶們即將用來存放隱藏檔案的文 件夾.step2 再次開啟執行對話方塊,輸入上面新建的加密資料夾 g jm....

資料夾加密軟體推薦 資料夾加密超級大師

我的電腦是多用途的,平時我工作用,休閒用,理財用,還要給小孩學習用,windows雖然可以建立不同許可權等級的使用者,但我的一些文件存放在各個硬碟中,即使是 guest 使用者,只要登入了我的電腦,也能訪問 磁碟上的檔案,隨著小孩的水平越來越高,一般的隱藏等手段已經無法保密檔案了,就在baidu上搜...

加密資料夾 徹底隱藏資料夾

六 一過兒童節了,我還是小孩。最近看到求助如何加密資料夾,我來說一說吧。常用的方法 這個,懂點電腦的都會吧 winrar都有密碼選項的。網上一搜,全都是。是時候展現真正的技術了!cmd,出來!切換到 檔案 資料夾 的目錄 attrib s h 檔案 資料夾 名 可見,命令執行前,日記資料夾並沒有隱藏...