由于经常用一些加密或者解密文件的,所以写了一个简单的采用Cipher DES针对文件加密、解密的工具类,记录一下,防止以后忘记了!
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
|
import
java.io.BufferedReader;
import
java.io.FileInputStream;
import
java.io.FileOutputStream;
import
java.io.InputStream;
import
java.io.InputStreamReader;
import
java.io.OutputStream;
import
java.security.Key;
import
java.util.ArrayList;
import
java.util.List;
import
javax.crypto.Cipher;
import
javax.crypto.CipherInputStream;
import
javax.crypto.spec.SecretKeySpec;
import
org.slf4j.Logger;
import
org.slf4j.LoggerFactory;
/***
* Des文件加密解密
*/
public
class
FileDeEncrypt {
private
static
final
Logger LOG = LoggerFactory.getLogger(FileDeEncrypt.
class
);
public
FileDeEncrypt(String keyStr) {
genKey(keyStr);
initCipher();
}
private
Key key;
// 解密密码
private
Cipher cipherDecrypt;
// 加密密码
private
Cipher cipherEncrypt;
/**
* 加密文件的核心
*
* @param file
* 要加密的文件
* @param destFile
* 加密后存放的文件名
*/
public
boolean
encryptFile(String srcFileName, String destFileName) {
try
{
InputStream is =
new
FileInputStream(srcFileName);
OutputStream out =
new
FileOutputStream(destFileName);
CipherInputStream cis =
new
CipherInputStream(is, cipherEncrypt);
byte
[] buffer =
new
byte
[
1024
];
int
r;
while
((r = cis.read(buffer)) >
0
) {
out.write(buffer,
0
, r);
}
cis.close();
is.close();
out.close();
LOG.info(
"文件{}加密完成,加密后的文件是:{}"
, srcFileName, destFileName);
return
true
;
}
catch
(Exception e) {
LOG.error(
"加密文件{}出现异常"
, srcFileName, e.getMessage());
e.printStackTrace();
return
false
;
}
}
/***
* 解密文件
*
* @param destFile
*/
public
List
List
new
ArrayList
try
{
InputStream is =
new
FileInputStream(fileName);
CipherInputStream cis =
new
CipherInputStream(is, cipherDecrypt);
BufferedReader reader =
new
BufferedReader(
new
InputStreamReader(cis));
String line =
null
;
while
((line = reader.readLine()) !=
null
) {
list.add(line);
}
reader.close();
cis.close();
is.close();
LOG.info(
"文件{}解密完成"
, fileName);
}
catch
(Exception e) {
LOG.error(
"解密文件{}出现异常"
, fileName, e.getMessage());
e.printStackTrace();
}
return
list;
}
/**
* 解密文件,返回reader
* @param fileName
* @return
*/
public
BufferedReader decryptFile(String fileName) {
BufferedReader reader =
null
;
try
{
InputStream is =
new
FileInputStream(fileName);
CipherInputStream cis =
new
CipherInputStream(is, cipherDecrypt);
reader =
new
BufferedReader(
new
InputStreamReader(cis));
}
catch
(Exception e) {
LOG.error(
"解密文件{}出现异常"
, fileName, e.getMessage());
e.printStackTrace();
}
return
reader;
}
private
void
initCipher() {
try
{
// 加密的cipher
cipherEncrypt = Cipher.getInstance(
"DES"
);
cipherEncrypt.init(Cipher.ENCRYPT_MODE, key);
// 解密的cipher
cipherDecrypt = Cipher.getInstance(
"DES"
);
cipherDecrypt.init(Cipher.DECRYPT_MODE, key);
}
catch
(Exception e) {
LOG.info(
"加密初始化出现异常:{}"
, e.getMessage());
e.printStackTrace();
}
}
/**
* 自定义一个key
*
* @param string
*/
public
void
genKey(String keyRule) {
// Key key = null;
byte
[] keyByte = keyRule.getBytes();
// 创建一个空的八位数组,默认情况下为0
byte
[] byteTemp =
new
byte
[
8
];
// 将用户指定的规则转换成八位数组
for
(
int
i =
0
; i < byteTemp.length && i < keyByte.length; i++) {
byteTemp[i] = keyByte[i];
}
key =
new
SecretKeySpec(byteTemp,
"DES"
);
}
/***
* 测试加密解密
*
* @param args
*/
public
static
void
main(String[] args) {
FileDeEncrypt deEncrypt =
new
FileDeEncrypt(
"STP FILE DE-ENCRYPT"
);
deEncrypt.encryptFile(
"pom.xml"
,
"pom.bin"
);
List
"pom.bin"
);
for
(String str : list) {
System.out.println(str);
}
}
}
|