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();
}
}
查看全文
相关阅读:
[Canvas学习]变形
[Canvas学习]样式与颜色
[Canvas学习]绘制图形
上海 day31--线程
上海 day31--进程间通信IPC机制、生产者与消费者模型
关于 序列化模块 json 的小问题和小理解!!!
上海 day30--并发编程、进程
上海 day29-- UDP协议通信和socketserver模块
上海 day28--套接字socket
易用常用的小知识点
原文地址:https://www.cnblogs.com/skyakira/p/965357.html
最新文章
第三章 选择结构(一)
第二章 变量、数据类型和运算符
第一章 初识Java
多值信号量
软件定时器
时间管理
时钟节拍
移植 uC/OS-III 到 STM32
uC/OS-III概要
电调控制电机
热门文章
Qt编写计算器(界面)
Qt开发环境搭建(Linux)
QT开发环境搭建(Windows)
[JavaScript语法学习]全面介绍Number
[CSS揭秘]平行四边形
[CSS揭秘]自适应的椭圆
[CSS揭秘]边框内圆角
[CSS揭秘]灵活的背景定位
[CSS揭秘]半透明边框
[Canvas学习]动画
Copyright © 2011-2022 走看看