zoukankan
html css js c++ java
DES文件加密
using
System;
using
System.IO;
using
System.Security;
using
System.Security.Cryptography;
using
System.Runtime.InteropServices;
using
System.Text;
/**/
///
<summary>
///
FileEncryptDecrypt 的摘要说明
///
</summary>
public
class
FileEncryptDecrypt
{
byte
[] desIV
=
{
0x01
,
0x02
,
0x03
,
0x04
,
0x05
,
0x06
,
0x07
,
0x08
}
;
byte
[] desKey
=
{ }
;
public
FileEncryptDecrypt()
{
//
//
TODO: 在此处添加构造函数逻辑
//
}
public
void
getKey(
string
keyString)
{
//
根据密码算出密钥
if
(keyString.Length
>=
8
)
{
desKey
=
new
byte
[]
{(
byte
)keyString[
0
],(
byte
)keyString[
1
],(
byte
)keyString[
2
],
(
byte
)keyString[
3
],(
byte
)keyString[
4
],(
byte
)keyString[
5
],
(
byte
)keyString[
6
],(
byte
)keyString[
7
]}
;
}
if
(keyString.Length
==
6
)
{
desKey
=
new
byte
[]
{(
byte
)keyString[
0
],(
byte
)keyString[
1
],(
byte
)keyString[
2
],
(
byte
)keyString[
3
],(
byte
)keyString[
4
],(
byte
)keyString[
5
],
0x07
,
0x08
}
;
}
if
(keyString.Length
==
7
)
{
desKey
=
new
byte
[]
{(
byte
)keyString[
0
],(
byte
)keyString[
1
],(
byte
)keyString[
2
],
(
byte
)keyString[
3
],(
byte
)keyString[
4
],(
byte
)keyString[
5
],
(
byte
)keyString[
6
],
0x07
}
;
}
}
public
void
DecryptFile(
string
inName,
string
outName)
{
try
{
//
创建文件流分别指向输入和输出文件
FileStream fin
=
new
FileStream(inName, FileMode.Open, FileAccess.Read);
FileStream fout
=
new
FileStream(outName, FileMode.OpenOrCreate, FileAccess.Write);
fout.SetLength(
0
);
//
每次的中间流.
byte
[] bin
=
new
byte
[
100
];
//
代表已经解密的流的大小
int
complete
=
0
;
//
代表要解密文件总的大小
long
totlen
=
fin.Length;
//
每次写入的大小
int
len;
//
创建DES对象
DES des
=
new
DESCryptoServiceProvider();
//
创建解密流
CryptoStream decStream
=
new
CryptoStream(fout, des.CreateDecryptor(desKey, desIV), CryptoStreamMode.Write);
//
从输入文件中读取流,然后解密到输出文件中
while
(complete
<
totlen)
{
len
=
fin.Read(bin,
0
,
100
);
decStream.Write(bin,
0
, len);
complete
=
complete
+
len;
}
//
关闭流
decStream.Close();
fout.Close();
fin.Close();
}
catch
(Exception error)
{
//
密码不正确的警告
throw
error;
}
}
public
void
EncryptFile(
string
inName,
string
outName)
{
//
创建文件流分别指向输入和输出文件
FileStream fin
=
new
FileStream(inName, FileMode.Open, FileAccess.Read);
FileStream fout
=
new
FileStream(outName, FileMode.OpenOrCreate, FileAccess.Write);
fout.SetLength(
0
);
//
每次的中间流.
byte
[] bin
=
new
byte
[
100
];
//
代表已经加密的流的大小
int
complete
=
0
;
//
代表要加密文件总的大小
long
totlen
=
fin.Length;
//
每次写入的大小
int
len;
//
创建DES对象
DES des
=
new
DESCryptoServiceProvider();
//
创建加密流
CryptoStream encStream
=
new
CryptoStream(fout, des.CreateEncryptor(desKey, desIV), CryptoStreamMode.Write);
//
从输入文件中读取流,然后加密到输出文件中
while
(complete
<
totlen)
{
len
=
fin.Read(bin,
0
,
100
);
encStream.Write(bin,
0
, len);
complete
=
complete
+
len;
}
//
关闭流
encStream.Close();
fout.Close();
fin.Close();
}
}
查看全文
相关阅读:
console报错:No mapping found for HTTP request with URI(xxx)
jquery,ajax详解
spring-mvc.xml 和 application-context.xml的区别
robot
Linux记录history命令
防火墙知识小结
wireshark语法小结
https和证书小结
生成自签名证书:生成证书和秘钥
查看win信任的证书办法机构(CA机构的公钥)
原文地址:https://www.cnblogs.com/skyakira/p/965357.html
最新文章
个人任务day7
2017.12.23第十四周学习进度04
个人任务day6
个人任务day5
如何查看某个端口被谁占用
@Controller和@RestController的区别
spring的@Transactional(rollbackFor=Exception.class)的使用
Java 读取 json文件
javascript计算两个时间差
js获取当前的日期时间 格式“yyyy-MM-dd HH:MM:SS”
热门文章
Jenkins持续集成环境, 如何自定义 maven 仓库
Jenkins构建时间Poll Scm的设置
linux 删除命令
mybatis入门
【转】Spring、Spring MVC、MyBatis整合文件配置详解
springmvc整合mybatis 配置文件
jakson
jackson java对象和json对象的互相转换
springMVC数据交互
MAP使用方法集合
Copyright © 2011-2022 走看看