加减密 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() ...
随机推荐
- centos7.0查看IP
原文:centos7.0查看IP 输入ip查询命名 ip addr 也可以输入 ifconfig(centOs7没有ifconfig命令)查看ip,但此命令会出现3个条目,centos的ip地址是e ...
- Linux 内核源代码分析 chap 2 存储管理 (5)
物理页面分配 linux 内核 2.4 中有 2 个版本号的物理页面分配函数 alloc_pages(). 一个在 mm/numa.c 中, 还有一个在 mm/page_alloc.c 中, 依据条件 ...
- 使用Samba在Linux服务器上搭建共享文件服务
最近我们的小团队需要在服务器上共分出一个共享文件夹用于大家存放公共的资源文档, 大家想啊,这肯定很简单呀,在Windows下面只要创建相关的windows account,共享某个文件夹,把读/写权限 ...
- 度量空间(metric space)
一个度量空间(metric space)由一个有序对(ordered pair)(M,d) 表示,其中 M 是一种集合,d 是定义在 M 上的一种度量,是如下的一种函数映射: d:M×M→R 且对于任 ...
- CImage将图片转为指定像素大小
CFileDialog fDlg(true, "jpg", "", OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT, &q ...
- swift学习第四天:swift中的循环
区间for循环 for i in 0..<10 { print(i) } for i in 0...10 { print(i) } 特殊写法 如果在for循环中不需要用到下标i for _ in ...
- libevent源码分析-介绍、安装、使用
Libevent介绍 安装 样例 Libevent介绍 在include\event2\event.h中有关于Libevent的介绍,这里简单翻译介绍一下: Libevent是以事件为驱动的开发可扩展 ...
- 【solr专题之四】在Tomcat 中部署Solr4.x 分类: H_HISTORY 2014-07-17 16:08 1286人阅读 评论(0) 收藏
1.安装Tomcat (1)下载并解压至/opt/tomcat中 # cd /opt/jediael # tar -zxvf apache-tomcat-7.0.54.tar.gz # mv apac ...
- Deepin系统更新apt-get源
1.复制原文件备份sudo cp /etc/apt/source.list /etc/apt/source.list.bak2.编辑源列表文件sudo vim /etc/apt/source.list ...
- 复制相关参数学习笔记--master上的参数
特别声明: 所有的过滤规则不建议在主库上设置. server_id 是一个整数,范围:1 至 power(2,32)-1 之间. 推荐使用端口号+ip最后一位的方式. 唯一区别ID,同一个集群 ...