/**//// <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的更多相关文章

  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. 国密算法SM2证书制作

    国密算法sm2非对称算法椭圆曲线 原文:http://www.jonllen.cn/jonllen/work/162.aspx 前段时间将系统的RSA算法全部升级为SM2国密算法,密码机和UKey硬件 ...

  2. springmvc hibernate整合

    今天复习一下SpringMVC+Hibernate的搭建,本来想着将Spring-Security权限控制框架也映入其中的,但是发现内容太多 了,Spring-Security的就留在下一篇吧,这篇主 ...

  3. springmvc+shiro+freemarker实现的安全及权限管理

    本文讲述了基于springmvc+shiro实现安全管理,shiro+freemarker实现权限验证. 首先我们从web.xml开始: <?xml version="1.0" ...

  4. 【Eclipse提高开发速度-插件篇】Checkstyle的使用

    1.CheckStyle是SourceForge下的一个项目,提供了一个帮助JAVA开发者遵守某些编码规范的工具. CheckStyle提供了大部分功能都是对于代码规范的检查 CheckStyle检验 ...

  5. maven 配置Project Facets时further configuration available不出来问题

    如果下边的 further configuration available不出来 把Dynamic web module 去掉勾选,应用与项目,然后再点开项目的properties,再选中Dynami ...

  6. 算法 Tricks(四)—— 判断序列中的字符/数值是否交替出现

    比如:353, 54545,数字都是交替出现的: bool alternate = true; for (int i = 0; i < M.size(); ++i){ if (M[i] != M ...

  7. MyBatis Generator插件之SerializablePlugin

    org.mybatis.generator.plugins.SerializablePlugin 在generatorConfig.xml中加上配置: <plugin type="or ...

  8. URL validation failed. The error could have been caused through the use of the browser&#39;s navigation

    URL validation failed. The error could have been caused through the use of the browser's navigation ...

  9. [Angular] Using InjectionToken

    Previously we have 'OpaqueToken', but it is DEPRECATED. The new one is called 'InjectionToken'. The ...

  10. 证明的手段 —— 不失一般性的(WLOG)

    不失一般性是数学中一个常见的表达.不失一般性(Without loss of generality,缩写:WLOG.WOLOG 或 w.l.o.g.)是数学中一个常见的表达. 如果给不相等的两数,a, ...