17
17
18
18
package io .minio .admin ;
19
19
20
+ import io .minio .errors .MinioException ;
20
21
import java .io .ByteArrayOutputStream ;
21
22
import java .io .EOFException ;
22
23
import java .io .IOException ;
@@ -156,22 +157,25 @@ private static byte[] generateKey(byte[] secret, byte[] salt) {
156
157
}
157
158
158
159
private static byte [] generateEncryptDecryptAdditionalData (
159
- boolean encryptFlag , int aeadId , byte [] key , byte [] paddedNonce )
160
- throws InvalidCipherTextException {
161
- AEADCipher cipher = getEncryptCipher (aeadId , key , paddedNonce );
162
- int outputLength = cipher .getMac ().length ;
163
- byte [] additionalData = new byte [outputLength ];
164
- cipher .doFinal (additionalData , 0 );
165
- return appendBytes (new byte [] {0 }, additionalData );
160
+ boolean encryptFlag , int aeadId , byte [] key , byte [] paddedNonce ) throws MinioException {
161
+ try {
162
+ AEADCipher cipher = getEncryptCipher (aeadId , key , paddedNonce );
163
+ int outputLength = cipher .getMac ().length ;
164
+ byte [] additionalData = new byte [outputLength ];
165
+ cipher .doFinal (additionalData , 0 );
166
+ return appendBytes (new byte [] {0 }, additionalData );
167
+ } catch (InvalidCipherTextException e ) {
168
+ throw new MinioException (e );
169
+ }
166
170
}
167
171
168
172
private static byte [] generateEncryptAdditionalData (int aeadId , byte [] key , byte [] paddedNonce )
169
- throws InvalidCipherTextException {
173
+ throws MinioException {
170
174
return generateEncryptDecryptAdditionalData (true , aeadId , key , paddedNonce );
171
175
}
172
176
173
177
private static byte [] generateDecryptAdditionalData (int aeadId , byte [] key , byte [] paddedNonce )
174
- throws InvalidCipherTextException {
178
+ throws MinioException {
175
179
return generateEncryptDecryptAdditionalData (false , aeadId , key , paddedNonce );
176
180
}
177
181
@@ -190,7 +194,7 @@ private static byte[] updateNonceId(byte[] nonce, int idx) {
190
194
}
191
195
192
196
/** Encrypt data payload. */
193
- public static byte [] encrypt (byte [] payload , String password ) throws InvalidCipherTextException {
197
+ public static byte [] encrypt (byte [] payload , String password ) throws MinioException {
194
198
byte [] nonce = random (NONCE_LENGTH );
195
199
byte [] salt = random (SALT_LENGTH );
196
200
@@ -219,7 +223,11 @@ public static byte[] encrypt(byte[] payload, String password) throws InvalidCiph
219
223
int outputLength = cipher .getOutputSize (chunk .length );
220
224
byte [] encryptedData = new byte [outputLength ];
221
225
int outputOffset = cipher .processBytes (chunk , 0 , chunk .length , encryptedData , 0 );
222
- cipher .doFinal (encryptedData , outputOffset );
226
+ try {
227
+ cipher .doFinal (encryptedData , outputOffset );
228
+ } catch (InvalidCipherTextException e ) {
229
+ throw new MinioException (e );
230
+ }
223
231
224
232
result = appendBytes (result , encryptedData );
225
233
@@ -243,20 +251,24 @@ public static class DecryptReader {
243
251
private byte [] oneByte = null ;
244
252
private boolean eof = false ;
245
253
246
- public DecryptReader (InputStream inputStream , byte [] secret )
247
- throws EOFException , IOException , InvalidCipherTextException {
254
+ public DecryptReader (InputStream inputStream , byte [] secret ) throws MinioException {
248
255
this .inputStream = inputStream ;
249
256
this .secret = secret ;
250
- readFully (this .inputStream , this .salt , true );
251
- readFully (this .inputStream , this .aeadId , true );
252
- readFully (this .inputStream , this .nonce , true );
257
+ try {
258
+ readFully (this .inputStream , this .salt , true );
259
+ readFully (this .inputStream , this .aeadId , true );
260
+ readFully (this .inputStream , this .nonce , true );
261
+ } catch (EOFException e ) {
262
+ throw new MinioException (e );
263
+ } catch (IOException e ) {
264
+ throw new MinioException (e );
265
+ }
253
266
this .key = generateKey (this .secret , this .salt );
254
267
byte [] paddedNonce = appendBytes (this .nonce , new byte [] {0 , 0 , 0 , 0 });
255
268
this .additionalData = generateDecryptAdditionalData (this .aeadId [0 ], this .key , paddedNonce );
256
269
}
257
270
258
- private byte [] decrypt (byte [] encryptedData , boolean lastChunk )
259
- throws InvalidCipherTextException {
271
+ private byte [] decrypt (byte [] encryptedData , boolean lastChunk ) throws MinioException {
260
272
this .count ++;
261
273
if (lastChunk ) {
262
274
this .additionalData = markAsLast (this .additionalData );
@@ -268,12 +280,16 @@ private byte[] decrypt(byte[] encryptedData, boolean lastChunk)
268
280
byte [] decryptedData = new byte [outputLength ];
269
281
int outputOffset =
270
282
cipher .processBytes (encryptedData , 0 , encryptedData .length , decryptedData , 0 );
271
- cipher .doFinal (decryptedData , outputOffset );
283
+ try {
284
+ cipher .doFinal (decryptedData , outputOffset );
285
+ } catch (InvalidCipherTextException e ) {
286
+ throw new MinioException (e );
287
+ }
272
288
return decryptedData ;
273
289
}
274
290
275
291
/** Read a chunk at least one byte more than chunk size. */
276
- private byte [] readChunk () throws IOException {
292
+ private byte [] readChunk () throws EOFException , IOException {
277
293
if (this .eof ) {
278
294
return new byte [] {};
279
295
}
@@ -302,19 +318,24 @@ private byte[] readChunk() throws IOException {
302
318
return baos .toByteArray ();
303
319
}
304
320
305
- public byte [] readAllBytes () throws IOException , InvalidCipherTextException {
321
+ public byte [] readAllBytes () throws MinioException {
306
322
ByteArrayOutputStream baos = new ByteArrayOutputStream ();
307
323
while (!this .eof ) {
308
- byte [] payload = this .readChunk ();
309
- baos .write (this .decrypt (payload , this .eof ));
324
+ try {
325
+ byte [] payload = this .readChunk ();
326
+ baos .write (this .decrypt (payload , this .eof ));
327
+ } catch (EOFException e ) {
328
+ throw new MinioException (e );
329
+ } catch (IOException e ) {
330
+ throw new MinioException (e );
331
+ }
310
332
}
311
333
return baos .toByteArray ();
312
334
}
313
335
}
314
336
315
337
/** Decrypt data stream. */
316
- public static byte [] decrypt (InputStream inputStream , String password )
317
- throws EOFException , IOException , InvalidCipherTextException {
338
+ public static byte [] decrypt (InputStream inputStream , String password ) throws MinioException {
318
339
DecryptReader reader =
319
340
new DecryptReader (inputStream , password .getBytes (StandardCharsets .UTF_8 ));
320
341
return reader .readAllBytes ();
0 commit comments