C# DES加密解密算法

#region DES Class /// <summary> /// ClassName: DES 加密类 /// DES加密、解密类库,字符串加密结果使用BASE64编码返回,支持文件的加密和解密 /// Time:2010-04-30 /// Auther:Hyey.wl /// DES 的摘要说明。 /// </summary> public sealed class DES { /// <summary> /// myiv is iv /// </summary> string myiv = "Hyey20100430"; /// <summary> /// mykey is key /// </summary> string mykey = "HyeyWl30"; /// <summary> /// DES加密偏移量 /// 必须是>=8位长的字符串 /// </summary> public string IV { get { return myiv; } set { myiv = value; } } /// <summary> /// DES加密的私钥 /// 必须是8位长的字符串 /// </summary> public string Key { get { return mykey; } set { mykey = value; } } public DES() { // // TODO: 在此处添加构造函数逻辑 // } /// <summary> /// 对字符串进行DES加密 /// Encrypts the specified sourcestring. /// </summary> /// <param name="sourcestring">The sourcestring.待加密的字符串</param> /// <returns>加密后的BASE64编码的字符串</returns> public string Encrypt(string sourceString) { byte[] btKey = Encoding.Default.GetBytes(mykey); byte[] btIV = Encoding.Default.GetBytes(myiv); DESCryptoServiceProvider des = new DESCryptoServiceProvider(); using (MemoryStream ms = new MemoryStream()) { byte[] inData = Encoding.Default.GetBytes(sourceString); try { using (CryptoStream cs = new CryptoStream(ms, des.CreateEncryptor(btKey, btIV), CryptoStreamMode.Write)) { cs.Write(inData, 0, inData.Length); cs.FlushFinalBlock(); } return Convert.ToBase64String(ms.ToArray()); } catch { throw; } } } /// <summary> /// Decrypts the specified encrypted string. /// 对DES加密后的字符串进行解密 /// </summary> /// <param name="encryptedString">The encrypted string.待解密的字符串</param> /// <returns>解密后的字符串</returns> public string Decrypt(string encryptedString) { byte[] btKey = Encoding.Default.GetBytes(mykey); byte[] btIV = Encoding.Default.GetBytes(myiv); DESCryptoServiceProvider des = new DESCryptoServiceProvider(); using (MemoryStream ms = new MemoryStream()) { byte[] inData = Convert.FromBase64String(encryptedString); try { using (CryptoStream cs = new CryptoStream(ms, des.CreateDecryptor(btKey, btIV), CryptoStreamMode.Write)) { cs.Write(inData, 0, inData.Length); cs.FlushFinalBlock(); } return Encoding.Default.GetString(ms.ToArray()); } catch { throw; } } } /// <summary> /// Encrypts the file. /// 对文件内容进行DES加密 /// </summary> /// <param name="sourceFile">The source file.待加密的文件绝对路径</param> /// <param name="destFile">The dest file.加密后的文件保存的绝对路径</param> public void EncryptFile(string sourceFile, string destFile) { if (!File.Exists(sourceFile)) throw new FileNotFoundException("指定的文件路径不存在!", sourceFile); byte[] btKey = Encoding.Default.GetBytes(mykey); byte[] btIV = Encoding.Default.GetBytes(myiv); DESCryptoServiceProvider des = new DESCryptoServiceProvider(); byte[] btFile = File.ReadAllBytes(sourceFile); using (FileStream fs = new FileStream(destFile, FileMode.Create, FileAccess.Write)) { try { using (CryptoStream cs = new CryptoStream(fs, des.CreateEncryptor(btKey, btIV), CryptoStreamMode.Write)) { cs.Write(btFile, 0, btFile.Length); cs.FlushFinalBlock(); } } catch { throw; } finally { fs.Close(); } } } /// <summary> /// Encrypts the file. /// 对文件内容进行DES加密,加密后覆盖掉原来的文件 /// </summary> /// <param name="sourceFile">The source file.待加密的文件的绝对路径</param> public void EncryptFile(string sourceFile) { EncryptFile(sourceFile, sourceFile); } /// <summary> /// Decrypts the file. /// 对文件内容进行DES解密 /// </summary> /// <param name="sourceFile">The source file.待解密的文件绝对路径</param> /// <param name="destFile">The dest file.解密后的文件保存的绝对路径</param> public void DecryptFile(string sourceFile, string destFile) { if (!File.Exists(sourceFile)) throw new FileNotFoundException("指定的文件路径不存在!", sourceFile); byte[] btKey = Encoding.Default.GetBytes(mykey); byte[] btIV = Encoding.Default.GetBytes(myiv); DESCryptoServiceProvider des = new DESCryptoServiceProvider(); byte[] btFile = File.ReadAllBytes(sourceFile); using (FileStream fs = new FileStream(destFile, FileMode.Create, FileAccess.Write)) { try { using (CryptoStream cs = new CryptoStream(fs, des.CreateDecryptor(btKey, btIV), CryptoStreamMode.Write)) { cs.Write(btFile, 0, btFile.Length); cs.FlushFinalBlock(); } } catch { throw; } finally { fs.Close(); } } } /// <summary> /// Decrypts the file. /// 对文件内容进行DES解密,加密后覆盖掉原来的文件. /// </summary> /// <param name="sourceFile">The source file.待解密的文件的绝对路径.</param> public void DecryptFile(string sourceFile) { DecryptFile(sourceFile, sourceFile); } } #endregion

源:http://www.cnblogs.com/Leo_wl/archive/2010/04/30/1725168.html

转载于:https://www.cnblogs.com/jiang1984j/archive/2010/08/09/1986748.html

C# DES加密解密算法相关推荐

  1. java 实现 DES加密 解密算法

    DES算法的入口参数有三个:Key.Data.Mode.其中Key为8个字节共64位,是DES算法的工作密钥:Data也为8个字节64位,是要被加密或被解密的数据:Mode为DES的工作方式,有两种: ...

  2. DES加密解密算法Java实现

    DES 使用一个 56 位的密钥以及附加的 8 位奇偶校验位,产生最大 64 位的分组大小.这是一个迭代的分组密码,使用称为 Feistel 的技术,其中将加密的文本块分成两半.使用子密钥对其中一半应 ...

  3. DES加密解密算法(前端后端)

    DES加密解密算法(前端&后端) 原作者 阿弥陀佛1114  原文链接:https://blog.csdn.net/zong1114/article/details/51754470 DES对 ...

  4. Java简单实现DES加密解密算法

    Java简单实现DES加密解密算法 文章目录 Java简单实现DES加密解密算法 DES算法介绍 实现相关java类 代码实现 DES算法介绍 DEC加密算法属于对称加密,即利用指定的密钥,按照密码的 ...

  5. 在Java中使用DES加密解密算法

    import javax.crypto.*; import javax.crypto.spec.DESKeySpec; import java.security.NoSuchAlgorithmExce ...

  6. iOS开发-DES加密解密算法

    前几天后台给了一个Java代码的加解密方式,让我这边直接用.我对应着Java上解密方法找到一些适合iOS的DES加解密算法,特总结一下 1.使用DES加密: //加密 +(NSString *) en ...

  7. C++实现DES加密解密算法

    文章目录 第一部分 DES概述 1.1 DES数据加密标准简介 1.2 DES算法流程 第二部分 DES的C++实现 第三部分 代码演示 3.1 待加密的明文文件demo.txt: 3.2 加密解密程 ...

  8. Java实现DES加密解密

    DES(Data Encryption Standard)是一种对称加密算法,所谓对称加密就是加密和解密都是使用同一个密钥. 加密原理: DES 使用一个 56 位的密钥以及附加的 8 位奇偶校验位, ...

  9. C++实现古典密码-凯撒密码加密解密算法

    文章目录 第一部分 Caesar密码简介 1.1 基本思想 1.2 历史沿革 第二部分 Caesar密码的C++实现 第一部分 Caesar密码简介 1.1 基本思想 在密码学中,恺撒密码(英语:Ca ...

最新文章

  1. linux mysql添加用户名和密码错误,linux下为mysql设置用户名和密码
  2. 2020 年 6 月编程语言排行榜,C 稳居第一,Rust 首进 Top 20!
  3. Spark基础-scala学习(三、Trait)
  4. 趣谈网络协议笔记-二(第十七讲)
  5. 用wxpython做ui_单击按钮如何在wxpython中制作其他窗口
  6. 在cmd指令看计算机位数,在.cmd中使用Windows命令来测试32位或64位并运行命令
  7. 用html+css+js实现一个无限级树形控件
  8. 2018年测试状况调查
  9. 题目1022:游船出租(结构体使用)
  10. 【Cisco NA】单臂路由+DHCP+DHCP中继+GRE Tunnel
  11. win10开始屏幕计算机,为什么Win10系统开始屏幕没反应?解决Win10系统开始屏幕没反应的方法...
  12. CAM表含义及各层交换机介绍
  13. Mac 双系统分区合并
  14. 积分商城系统业务逻辑思维导图_怎么开发积分商城系统_OctShop
  15. HDU - 1859 最小长方形
  16. WPS如何生成指定区间随机数
  17. WGS 1984地理坐标系的墨卡托投影分度带(UTM ZONE)选择方法
  18. 数据仓库DW、ODS、DM及其区别
  19. 美元指数K线图怎么看?
  20. android应用开发入门

热门文章

  1. Liunx——参考数据与延伸阅读
  2. Go实现UDP-IP-Data的IP数据报生成
  3. NOIP2017day2题解
  4. RESTful源码学习笔记之RPC和 RESTful 什么区别
  5. “提升业务决策效率:探索FICO Blaze决策引擎的应用“
  6. 梆梆安全的金砖梦想:“安全联结世界”
  7. 修复lsp,360浏览器可以上网其它软件不行
  8. 模块电路选型(4)----通信模块
  9. PHP商城的搜索功能
  10. 一文读懂 MySQL Explain 执行计划