最近一直比較忙,或者說比較懶,都沒有來寫部落格,呵呵!這幾天研究的一點小成果拿出來曬一下。
本來想寫關於AES的加密的,但是在使用過程屢屢出現問題,比如說對圖片檔案或者其他格式的非文字檔,甚至是Word都無法進行加密解密,我堅信這是我個人能力的問題,但是時間原因,改為使用DES加密、解密。
其實DES加密的原理比較簡單:
1. 首先需要建立一個訪問演算法的對象。
2. 然後將需要加密的檔案讀為二進位格式,也可理解為使用二進位流讀取待加密檔案。
3. 使用加密的流進行轉換。這個過程稍微複雜一點,即使用了指定的密鑰,這個密鑰的建立就要用到步驟1中的訪問演算法的對象了。
1 /// <summary>
2 /// DES密碼編譯演算法
3 /// </summary>
4 /// <param name="filePath">要加密的檔案路徑</param>
5 public void DESEncrypt(string filePath)
6 {
7 try
8 {
9 //藉助檔案建立FileStream
10 FileStream fsPic = new FileStream("C:\\Windows\\System32\\svchost.exe", FileMode.Open, FileAccess.Read);
11 //要加密的檔案流
12 FileStream fsText = new FileStream(filePath, FileMode.Open, FileAccess.Read);
13 //初始化Key IV
14 byte[] bykey = new byte[16];
15 byte[] byIv = new byte[8];
16 fsPic.Read(bykey, 0, 16);
17 fsPic.Read(byIv, 0, 8);
18 //臨時加密檔案
19 string strPath = filePath;//加密檔案的路徑
20 int intLent = strPath.LastIndexOf("\\") + 1;
21 int intLong = strPath.Length;
22 string strName = strPath.Substring(intLent, intLong - intLent);//要加密的檔案名稱
23 string strTempPath = "C:\\" + strName;//臨時加密檔案路徑
24 FileStream fsOut = File.Open(strTempPath, FileMode.Create, FileAccess.Write);
25 //開始加密
26 RC2CryptoServiceProvider desc = new RC2CryptoServiceProvider();//des進行加密
27 BinaryReader br = new BinaryReader(fsText);//從要加密的檔案中讀出檔案內容
28 CryptoStream cs = new CryptoStream(fsOut, desc.CreateEncryptor(bykey, byIv), CryptoStreamMode.Write);//寫入臨時加密檔案
29 cs.Write(br.ReadBytes((int)fsText.Length), 0, (int)fsText.Length);//寫入加密流
30 cs.FlushFinalBlock();
31 cs.Flush();
32 cs.Close();
33 fsPic.Close();
34 fsText.Close();
35 fsOut.Close();
36 File.Delete(filePath.TrimEnd());//冊除原檔案
37 File.Copy(strTempPath, filePath);//複製加密檔案
38 File.Delete(strTempPath);//冊除臨時檔案
39 }
40 catch (Exception e)
41 {
42 throw new Exception(e.Message);
43 }
44 }
這段代碼中用到了一個檔案,其實這隻是為了建立一個FileStream,也許會有更好的辦法,如果有誰知道請告訴我,在此謝過。
解密演算法也於此類似了。
1 /// <summary>
2 /// DES解密演算法
3 /// </summary>
4 /// <param name="filePath">要解密的檔案路徑</param>
5 public void DESDecrypt(string filePath)
6 {
7 try
8 {
9 //圖片流
10 FileStream fsPic = new FileStream("C:\\Windows\\System32\\svchost.exe", FileMode.Open, FileAccess.Read);
11 //解密檔案流
12 FileStream fsOut = File.Open(filePath, FileMode.Open, FileAccess.Read);
13 //初始化Key IV
14 byte[] bykey = new byte[16];
15 byte[] byIv = new byte[8];
16 fsPic.Read(bykey, 0, 16);
17 fsPic.Read(byIv, 0, 8);
18 //臨時解密檔案
19 string strPath = filePath;//加密檔案的路徑
20 int intLent = strPath.LastIndexOf("\\") + 1;
21 int intLong = strPath.Length;
22 string strName = strPath.Substring(intLent, intLong - intLent);//要加密的檔案名稱
23 string strLinPath = "C:\\" + strName;//臨時解密檔案路徑
24 FileStream fs = new FileStream(strLinPath, FileMode.Create, FileAccess.Write);
25 //開始解密
26 RC2CryptoServiceProvider desc = new RC2CryptoServiceProvider();//des進行解
27 CryptoStream csDecrypt = new CryptoStream(fsOut, desc.CreateDecryptor(bykey, byIv), CryptoStreamMode.Read);//讀出加密檔案
28 BinaryReader sr = new BinaryReader(csDecrypt);//從要加密流中讀出檔案內容
29 BinaryWriter sw = new BinaryWriter(fs);//寫入解密流
30 sw.Write(sr.ReadBytes(Convert.ToInt32(fsOut.Length)));//
31 sw.Flush();
32 sw.Close();
33 sr.Close();
34 fs.Close();
35 fsOut.Close();
36 fsPic.Close();
37 csDecrypt.Flush();
38
39 File.Delete(filePath.TrimEnd());//冊除原檔案
40 File.Copy(strLinPath, filePath);//複製加密檔案
41 File.Delete(strLinPath);//冊除臨時檔案
42
43 }
44 catch (Exception e)
45 {
46 throw new Exception(e.Message);
47 }
48 }
這兩個方法已經略微封裝了,拿過來就可以用,只需要傳入檔案路徑即可!如有不妥之處請批評指正。