1. /**//// <summary>
  2. /// DES
  3. /// </summary>
  4. public class DES_
  5. {
  6. private DES mydes;
  7. public string Key;
  8. public string IV;
  9. /**//// <summary>
  10. /// 对称加密类的构造函数
  11. /// </summary>
  12. public DES_(string key)
  13. {
  14. mydes = new DESCryptoServiceProvider();
  15. Key = key;
  16. IV = "728#$$%^TyguyshdsufhsfwofnhKJHJKHIYhfiusf98*(^%$^&&(*&()$##@%%$RHGJJHHJ";
  17. }
  18. /**//// <summary>
  19. /// 对称加密类的构造函数
  20. /// </summary>
  21. public DES_(string key, string iv)
  22. {
  23. mydes = new DESCryptoServiceProvider();
  24. Key = key;
  25. IV = iv;
  26. }
  27. /**//// <summary>
  28. /// 获得密钥
  29. /// </summary>
  30. /// <returns>密钥</returns>
  31. private byte[] GetLegalKey()
  32. {
  33. string sTemp = Key;
  34. mydes.GenerateKey();
  35. byte[] bytTemp = mydes.Key;
  36. int KeyLength = bytTemp.Length;
  37. if (sTemp.Length > KeyLength)
  38. sTemp = sTemp.Substring(, KeyLength);
  39. else if (sTemp.Length < KeyLength)
  40. sTemp = sTemp.PadRight(KeyLength, ' ');
  41. return ASCIIEncoding.ASCII.GetBytes(sTemp);
  42. }
  43. /**//// <summary>
  44. /// 获得初始向量IV
  45. /// </summary>
  46. /// <returns>初试向量IV</returns>
  47. private byte[] GetLegalIV()
  48. {
  49. string sTemp = IV;
  50. mydes.GenerateIV();
  51. byte[] bytTemp = mydes.IV;
  52. int IVLength = bytTemp.Length;
  53. if (sTemp.Length > IVLength)
  54. sTemp = sTemp.Substring(, IVLength);
  55. else if (sTemp.Length < IVLength)
  56. sTemp = sTemp.PadRight(IVLength, ' ');
  57. return ASCIIEncoding.ASCII.GetBytes(sTemp);
  58. }
  59. /**//// <summary>
  60. /// 加密方法
  61. /// </summary>
  62. /// <param name="Source">待加密的串</param>
  63. /// <returns>经过加密的串</returns>
  64. public string Encrypt(string Source)
  65. {
  66. try
  67. {
  68. byte[] bytIn = UTF8Encoding.UTF8.GetBytes(Source);
  69. MemoryStream ms = new MemoryStream();
  70. mydes.Key = GetLegalKey();
  71. mydes.IV = GetLegalIV();
  72. ICryptoTransform encrypto = mydes.CreateEncryptor();
  73. CryptoStream cs = new CryptoStream(ms, encrypto, CryptoStreamMode.Write);
  74. cs.Write(bytIn, , bytIn.Length);
  75. cs.FlushFinalBlock();
  76. ms.Close();
  77. byte[] bytOut = ms.ToArray();
  78. return Convert.ToBase64String(bytOut);
  79. }
  80. catch (Exception ex)
  81. {
  82. throw new Exception("在文件加密的时候出现错误!错误提示: " + ex.Message);
  83. }
  84. }
  85. /**//// <summary>
  86. /// 解密方法
  87. /// </summary>
  88. /// <param name="Source">待解密的串</param>
  89. /// <returns>经过解密的串</returns>
  90. public string Decrypt(string Source)
  91. {
  92. try
  93. {
  94. byte[] bytIn = Convert.FromBase64String(Source);
  95. MemoryStream ms = new MemoryStream(bytIn, , bytIn.Length);
  96. mydes.Key = GetLegalKey();
  97. mydes.IV = GetLegalIV();
  98. ICryptoTransform encrypto = mydes.CreateDecryptor();
  99. CryptoStream cs = new CryptoStream(ms, encrypto, CryptoStreamMode.Read);
  100. StreamReader sr = new StreamReader(cs);
  101. return sr.ReadToEnd();
  102. }
  103. catch (Exception ex)
  104. {
  105. throw new Exception("在文件解密的时候出现错误!错误提示: " + ex.Message);
  106. }
  107. }
  108. /**//// <summary>
  109. /// 加密方法byte[] to byte[]
  110. /// </summary>
  111. /// <param name="Source">待加密的byte数组</param>
  112. /// <returns>经过加密的byte数组</returns>
  113. public byte[] Encrypt(byte[] Source)
  114. {
  115. try
  116. {
  117. byte[] bytIn = Source;
  118. MemoryStream ms = new MemoryStream();
  119. mydes.Key = GetLegalKey();
  120. mydes.IV = GetLegalIV();
  121. ICryptoTransform encrypto = mydes.CreateEncryptor();
  122. CryptoStream cs = new CryptoStream(ms, encrypto, CryptoStreamMode.Write);
  123. cs.Write(bytIn, , bytIn.Length);
  124. cs.FlushFinalBlock();
  125. ms.Close();
  126. byte[] bytOut = ms.ToArray();
  127. return bytOut;
  128. }
  129. catch (Exception ex)
  130. {
  131. throw new Exception("在文件加密的时候出现错误!错误提示: " + ex.Message);
  132. }
  133. }
  134. /**//// <summary>
  135. /// 解密方法byte[] to byte[]
  136. /// </summary>
  137. /// <param name="Source">待解密的byte数组</param>
  138. /// <returns>经过解密的byte数组</returns>
  139. public byte[] Decrypt(byte[] Source)
  140. {
  141. try
  142. {
  143. byte[] bytIn = Source;
  144. MemoryStream ms = new MemoryStream(bytIn, , bytIn.Length);
  145. mydes.Key = GetLegalKey();
  146. mydes.IV = GetLegalIV();
  147. ICryptoTransform encrypto = mydes.CreateDecryptor();
  148. CryptoStream cs = new CryptoStream(ms, encrypto, CryptoStreamMode.Read);
  149. StreamReader sr = new StreamReader(cs);
  150. return UTF8Encoding.UTF8.GetBytes(sr.ReadToEnd());
  151. }
  152. catch (Exception ex)
  153. {
  154. throw new Exception("在文件解密的时候出现错误!错误提示: " + ex.Message);
  155. }
  156. }
  157.  
  158. /**//// <summary>
  159. /// 加密方法File to File
  160. /// </summary>
  161. /// <param name="inFileName">待加密文件的路径</param>
  162. /// <param name="outFileName">待加密后文件的输出路径</param>
  163. public void Encrypt(string inFileName, string outFileName)
  164. {
  165. try
  166. {
  167. FileStream fin = new FileStream(inFileName, FileMode.Open, FileAccess.Read);
  168. FileStream fout = new FileStream(outFileName, FileMode.OpenOrCreate, FileAccess.Write);
  169. fout.SetLength();
  170. mydes.Key = GetLegalKey();
  171. mydes.IV = GetLegalIV();
  172. byte[] bin = new byte[];
  173. long rdlen = ;
  174. long totlen = fin.Length;
  175. int len;
  176. ICryptoTransform encrypto = mydes.CreateEncryptor();
  177. CryptoStream cs = new CryptoStream(fout, encrypto, CryptoStreamMode.Write);
  178. while (rdlen < totlen)
  179. {
  180. len = fin.Read(bin, , );
  181. cs.Write(bin, , len);
  182. rdlen = rdlen + len;
  183. }
  184. cs.Close();
  185. fout.Close();
  186. fin.Close();
  187. }
  188. catch (Exception ex)
  189. {
  190. throw new Exception("在文件加密的时候出现错误!错误提示: " + ex.Message);
  191. }
  192. }
  193. /**//// <summary>
  194. /// 解密方法File to File
  195. /// </summary>
  196. /// <param name="inFileName">待解密文件的路径</param>
  197. /// <param name="outFileName">待解密后文件的输出路径</param>
  198. public void Decrypt(string inFileName, string outFileName)
  199. {
  200. try
  201. {
  202. FileStream fin = new FileStream(inFileName, FileMode.Open, FileAccess.Read);
  203. FileStream fout = new FileStream(outFileName, FileMode.OpenOrCreate, FileAccess.Write);
  204. fout.SetLength();
  205. byte[] bin = new byte[];
  206. long rdlen = ;
  207. long totlen = fin.Length;
  208. int len;
  209. mydes.Key = GetLegalKey();
  210. mydes.IV = GetLegalIV();
  211. ICryptoTransform encrypto = mydes.CreateDecryptor();
  212. CryptoStream cs = new CryptoStream(fout, encrypto, CryptoStreamMode.Write);
  213. while (rdlen < totlen)
  214. {
  215. len = fin.Read(bin, , );
  216. cs.Write(bin, , len);
  217. rdlen = rdlen + len;
  218. }
  219. cs.Close();
  220. fout.Close();
  221. fin.Close();
  222. }
  223. catch (Exception ex)
  224. {
  225. throw new Exception("在文件解密的时候出现错误!错误提示: " + ex.Message);
  226. }
  227. }
  228. }

加减密 DES的更多相关文章

  1. Java中常用加减密方式

    1.加密概述: 加密就是是以某种特殊的算法改变原有的信息数据,使得未授权的用户即使以获得了加密的信息,但因不知解密方式,仍无法了解信息的内容.大体上又分为双向加密和单向加密. 2.单项加密 2.1.概 ...

  2. AES/ECB/NoPadding 加减密

    package unit; import javax.crypto.Cipher; import javax.crypto.spec.SecretKeySpec; import org.apache. ...

  3. Android带加减的edittext

    看了网上这样自带加减的edittext写得好复杂,还有各种监听事件,我觉得没有必有.于是我自己写了一个. 我这个edittext仅仅限制整数,每次加减1. public class TestEditT ...

  4. js实现输入框数量加减【转】

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  5. 自己动手丰衣足食之 jQuery 数量加减插件

    引言 做一个手机端的订单相关项目中,其中下订单时需要用到数量加减的控件,可以设置默认值,也可以设置最大值和最小值.使用jQuery这么长时间了,平时很少去编写属于自己的插件,现在编写的时候对立面的一些 ...

  6. php 时间加减

    <?php date_default_timezone_set('PRC'); //默认时区 echo "今天:",date("Y-m-d",time() ...

  7. freemarker 数据做加减计算

    controller的部分: @Controller@RequestMapping("/ContactsFrameIndex")public class ContactsFrame ...

  8. Oracle中的日期加减

    加法   select sysdate,add_months(sysdate,12) from dual;        --加1年 select sysdate,add_months(sysdate ...

  9. php如何在某个时间上加一天?一小时? 时间加减

    <?php date_default_timezone_set('PRC'); //默认时区 echo "今天:",date("Y-m-d",time() ...

随机推荐

  1. javascript面对对象编程 之继承

    上一篇博客中为大家介绍了javascript面向对象编程原则的封装,今天为大家介绍继承.在javascript中没有类的概念,全部不能像c#.java语言那样.直接的用类去继承类.比方如今有比方.如今 ...

  2. Android onLoadFinished与onLoaderReset

    onLoadFinished 这个方法是在前面已创建的加载器已经完成其加载过程后被调用,这个方法保证会在应用到加载器上的数据被释放之前被调用.在此方法中,你必须删除所有对旧数据的使用(因为它将很快会被 ...

  3. 开发板 视频04_05 ubuntu的联网及基本设置

    4g内存 如果电脑有两g,只能给1.5g 处理器可以根据实际选 usb3.0 或者 2.0 联网模式:: 桥接模式 启动式连接,,,,网是不固定的 仅主机模式,主机和虚拟机在一个网络 第三种联网,自定 ...

  4. es6三点运算符的用法

    扩展运算符( spread )是三个点(...).它好比 rest 参数的逆运算,将一个数组转为用逗号分隔的参数序列. console.log(...[1,2,3]); console.log(1,. ...

  5. (转)nginx的root和alias指令的区别

    转自 http://www.cnblogs.com/tintin1926/archive/2012/07/11/2586813.html nginx配置下有两个指定目录的执行,root和alias   ...

  6. ios sqlite数据库操作

    @interface MyViewController () { // 数据库实例,代表着整个数据库 sqlite3 *_db; } @end @implementation MyViewContro ...

  7. 结合Wireshark捕获分组深入理解TCP/IP协议栈之TCP协议(TCP报文格式+三次握手实例)

    摘要:     本文简单介绍了TCP面向连接理论知识,详细讲述了TCP报文各个字段含义,并从Wireshark俘获分组中选取TCP连接建立相关报文段进行分析. 一.概述     TCP是面向连接的可靠 ...

  8. 31、CMOS摄像头说明

    ov7740(摄像头模块) 输入信号: 自然景观等的模拟信号输出信号: RGB.YUV格式的数字信号 1). 常用参数输入信号: 自然景观等的模拟信号输出信号: 输出格式为:RAW RGB.YUV输出 ...

  9. mysql8 mongodb4 增删改查 性能对比,2019 最专业对比,nosql 真的比 sql 性能强很多?

    原文:mysql8 mongodb4 增删改查 性能对比,2019 最专业对比,nosql 真的比 sql 性能强很多? 版权所有:http://www.fengyunxiao.cn 近几年看了很多关 ...

  10. [PReact] Reduce the Size of a React App in Two Lines with preact-compat

    Not every app is greenfield, and it would be a shame if existing React apps could not benefit from t ...