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();
}
}
查看全文
相关阅读:
UVA 11149.Power of Matrix-矩阵快速幂倍增
51nod 1137.矩阵乘法-矩阵乘法
HDU 4920.Matrix multiplication-矩阵乘法
HDU 6237.A Simple Stone Game-欧拉函数找素因子 (2017中国大学生程序设计竞赛-哈尔滨站-重现赛)
HDU 6235.Permutation (2017中国大学生程序设计竞赛-哈尔滨站-重现赛)
POJ 2226.Muddy Fields-二分图最大匹配(最小点覆盖)
POJ 3041.Asteroids-Hungary(匈牙利算法)
HDU 2063.过山车-Hungary(匈牙利算法)
Codeforces 832 B. Petya and Exam-字符串匹配
HDU 6038.Function-数学+思维 (2017 Multi-University Training Contest
原文地址:https://www.cnblogs.com/skyakira/p/965357.html
最新文章
LeetCode
LeetCode
剑指Offer
剑指Offer
剑指Offer
质因数分解
noi 第n小的质数
157. [USACO Nov07] 奶牛跨栏(第三次考试大整理)
186. [USACO Oct08] 牧场旅行 (第三次考试大整理)
185.[USACO Oct08] 挖水井 (第三次考试大整理)
热门文章
[CSU1809] Parenthesis(RMQ)
[51NOD]2 3 5 7的倍数(容斥原理)
[POJ2761]Feed the dogs(静态区间第k小,主席树)
[CF580D]Kefa and Dishes(状压dp)
[HDOJ1492]Happy 2004(数论,快速幂,逆元,积性函数)
[51NOD1272]最大距离(贪心)
[HDOJ1540]Tunnel Warfare(线段树)
[HDOJ1395]2^x mod n = 1(欧拉函数)
[UVA1434] YAPTCHA(数论,威尔逊定理)
[HDOJ1806]Frequent values(RMQ,ST)
Copyright © 2011-2022 走看看