加减密 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() ...
随机推荐
- postman和fiddler的基本使用
本文转自:https://www.cnblogs.com/qq909283/p/6826578.html 写在前面:本文主要的章节规划: 1.什么是接口测试 另外,有的时候会直接调用别的公司的接口,比 ...
- 实现span设置宽度(行内元素本来不支持调宽度高度这些样式)(变成行内块元素:display:inline-block;)
实现span设置宽度(行内元素本来不支持调宽度高度这些样式)(变成行内块元素:display:inline-block;) 一.总结 1.将span从行内元素变成行内快元素就可以调了: 设置样式的时候 ...
- Android OnGestureListener用法 识别用户手势 左右滑动
Android可以识别用户的手势(即用户用手指滑动的方向),通过用户不同的手势,从而做出不同的处理 需要使用OnGestureListener 比如说看电子书的时候翻页,或者要滑动一些其他内容 直接上 ...
- 【POJ 1226】Substrings
[链接]h在这里写链接 [题意] 给你n个字符串. 让你找一个字符串s. 设s'为这个字符串的逆序. 要求s或者s'在每个字符串里都能够找得到. 并且要求s的长度最长. 求出这个最长的串的长度. [题 ...
- testng并发测试与测试并发
一.testng并发测试 通过xml文件中suit结点的parallel属性指定,如 <suite name="bundle-module-testabc" parallel ...
- 重新配置vim
重新配置,并非折腾,发个链接吧留着以后用。 都是前辈 vimer程序员的世界 Vim(gvim)配色方案推荐 gvim(vim)使用微软雅黑中文字体 Vim(gvim)编程字体推荐 所需即所获:像 I ...
- MFC只允许进行一个实例
APP---InitInstance() 放在所有程序运行前 //只允许运行一个实例 BOOL bfound = FALSE; hmutex = CreateMutex(NULL,TRUE,&quo ...
- [React] Use React.cloneElement to Modify and Add Additional Properties to React Children
In this lesson we'll show how to use React.cloneElement to add additional properties to the children ...
- 《SPA设计与架构》之客户端路由
原文 简书原文:https://www.jianshu.com/p/4d83475f71da 大纲 1.传统路由 2.SPA导航 3.客户端路由器的工作机制 1.传统路由 在传统Web应用程序中,导航 ...
- Mysql错误: ERROR 1205: Lock wait timeout exceeded解决办法(MySQL锁表、事物锁表的处理方法)
Java执行一个SQL查询未提交,遇到1205错误. java.lang.Exception: ### Error updating database. Cause: java.sql.SQLExc ...