winston 发表于 2012-2-17 14:02:18

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         }

  这两个方法已经略微封装了,拿过来就可以用,只需要传入文件路径即可!如有不妥之处请批评指正。

http://www.cnblogs.com/distance/aggbug/2354603.html?type=1本文链接

页: [1]
查看完整版本: DES加密算法,简单使用