DES加密算法,簡單使用!


  最近一直比較忙,或者說比較懶,都沒有來寫博客,呵呵!這幾天研究的一點小成果拿出來曬一下。

  本來想寫關於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 }


  這兩個方法已經略微封裝了,拿過來就可以用,只需要傳入文件路徑即可!如有不妥之處請批評指正。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM