加减密 DES
- /**//// <summary>
- /// DES
- /// </summary>
- public class DES_
- {
- private DES mydes;
- public string Key;
- public string IV;
- /**//// <summary>
- /// 对称加密类的构造函数
- /// </summary>
- public DES_(string key)
- {
- mydes = new DESCryptoServiceProvider();
- Key = key;
- IV = "728#$$%^TyguyshdsufhsfwofnhKJHJKHIYhfiusf98*(^%$^&&(*&()$##@%%$RHGJJHHJ";
- }
- /**//// <summary>
- /// 对称加密类的构造函数
- /// </summary>
- public DES_(string key, string iv)
- {
- mydes = new DESCryptoServiceProvider();
- Key = key;
- IV = iv;
- }
- /**//// <summary>
- /// 获得密钥
- /// </summary>
- /// <returns>密钥</returns>
- private byte[] GetLegalKey()
- {
- string sTemp = Key;
- mydes.GenerateKey();
- byte[] bytTemp = mydes.Key;
- int KeyLength = bytTemp.Length;
- if (sTemp.Length > KeyLength)
- sTemp = sTemp.Substring(, KeyLength);
- else if (sTemp.Length < KeyLength)
- sTemp = sTemp.PadRight(KeyLength, ' ');
- return ASCIIEncoding.ASCII.GetBytes(sTemp);
- }
- /**//// <summary>
- /// 获得初始向量IV
- /// </summary>
- /// <returns>初试向量IV</returns>
- private byte[] GetLegalIV()
- {
- string sTemp = IV;
- mydes.GenerateIV();
- byte[] bytTemp = mydes.IV;
- int IVLength = bytTemp.Length;
- if (sTemp.Length > IVLength)
- sTemp = sTemp.Substring(, IVLength);
- else if (sTemp.Length < IVLength)
- sTemp = sTemp.PadRight(IVLength, ' ');
- return ASCIIEncoding.ASCII.GetBytes(sTemp);
- }
- /**//// <summary>
- /// 加密方法
- /// </summary>
- /// <param name="Source">待加密的串</param>
- /// <returns>经过加密的串</returns>
- public string Encrypt(string Source)
- {
- try
- {
- byte[] bytIn = UTF8Encoding.UTF8.GetBytes(Source);
- MemoryStream ms = new MemoryStream();
- mydes.Key = GetLegalKey();
- mydes.IV = GetLegalIV();
- ICryptoTransform encrypto = mydes.CreateEncryptor();
- CryptoStream cs = new CryptoStream(ms, encrypto, CryptoStreamMode.Write);
- cs.Write(bytIn, , bytIn.Length);
- cs.FlushFinalBlock();
- ms.Close();
- byte[] bytOut = ms.ToArray();
- return Convert.ToBase64String(bytOut);
- }
- catch (Exception ex)
- {
- throw new Exception("在文件加密的时候出现错误!错误提示: " + ex.Message);
- }
- }
- /**//// <summary>
- /// 解密方法
- /// </summary>
- /// <param name="Source">待解密的串</param>
- /// <returns>经过解密的串</returns>
- public string Decrypt(string Source)
- {
- try
- {
- byte[] bytIn = Convert.FromBase64String(Source);
- MemoryStream ms = new MemoryStream(bytIn, , bytIn.Length);
- mydes.Key = GetLegalKey();
- mydes.IV = GetLegalIV();
- ICryptoTransform encrypto = mydes.CreateDecryptor();
- CryptoStream cs = new CryptoStream(ms, encrypto, CryptoStreamMode.Read);
- StreamReader sr = new StreamReader(cs);
- return sr.ReadToEnd();
- }
- catch (Exception ex)
- {
- throw new Exception("在文件解密的时候出现错误!错误提示: " + ex.Message);
- }
- }
- /**//// <summary>
- /// 加密方法byte[] to byte[]
- /// </summary>
- /// <param name="Source">待加密的byte数组</param>
- /// <returns>经过加密的byte数组</returns>
- public byte[] Encrypt(byte[] Source)
- {
- try
- {
- byte[] bytIn = Source;
- MemoryStream ms = new MemoryStream();
- mydes.Key = GetLegalKey();
- mydes.IV = GetLegalIV();
- ICryptoTransform encrypto = mydes.CreateEncryptor();
- CryptoStream cs = new CryptoStream(ms, encrypto, CryptoStreamMode.Write);
- cs.Write(bytIn, , bytIn.Length);
- cs.FlushFinalBlock();
- ms.Close();
- byte[] bytOut = ms.ToArray();
- return bytOut;
- }
- catch (Exception ex)
- {
- throw new Exception("在文件加密的时候出现错误!错误提示: " + ex.Message);
- }
- }
- /**//// <summary>
- /// 解密方法byte[] to byte[]
- /// </summary>
- /// <param name="Source">待解密的byte数组</param>
- /// <returns>经过解密的byte数组</returns>
- public byte[] Decrypt(byte[] Source)
- {
- try
- {
- byte[] bytIn = Source;
- MemoryStream ms = new MemoryStream(bytIn, , bytIn.Length);
- mydes.Key = GetLegalKey();
- mydes.IV = GetLegalIV();
- ICryptoTransform encrypto = mydes.CreateDecryptor();
- CryptoStream cs = new CryptoStream(ms, encrypto, CryptoStreamMode.Read);
- StreamReader sr = new StreamReader(cs);
- return UTF8Encoding.UTF8.GetBytes(sr.ReadToEnd());
- }
- catch (Exception ex)
- {
- throw new Exception("在文件解密的时候出现错误!错误提示: " + ex.Message);
- }
- }
- /**//// <summary>
- /// 加密方法File to File
- /// </summary>
- /// <param name="inFileName">待加密文件的路径</param>
- /// <param name="outFileName">待加密后文件的输出路径</param>
- public void Encrypt(string inFileName, string outFileName)
- {
- try
- {
- FileStream fin = new FileStream(inFileName, FileMode.Open, FileAccess.Read);
- FileStream fout = new FileStream(outFileName, FileMode.OpenOrCreate, FileAccess.Write);
- fout.SetLength();
- mydes.Key = GetLegalKey();
- mydes.IV = GetLegalIV();
- byte[] bin = new byte[];
- long rdlen = ;
- long totlen = fin.Length;
- int len;
- ICryptoTransform encrypto = mydes.CreateEncryptor();
- CryptoStream cs = new CryptoStream(fout, encrypto, CryptoStreamMode.Write);
- while (rdlen < totlen)
- {
- len = fin.Read(bin, , );
- cs.Write(bin, , len);
- rdlen = rdlen + len;
- }
- cs.Close();
- fout.Close();
- fin.Close();
- }
- catch (Exception ex)
- {
- throw new Exception("在文件加密的时候出现错误!错误提示: " + ex.Message);
- }
- }
- /**//// <summary>
- /// 解密方法File to File
- /// </summary>
- /// <param name="inFileName">待解密文件的路径</param>
- /// <param name="outFileName">待解密后文件的输出路径</param>
- public void Decrypt(string inFileName, string outFileName)
- {
- try
- {
- FileStream fin = new FileStream(inFileName, FileMode.Open, FileAccess.Read);
- FileStream fout = new FileStream(outFileName, FileMode.OpenOrCreate, FileAccess.Write);
- fout.SetLength();
- byte[] bin = new byte[];
- long rdlen = ;
- long totlen = fin.Length;
- int len;
- mydes.Key = GetLegalKey();
- mydes.IV = GetLegalIV();
- ICryptoTransform encrypto = mydes.CreateDecryptor();
- CryptoStream cs = new CryptoStream(fout, encrypto, CryptoStreamMode.Write);
- while (rdlen < totlen)
- {
- len = fin.Read(bin, , );
- cs.Write(bin, , len);
- rdlen = rdlen + len;
- }
- cs.Close();
- fout.Close();
- fin.Close();
- }
- catch (Exception ex)
- {
- throw new Exception("在文件解密的时候出现错误!错误提示: " + ex.Message);
- }
- }
- }
加减密 DES的更多相关文章
- Java中常用加减密方式
1.加密概述: 加密就是是以某种特殊的算法改变原有的信息数据,使得未授权的用户即使以获得了加密的信息,但因不知解密方式,仍无法了解信息的内容.大体上又分为双向加密和单向加密. 2.单项加密 2.1.概 ...
- AES/ECB/NoPadding 加减密
package unit; import javax.crypto.Cipher; import javax.crypto.spec.SecretKeySpec; import org.apache. ...
- Android带加减的edittext
看了网上这样自带加减的edittext写得好复杂,还有各种监听事件,我觉得没有必有.于是我自己写了一个. 我这个edittext仅仅限制整数,每次加减1. public class TestEditT ...
- js实现输入框数量加减【转】
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...
- 自己动手丰衣足食之 jQuery 数量加减插件
引言 做一个手机端的订单相关项目中,其中下订单时需要用到数量加减的控件,可以设置默认值,也可以设置最大值和最小值.使用jQuery这么长时间了,平时很少去编写属于自己的插件,现在编写的时候对立面的一些 ...
- php 时间加减
<?php date_default_timezone_set('PRC'); //默认时区 echo "今天:",date("Y-m-d",time() ...
- freemarker 数据做加减计算
controller的部分: @Controller@RequestMapping("/ContactsFrameIndex")public class ContactsFrame ...
- Oracle中的日期加减
加法 select sysdate,add_months(sysdate,12) from dual; --加1年 select sysdate,add_months(sysdate ...
- php如何在某个时间上加一天?一小时? 时间加减
<?php date_default_timezone_set('PRC'); //默认时区 echo "今天:",date("Y-m-d",time() ...
随机推荐
- javascript面对对象编程 之继承
上一篇博客中为大家介绍了javascript面向对象编程原则的封装,今天为大家介绍继承.在javascript中没有类的概念,全部不能像c#.java语言那样.直接的用类去继承类.比方如今有比方.如今 ...
- Android onLoadFinished与onLoaderReset
onLoadFinished 这个方法是在前面已创建的加载器已经完成其加载过程后被调用,这个方法保证会在应用到加载器上的数据被释放之前被调用.在此方法中,你必须删除所有对旧数据的使用(因为它将很快会被 ...
- 开发板 视频04_05 ubuntu的联网及基本设置
4g内存 如果电脑有两g,只能给1.5g 处理器可以根据实际选 usb3.0 或者 2.0 联网模式:: 桥接模式 启动式连接,,,,网是不固定的 仅主机模式,主机和虚拟机在一个网络 第三种联网,自定 ...
- es6三点运算符的用法
扩展运算符( spread )是三个点(...).它好比 rest 参数的逆运算,将一个数组转为用逗号分隔的参数序列. console.log(...[1,2,3]); console.log(1,. ...
- (转)nginx的root和alias指令的区别
转自 http://www.cnblogs.com/tintin1926/archive/2012/07/11/2586813.html nginx配置下有两个指定目录的执行,root和alias ...
- ios sqlite数据库操作
@interface MyViewController () { // 数据库实例,代表着整个数据库 sqlite3 *_db; } @end @implementation MyViewContro ...
- 结合Wireshark捕获分组深入理解TCP/IP协议栈之TCP协议(TCP报文格式+三次握手实例)
摘要: 本文简单介绍了TCP面向连接理论知识,详细讲述了TCP报文各个字段含义,并从Wireshark俘获分组中选取TCP连接建立相关报文段进行分析. 一.概述 TCP是面向连接的可靠 ...
- 31、CMOS摄像头说明
ov7740(摄像头模块) 输入信号: 自然景观等的模拟信号输出信号: RGB.YUV格式的数字信号 1). 常用参数输入信号: 自然景观等的模拟信号输出信号: 输出格式为:RAW RGB.YUV输出 ...
- mysql8 mongodb4 增删改查 性能对比,2019 最专业对比,nosql 真的比 sql 性能强很多?
原文:mysql8 mongodb4 增删改查 性能对比,2019 最专业对比,nosql 真的比 sql 性能强很多? 版权所有:http://www.fengyunxiao.cn 近几年看了很多关 ...
- [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 ...