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();
}
}
查看全文
相关阅读:
104每日博客
924每日博客
921每日博客
928每日博客
929每日博客
930每日博客
927每日博客
大志非才不就,大才非学不成—我的博文资源汇总
vue 项目搭建
Vant 使用记录
原文地址:https://www.cnblogs.com/skyakira/p/965357.html
最新文章
springboot之rest使用与原理
Rest原理
springboot欢迎页的处理规则
springboot的Servlet API
springboot复杂参数
sp资源处理的默认规则
springboot注解
Code: Open URL by a New Browser(转)
ASP.NET控件开发之"代码紧凑原则"(转)
精妙SQL语句
热门文章
Oracle 9i 分析函数参考手册(转)
避免下载Word/Excel文件时被IE开启 (转)
基于NHibernate的三层结构应用程序开发初步
自动处理过长字符串显示的Web控件
让您的Ajax应用加载数据时界面友好
ASP.NET程序中常用的三十三种代码
让Internet Explorer成为你的软件集成平台
IAR中如何定向把数组和函数放在指定的地址单元
923每日博客
101每日博客
Copyright © 2011-2022 走看看