加减密 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() ...
随机推荐
- 【Codeforces Round #434 (Div. 1) B】Polycarp's phone book
[链接]h在这里写链接 [题意] 给你n个电话号码. 让你给每一个电话号码选定一个字符串s; 使得这个串s是这个电话号码的子串. 且不是任何一个其他电话号码的子串. s要求最短. [题解] 字典树. ...
- Java Lock Example – ReentrantLock(java锁的例子)
Welcome to Java Lock example tutorial. Usually when working with multi-threaded environment, we use ...
- 20160206.CCPP体系具体解释(0016天)
代码片段(01):.指针.c+02.间接赋值.c 内容概要:内存 ///01.指针 #include <stdio.h> #include <stdlib.h> //01.取地 ...
- js课程 4-12 js中正则表达式如何使用
js课程 4-12 js中正则表达式如何使用 一.总结 一句话总结: 1.js正则表达式手册取哪里找? w3cschool或者菜鸟教程->找到js正则表达式->完整的RegExp参考手册这 ...
- Android Java使用JavaMail API发送和接收邮件的代码示例
JavaMail是Oracle甲骨文开发的Java邮件类API,支持多种邮件协议,这里我们就来看一下Java使用JavaMail API发送和接收邮件的代码示例 使用Javamail发送邮件,必需的j ...
- 10.13 android输入系统_多点触摸驱动理论与框架
1.多点触摸驱动理论 驱动程序仅上报多个触点的位置就可以,是放大还是缩小由应用程序控制 对于多点触摸驱动在linux系统中有个输入子系统,其已经实现了open/read/write等接口 我们只需要实 ...
- mysql三种带事务批量插入
原文:mysql三种带事务批量插入 c#之mysql三种带事务批量插入 前言 对于像我这样的业务程序员开发一些表单内容是家常便饭的事情,说道表单 我们都避免不了多行内容的提交,多行内容保存,自然要用到 ...
- [WASM] Compile C Code into WebAssembly
We use the C language instead of pure WAST to create a square root function using WASM Fiddle (https ...
- [array] leetCode-26. Remove Duplicates from Sorted Array - Easy
26. Remove Duplicates from Sorted Array - Easy descrition Given a sorted array, remove the duplicate ...
- 正确使用pthread_create,防止内存泄漏
近日,听说pthread_create会造成内存泄漏,觉得不可思议,因此对posix(nptl)的线程创建和销毁进行了分析. 分析结果:如果使用不当,确实会造成内存泄漏. 产生根源:pthread ...