|
1 |
| |
|
2 |
| |
|
3 |
| |
|
4 |
| |
|
5 |
| |
|
6 |
| |
|
7 |
| |
|
8 |
| |
|
9 |
| |
|
10 |
| package org.wiztools.wizcrypt; |
|
11 |
| |
|
12 |
| import java.io.IOException; |
|
13 |
| import java.io.InputStream; |
|
14 |
| import java.io.OutputStream; |
|
15 |
| import java.nio.charset.Charset; |
|
16 |
| import java.util.Arrays; |
|
17 |
| import java.util.ResourceBundle; |
|
18 |
| import java.util.zip.GZIPInputStream; |
|
19 |
| import java.util.zip.GZIPOutputStream; |
|
20 |
| import javax.crypto.CipherInputStream; |
|
21 |
| import javax.crypto.CipherOutputStream; |
|
22 |
| |
|
23 |
| |
|
24 |
| |
|
25 |
| |
|
26 |
| |
|
27 |
| |
|
28 |
| |
|
29 |
| public final class WizCrypt { |
|
30 |
| |
|
31 |
| private static final ResourceBundle rb = ResourceBundle.getBundle("org.wiztools.wizcrypt.wizcryptmsg"); |
|
32 |
| |
|
33 |
| |
|
34 |
0
| private WizCrypt() {
|
|
35 |
| } |
|
36 |
| |
|
37 |
| |
|
38 |
| |
|
39 |
| |
|
40 |
| |
|
41 |
| |
|
42 |
| |
|
43 |
| |
|
44 |
| |
|
45 |
| |
|
46 |
| |
|
47 |
| |
|
48 |
| |
|
49 |
| |
|
50 |
| |
|
51 |
| |
|
52 |
| |
|
53 |
| |
|
54 |
| |
|
55 |
| |
|
56 |
| |
|
57 |
| |
|
58 |
3
| public static void encrypt(final InputStream is, final OutputStream os,
|
|
59 |
| final CipherKey ck, final Callback cb, final long size) throws IOException{ |
|
60 |
| |
|
61 |
3
| CipherInputStream cis = null;
|
|
62 |
3
| OutputStream gos = new GZIPOutputStream(os);
|
|
63 |
3
| try{
|
|
64 |
3
| if(cb != null){
|
|
65 |
2
| cb.begin();
|
|
66 |
| } |
|
67 |
| |
|
68 |
3
| cis = new CipherInputStream(is, ck.cipher);
|
|
69 |
| |
|
70 |
| |
|
71 |
3
| byte[] versionStr = FileFormatVersion.WC07.getBytes(WizCryptAlgorithms.STR_ENCODE);
|
|
72 |
3
| gos.write(versionStr);
|
|
73 |
| |
|
74 |
| |
|
75 |
3
| gos.write(ck.passKeyHash);
|
|
76 |
| |
|
77 |
3
| int i = -1;
|
|
78 |
3
| byte[] buffer = new byte[0xFFFF];
|
|
79 |
3
| long readSize = 0;
|
|
80 |
?
| while((i=cis.read(buffer)) != -1){
|
|
81 |
49
| gos.write(buffer, 0, i);
|
|
82 |
49
| readSize += i;
|
|
83 |
49
| if(cb != null){
|
|
84 |
48
| if(size == -1){
|
|
85 |
24
| cb.notifyProgress(readSize);
|
|
86 |
| } |
|
87 |
| else{ |
|
88 |
24
| cb.notifyProgress(readSize * 100 / size);
|
|
89 |
| } |
|
90 |
| } |
|
91 |
| } |
|
92 |
| } |
|
93 |
| finally{ |
|
94 |
3
| try{
|
|
95 |
3
| if(gos != null){
|
|
96 |
3
| gos.close();
|
|
97 |
| } |
|
98 |
| } catch(IOException ioe){ |
|
99 |
0
| System.err.println(ioe.getMessage());
|
|
100 |
| } |
|
101 |
3
| try{
|
|
102 |
3
| if(cis != null){
|
|
103 |
3
| cis.close();
|
|
104 |
| } |
|
105 |
| } catch(IOException ioe){ |
|
106 |
0
| System.err.println(ioe.getMessage());
|
|
107 |
| } |
|
108 |
3
| if(cb != null){
|
|
109 |
2
| cb.end();
|
|
110 |
| } |
|
111 |
| } |
|
112 |
| } |
|
113 |
| |
|
114 |
| |
|
115 |
| |
|
116 |
| |
|
117 |
| |
|
118 |
1
| public static void encrypt(final InputStream is, final OutputStream os,
|
|
119 |
| final CipherKey ck) throws IOException{ |
|
120 |
1
| encrypt(is, os, ck, null, -1);
|
|
121 |
| } |
|
122 |
| |
|
123 |
| |
|
124 |
| |
|
125 |
| |
|
126 |
| |
|
127 |
1
| public static void encrypt(final InputStream is, final OutputStream os,
|
|
128 |
| final CipherKey ck, final Callback cb) throws IOException{ |
|
129 |
1
| encrypt(is, os, ck, cb, -1);
|
|
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 |
4
| public static void decrypt(final InputStream is, final OutputStream os,
|
|
156 |
| final CipherKey ck, final Callback cb, final long size) throws IOException, PasswordMismatchException{ |
|
157 |
| |
|
158 |
4
| CipherOutputStream cos = null;
|
|
159 |
4
| InputStream gis = new GZIPInputStream(is);
|
|
160 |
4
| try{
|
|
161 |
4
| if(cb != null){
|
|
162 |
2
| cb.begin();
|
|
163 |
| } |
|
164 |
| |
|
165 |
| |
|
166 |
4
| byte[] versionStr = FileFormatVersion.WC07.getBytes(WizCryptAlgorithms.STR_ENCODE);
|
|
167 |
4
| int versionByteLen = versionStr.length;
|
|
168 |
4
| byte[] magicNumber = new byte[versionByteLen];
|
|
169 |
4
| gis.read(magicNumber, 0, versionByteLen);
|
|
170 |
4
| System.out.println("magicNumber: "+new String(magicNumber));
|
|
171 |
| |
|
172 |
| |
|
173 |
4
| byte[] filePassKeyHash = new byte[16];
|
|
174 |
4
| gis.read(filePassKeyHash, 0, 16);
|
|
175 |
| |
|
176 |
4
| if(!Arrays.equals(ck.passKeyHash, filePassKeyHash)){
|
|
177 |
1
| throw new PasswordMismatchException(rb.getString("err.pwd.not.match"));
|
|
178 |
| } |
|
179 |
| |
|
180 |
3
| cos = new CipherOutputStream(os, ck.cipher);
|
|
181 |
| |
|
182 |
3
| int i = -1;
|
|
183 |
3
| byte[] buffer = new byte[0xFFFF];
|
|
184 |
3
| long readSize = 0;
|
|
185 |
?
| while((i=gis.read(buffer)) != -1){
|
|
186 |
49
| cos.write(buffer, 0, i);
|
|
187 |
49
| readSize += i;
|
|
188 |
49
| if(cb != null){
|
|
189 |
48
| if(size == -1){
|
|
190 |
24
| cb.notifyProgress(readSize);
|
|
191 |
| } |
|
192 |
| else{ |
|
193 |
24
| cb.notifyProgress(readSize * 100 / size);
|
|
194 |
| } |
|
195 |
| } |
|
196 |
| } |
|
197 |
| } |
|
198 |
| finally{ |
|
199 |
4
| try{
|
|
200 |
4
| if(cos != null){
|
|
201 |
3
| cos.close();
|
|
202 |
| } |
|
203 |
| } catch(IOException ioe){ |
|
204 |
0
| System.err.println(ioe.getMessage());
|
|
205 |
| } |
|
206 |
4
| try{
|
|
207 |
4
| if(gis != null){
|
|
208 |
4
| gis.close();
|
|
209 |
| } |
|
210 |
| } catch(IOException ioe){ |
|
211 |
0
| System.err.println(ioe.getMessage());
|
|
212 |
| } |
|
213 |
4
| if(cb != null){
|
|
214 |
2
| cb.end();
|
|
215 |
| } |
|
216 |
| } |
|
217 |
| } |
|
218 |
| |
|
219 |
| |
|
220 |
| |
|
221 |
| |
|
222 |
| |
|
223 |
2
| public static void decrypt(final InputStream is, final OutputStream os,
|
|
224 |
| final CipherKey ck) throws IOException, PasswordMismatchException{ |
|
225 |
2
| decrypt(is, os, ck, null, -1);
|
|
226 |
| } |
|
227 |
| |
|
228 |
| |
|
229 |
| |
|
230 |
| |
|
231 |
| |
|
232 |
1
| public static void decrypt(final InputStream is, final OutputStream os,
|
|
233 |
| final CipherKey ck, final Callback cb) throws IOException, |
|
234 |
| PasswordMismatchException{ |
|
235 |
1
| decrypt(is, os, ck, cb, -1);
|
|
236 |
| } |
|
237 |
| } |