mirror of https://github.com/OpenIdentityPlatform/OpenDJ.git

neil_a_wilson
15.34.2006 059f1eb59194d5a7484b876fbbc9a938521e15a9
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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
/*
 * CDDL HEADER START
 *
 * The contents of this file are subject to the terms of the
 * Common Development and Distribution License, Version 1.0 only
 * (the "License").  You may not use this file except in compliance
 * with the License.
 *
 * You can obtain a copy of the license at
 * trunk/opends/resource/legal-notices/OpenDS.LICENSE
 * or https://OpenDS.dev.java.net/OpenDS.LICENSE.
 * See the License for the specific language governing permissions
 * and limitations under the License.
 *
 * When distributing Covered Code, include this CDDL HEADER in each
 * file and include the License file at
 * trunk/opends/resource/legal-notices/OpenDS.LICENSE.  If applicable,
 * add the following below this CDDL HEADER, with the fields enclosed
 * by brackets "[]" replaced with your own identifying * information:
 *      Portions Copyright [yyyy] [name of copyright owner]
 *
 * CDDL HEADER END
 *
 *
 *      Portions Copyright 2006 Sun Microsystems, Inc.
 */
package org.opends.server.core;
 
 
 
import java.io.InputStream;
import java.io.IOException;
import java.security.GeneralSecurityException;
import java.security.InvalidAlgorithmParameterException;
import java.security.InvalidKeyException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.security.SecureRandom;
import java.util.zip.DataFormatException;
import java.util.zip.Deflater;
import java.util.zip.Inflater;
import javax.crypto.Cipher;
import javax.crypto.Mac;
import javax.crypto.NoSuchPaddingException;
import javax.crypto.SecretKey;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;
 
import org.opends.server.config.ConfigException;
 
import static org.opends.server.loggers.Debug.*;
import static org.opends.server.util.StaticUtils.*;
 
 
 
/**
 * This class provides the interface to the Directory Server cryptographic
 * framework, which may be used for hashing, encryption, and other kinds of
 * cryptographic operations.  Note that it also contains methods for compressing
 * and uncompressing data.  Although these are not strictly cryptographic
 * operations, there are a lot of similarities and it may be conceivable at
 * some point that accelerated compression may be available just as it is for
 * cryptographic operations.
 */
public class CryptoManager
{
  /**
   * The fully-qualified name of this class for debugging purposes.
   */
  private static final String CLASS_NAME =
       "org.opends.server.core.CryptoManager";
 
 
 
  // The default secret key that we will use for encryption and decryption.
  private SecretKey secretKey;
 
  // The preferred cipher for the Directory Server.
  private String preferredCipher;
 
  // The preferred message digest algorithm for the Directory Server.
  private String preferredDigestAlgorithm;
 
  // The preferred MAC algorithm for the Directory Server.
  private String preferredMACAlgorithm;
 
 
 
  /**
   * Creates a new instance of this crypto manager object.
   *
   * @throws  ConfigException  If a problem occurs while creating this crypto
   *                           manager that is a result of a problem in the
   *                           configuration.
   *
   * @throws  InitializationException  If a problem occurs while creating this
   *                                   crypto manager that is not the result of
   *                                   a problem in the configuration.
   */
  public CryptoManager()
         throws ConfigException, InitializationException
  {
    assert debugConstructor(CLASS_NAME);
 
    // FIXME -- Get the defaults from the configuration rather than hard-coding
    // them.
    preferredDigestAlgorithm = "SHA-1";
    preferredMACAlgorithm    = "HmacSHA1";
    preferredCipher          = "AES/CBC/PKCS5Padding";
 
 
    // FIXME -- Use a much more secure way of constructing the secret key.
    secretKey = new SecretKeySpec(new byte[16], "AES");
 
 
    // Make sure that we can create instances of the preferred digest, MAC, and
    // cipher algorithms.
    try
    {
      MessageDigest.getInstance(preferredDigestAlgorithm);
    }
    catch (Exception e)
    {
      assert debugException(CLASS_NAME, "<init>", e);
 
      // FIXME -- Number this.
      throw new InitializationException(-1, "Can't get preferred digest:  " +
                                        stackTraceToSingleLineString(e), e);
    }
 
    try
    {
      Mac mac = Mac.getInstance(preferredMACAlgorithm);
      mac.init(secretKey);
    }
    catch (Exception e)
    {
      assert debugException(CLASS_NAME, "<init>", e);
 
      // FIXME -- Number this.
      throw new InitializationException(-1, "Can't get preferred MAC " +
                                        "provider:  " +
                                        stackTraceToSingleLineString(e), e);
    }
 
    try
    {
      Cipher cipher = Cipher.getInstance(preferredCipher);
      cipher.init(Cipher.ENCRYPT_MODE, secretKey);
    }
    catch (Exception e)
    {
      assert debugException(CLASS_NAME, "<init>", e);
 
      // FIXME -- Number this.
      throw new InitializationException(-1, "Can't get preferred cipher:  " +
                                        stackTraceToSingleLineString(e), e);
    }
  }
 
 
 
  /**
   * Retrieves an instance of a secure random number generator.
   *
   * @return  An instance of a secure random number generator.
   */
  public SecureRandom getSecureRandom()
  {
    assert debugEnter(CLASS_NAME, "getSecureRandom");
 
    // FIXME -- Is this threadsafe?  Can we share a single instance among all
    // threads?
    return new SecureRandom();
  }
 
 
 
  /**
   * Retrieves the name of the preferred message digest algorithm.
   *
   * @return  The name of the preferred message digest algorithm
   */
  public String getPreferredMessageDigestAlgorithm()
  {
    assert debugEnter(CLASS_NAME, "getPreferredMessageDigestAlgorithm");
 
    return preferredDigestAlgorithm;
  }
 
 
 
  /**
   * Retrieves a <CODE>MessageDigest</CODE> object that may be used to generate
   * digests using the preferred digest algorithm.
   *
   * @return  A <CODE>MessageDigest</CODE> object that may be used to generate
   *          digests using the preferred digest algorithm.
   *
   * @throws  NoSuchAlgorithmException  If the requested algorithm is not
   *                                    supported or is unavailable.
   */
  public MessageDigest getPreferredMessageDigest()
         throws NoSuchAlgorithmException
  {
    assert debugEnter(CLASS_NAME, "getPreferredMessageDigest");
 
    return MessageDigest.getInstance(preferredDigestAlgorithm);
  }
 
 
 
  /**
   * Retrieves a <CODE>MessageDigest</CODE> object that may be used to generate
   * digests using the specified algorithm.
   *
   * @param  digestAlgorithm  The algorithm to use to generate the message
   *                          digest.
   *
   * @return  A <CODE>MessageDigest</CODE> object that may be used to generate
   *          digests using the specified algorithm.
   *
   * @throws  NoSuchAlgorithmException  If the requested algorithm is not
   *                                    supported or is unavailable.
   */
  public MessageDigest getMessageDigest(String digestAlgorithm)
         throws NoSuchAlgorithmException
  {
    assert debugEnter(CLASS_NAME, "getMessageDigest",
                      String.valueOf(digestAlgorithm));
 
    return MessageDigest.getInstance(digestAlgorithm);
  }
 
 
 
  /**
   * Retrieves a byte array containing a message digest based on the provided
   * data, using the preferred digest algorithm.
   *
   * @param  data  The data to be digested.
   *
   * @return  A byte array containing the generated message digest.
   *
   * @throws  NoSuchAlgorithmException  If the requested algorithm is not
   *                                    supported or is unavailable.
   */
  public byte[] digest(byte[] data)
         throws NoSuchAlgorithmException
  {
    assert debugEnter(CLASS_NAME, "digest", "byte[" + data.length + "]");
 
    return MessageDigest.getInstance(preferredDigestAlgorithm).digest(data);
  }
 
 
 
  /**
   * Retrieves a byte array containing a message digest based on the provided
   * data, using the requested digest algorithm.
   *
   * @param  digestAlgorithm  The algorithm to use to generate the message
   *                          digest.
   * @param  data             The data to be digested.
   *
   * @return  A byte array containing the generated message digest.
   *
   * @throws  NoSuchAlgorithmException  If the requested algorithm is not
   *                                    supported or is unavailable.
   */
  public byte[] digest(String digestAlgorithm, byte[] data)
         throws NoSuchAlgorithmException
  {
    assert debugEnter(CLASS_NAME, "digest", String.valueOf(digestAlgorithm),
                      "byte[" + data.length + "]");
 
    return MessageDigest.getInstance(digestAlgorithm).digest(data);
  }
 
 
 
  /**
   * Retrieves a byte array containing a message digest based on the data read
   * from the provided input stream, using the preferred digest algorithm.  Data
   * will be read until the end of the stream is reached.
   *
   * @param  inputStream  The input stream from which the data is to be read.
   *
   * @return  A byte array containing the generated message digest.
   *
   * @throws  IOException  If a problem occurs while reading data from the
   *                       provided stream.
   *
   * @throws  NoSuchAlgorithmException  If the requested algorithm is not
   *                                    supported or is unavailable.
   */
  public byte[] digest(InputStream inputStream)
         throws IOException, NoSuchAlgorithmException
  {
    assert debugEnter(CLASS_NAME, "digest", "java.io.InputStream");
 
    MessageDigest digest = MessageDigest.getInstance(preferredDigestAlgorithm);
 
    byte[] buffer = new byte[8192];
    while (true)
    {
      int bytesRead = inputStream.read(buffer);
      if (bytesRead < 0)
      {
        break;
      }
 
      digest.update(buffer, 0, bytesRead);
    }
 
    return digest.digest();
  }
 
 
 
  /**
   * Retrieves a byte array containing a message digest based on the data read
   * from the provided input stream, using the requested digest algorithm.  Data
   * will be read until the end of the stream is reached.
   *
   * @param  digestAlgorithm  The algorithm to use to generate the message
   *                          digest.
   * @param  inputStream      The input stream from which the data is to be
   *                          read.
   *
   * @return  A byte array containing the generated message digest.
   *
   * @throws  IOException  If a problem occurs while reading data from the
   *                       provided stream.
   *
   * @throws  NoSuchAlgorithmException  If the requested algorithm is not
   *                                    supported or is unavailable.
   */
  public byte[] digest(String digestAlgorithm, InputStream inputStream)
         throws IOException, NoSuchAlgorithmException
  {
    assert debugEnter(CLASS_NAME, "digest", "java.io.InputStream");
 
    MessageDigest digest = MessageDigest.getInstance(digestAlgorithm);
 
    byte[] buffer = new byte[8192];
    while (true)
    {
      int bytesRead = inputStream.read(buffer);
      if (bytesRead < 0)
      {
        break;
      }
 
      digest.update(buffer, 0, bytesRead);
    }
 
    return digest.digest();
  }
 
 
 
  /**
   * Retrieves the name of the preferred MAC algorithm.
   *
   * @return  The name of the preferred MAC algorithm
   */
  public String getPreferredMACAlgorithm()
  {
    assert debugEnter(CLASS_NAME, "getPreferredMessageDigestAlgorithm");
 
    return preferredMACAlgorithm;
  }
 
 
 
  /**
   * Retrieves a MAC provider using the preferred algorithm.
   *
   * @return  A MAC provider using the preferred algorithm.
   *
   * @throws  NoSuchAlgorithmException  If the requested algorithm is not
   *                                    supported or is unavailable.
   *
   * @throws  InvalidKeyException  If the provided key is not appropriate for
   *                               use with the requested MAC algorithm.
   */
  public Mac getPreferredMACProvider()
         throws NoSuchAlgorithmException, InvalidKeyException
  {
    assert debugEnter(CLASS_NAME, "getPreferredMACProvider");
 
    Mac mac = Mac.getInstance(preferredMACAlgorithm);
    mac.init(secretKey);
 
    return mac;
  }
 
 
 
  /**
   * Retrieves a MAC provider using the specified algorithm.
   *
   * @param  macAlgorithm  The algorithm to use for the MAC provider.
   *
   * @return  A MAC provider using the specified algorithm.
   *
   * @throws  NoSuchAlgorithmException  If the requested algorithm is not
   *                                    supported or is unavailable.
   *
   * @throws  InvalidKeyException  If the provided key is not appropriate for
   *                               use with the requested MAC algorithm.
   */
  public Mac getMACProvider(String macAlgorithm)
         throws NoSuchAlgorithmException, InvalidKeyException
  {
    assert debugEnter(CLASS_NAME, "getMACProvider",
                      String.valueOf(macAlgorithm));
 
    Mac mac = Mac.getInstance(macAlgorithm);
    mac.init(secretKey);
 
    return mac;
  }
 
 
 
  /**
   * Retrieves a byte array containing a MAC based on the provided data, using
   * the preferred MAC algorithm.
   *
   * @param  data  The data for which to generate the MAC.
   *
   * @return  A byte array containing the generated MAC.
   *
   * @throws  NoSuchAlgorithmException  If the requested algorithm is not
   *                                    supported or is unavailable.
   */
  public byte[] mac(byte[] data)
         throws NoSuchAlgorithmException
  {
    assert debugEnter(CLASS_NAME, "mac", "byte[" + data.length + "]");
 
    return Mac.getInstance(preferredMACAlgorithm).doFinal(data);
  }
 
 
 
  /**
   * Retrieves a byte array containing a MAC based on the provided data, using
   * the requested MAC algorithm.
   *
   * @param  macAlgorithm  The algorithm to use for the MAC.
   * @param  data          The data for which to generate the MAC.
   *
   * @return  A byte array containing the generated MAC.
   *
   * @throws  NoSuchAlgorithmException  If the requested algorithm is not
   *                                    supported or is unavailable.
   */
  public byte[] mac(String macAlgorithm, byte[] data)
         throws NoSuchAlgorithmException
  {
    assert debugEnter(CLASS_NAME, "mac", String.valueOf(macAlgorithm),
                      "byte[" + data.length + "]");
 
    return Mac.getInstance(macAlgorithm).doFinal(data);
  }
 
 
 
  /**
   * Retrieves a byte array containing a MAC based on the data read from the
   * provided input stream, using the preferred MAC algorithm.  Data will be
   * read until the end of the stream is reached.
   *
   * @param  inputStream  The input stream from which the data is to be read.
   *
   * @return  A byte array containing the generated MAC.
   *
   * @throws  IOException  If a problem occurs while reading data from the
   *                       provided stream.
   *
   * @throws  NoSuchAlgorithmException  If the requested algorithm is not
   *                                    supported or is unavailable.
   */
  public byte[] mac(InputStream inputStream)
         throws IOException, NoSuchAlgorithmException
  {
    assert debugEnter(CLASS_NAME, "digest", "java.io.InputStream");
 
    Mac mac = Mac.getInstance(preferredMACAlgorithm);
 
    byte[] buffer = new byte[8192];
    while (true)
    {
      int bytesRead = inputStream.read(buffer);
      if (bytesRead < 0)
      {
        break;
      }
 
      mac.update(buffer, 0, bytesRead);
    }
 
    return mac.doFinal();
  }
 
 
 
  /**
   * Retrieves a byte array containing a MAC based on the data read from the
   * provided input stream, using the preferred MAC algorithm.  Data will be
   * read until the end of the stream is reached.
   *
   * @param  macAlgorithm  The algorithm to use for the MAC.
   * @param  inputStream   The input stream from which the data is to be read.
   *
   * @return  A byte array containing the generated MAC.
   *
   * @throws  IOException  If a problem occurs while reading data from the
   *                       provided stream.
   *
   * @throws  NoSuchAlgorithmException  If the requested algorithm is not
   *                                    supported or is unavailable.
   */
  public byte[] mac(String macAlgorithm, InputStream inputStream)
         throws IOException, NoSuchAlgorithmException
  {
    assert debugEnter(CLASS_NAME, "digest", String.valueOf(macAlgorithm),
                      "java.io.InputStream");
 
    Mac mac = Mac.getInstance(macAlgorithm);
 
    byte[] buffer = new byte[8192];
    while (true)
    {
      int bytesRead = inputStream.read(buffer);
      if (bytesRead < 0)
      {
        break;
      }
 
      mac.update(buffer, 0, bytesRead);
    }
 
    return mac.doFinal();
  }
 
 
 
  /**
   * Retrieves the name of the preferred cipher algorithm.
   *
   * @return  The name of the preferred cipher algorithm
   */
  public String getPreferredCipherAlgorithm()
  {
    assert debugEnter(CLASS_NAME, "getPreferredCipherAlgorithm");
 
    return preferredCipher;
  }
 
 
 
  /**
   * Retrieves a cipher using the preferred algorithm and the specified cipher
   * mode.
   *
   * @param  cipherMode  The cipher mode that indicates how the cipher will be
   *                     used (e.g., encryption, decryption, wrapping,
   *                     unwrapping).
   *
   * @return  A cipher using the preferred algorithm.
   *
   * @throws  NoSuchAlgorithmException  If the requested algorithm is not
   *                                    supported or is unavailable.
   *
   * @throws  NoSuchPaddingException  If the requested padding mechanism is not
   *                                  supported or is unavailable.
   *
   * @throws  InvalidKeyException  If the provided key is not appropriate for
   *                               use with the requested cipher algorithm.
   *
   * @throws  InvalidAlgorithmParameterException  If an internal problem occurs
   *                                              as a result of the
   *                                              initialization vector used.
   */
  public Cipher getPreferredCipher(int cipherMode)
         throws NoSuchAlgorithmException, NoSuchPaddingException,
                InvalidKeyException, InvalidAlgorithmParameterException
  {
    assert debugEnter(CLASS_NAME, "getPreferredCipher",
                      String.valueOf(cipherMode));
 
    Cipher cipher = Cipher.getInstance(preferredCipher);
 
    // FIXME -- This needs to be more secure.
    IvParameterSpec iv = new IvParameterSpec(new byte[16]);
 
    cipher.init(cipherMode, secretKey, iv);
 
    return cipher;
  }
 
 
 
  /**
   * Retrieves a cipher using the preferred algorithm and the specified cipher
   * mode.
   *
   * @param  cipherAlgorithm  The algorithm to use for the cipher.
   * @param  cipherMode       The cipher mode that indicates how the cipher will
   *                          be used (e.g., encryption, decryption, wrapping,
   *                          unwrapping).
   *
   * @return  A cipher using the preferred algorithm.
   *
   * @throws  NoSuchAlgorithmException  If the requested algorithm is not
   *                                    supported or is unavailable.
   *
   * @throws  NoSuchPaddingException  If the requested padding mechanism is not
   *                                  supported or is unavailable.
   *
   * @throws  InvalidKeyException  If the provided key is not appropriate for
   *                               use with the requested cipher algorithm.
   *
   * @throws  InvalidAlgorithmParameterException  If an internal problem occurs
   *                                              as a result of the
   *                                              initialization vector used.
   */
  public Cipher getCipher(String cipherAlgorithm, int cipherMode)
         throws NoSuchAlgorithmException, NoSuchPaddingException,
                InvalidKeyException, InvalidAlgorithmParameterException
  {
    assert debugEnter(CLASS_NAME, "getCipher",
                      String.valueOf(cipherAlgorithm),
                      String.valueOf(cipherMode));
 
    Cipher cipher = Cipher.getInstance(cipherAlgorithm);
 
    // FIXME -- This needs to be more secure.
    IvParameterSpec iv = new IvParameterSpec(new byte[16]);
 
    cipher.init(cipherMode, secretKey, iv);
 
    return cipher;
  }
 
 
 
  /**
   * Encrypts the data in the provided byte array using the preferred cipher.
   *
   * @param  data  The data to be encrypted.
   *
   * @return  A byte array containing the encrypted representation of the
   *          provided data.
   *
   * @throws  GeneralSecurityException  If a problem occurs while attempting to
   *                                    encrypt the data.
   */
  public byte[] encrypt(byte[] data)
         throws GeneralSecurityException
  {
    assert debugEnter(CLASS_NAME, "encrypt", "byte[" + data.length + "]");
 
    Cipher cipher = Cipher.getInstance(preferredCipher);
 
    // FIXME -- This needs to be more secure.
    IvParameterSpec iv = new IvParameterSpec(new byte[16]);
 
    cipher.init(Cipher.ENCRYPT_MODE, secretKey, iv);
    return cipher.doFinal(data);
  }
 
 
 
  /**
   * Decrypts the data in the provided byte array using the preferred cipher.
   *
   * @param  data  The data to be decrypted.
   *
   * @return  A byte array containing the cleartext representation of the
   *          provided data.
   *
   * @throws  GeneralSecurityException  If a problem occurs while attempting to
   *                                    decrypt the data.
   */
  public byte[] decrypt(byte[] data)
         throws GeneralSecurityException
  {
    assert debugEnter(CLASS_NAME, "decrypt", "byte[" + data.length + "]");
 
    Cipher cipher = Cipher.getInstance(preferredCipher);
 
    // FIXME -- This needs to be more secure.
    IvParameterSpec iv = new IvParameterSpec(new byte[16]);
 
    cipher.init(Cipher.DECRYPT_MODE, secretKey, iv);
    return cipher.doFinal(data);
  }
 
 
 
  /**
   * Encrypts the data in the provided byte array using the preferred cipher.
   *
   * @param  cipherAlgorithm  The algorithm to use to encrypt the data.
   * @param  data             The data to be encrypted.
   *
   * @return  A byte array containing the encrypted representation of the
   *          provided data.
   *
   * @throws  GeneralSecurityException  If a problem occurs while attempting to
   *                                    encrypt the data.
   */
  public byte[] encrypt(String cipherAlgorithm, byte[] data)
         throws GeneralSecurityException
  {
    assert debugEnter(CLASS_NAME, "encrypt", "byte[" + data.length + "]");
 
    Cipher cipher = Cipher.getInstance(cipherAlgorithm);
 
    // FIXME -- This needs to be more secure.
    IvParameterSpec iv = new IvParameterSpec(new byte[16]);
 
    cipher.init(Cipher.ENCRYPT_MODE, secretKey, iv);
    return cipher.doFinal(data);
  }
 
 
 
  /**
   * Decrypts the data in the provided byte array using the requested cipher.
   *
   * @param  cipherAlgorithm  The algorithm to use to decrypt the data.
   * @param  data             The data to be decrypted.
   *
   * @return  A byte array containing the cleartext representation of the
   *          provided data.
   *
   * @throws  GeneralSecurityException  If a problem occurs while attempting to
   *                                    decrypt the data.
   */
  public byte[] decrypt(String cipherAlgorithm, byte[] data)
         throws GeneralSecurityException
  {
    assert debugEnter(CLASS_NAME, "decrypt", "byte[" + data.length + "]");
 
    Cipher cipher = Cipher.getInstance(cipherAlgorithm);
 
    // FIXME -- This needs to be more secure.
    IvParameterSpec iv = new IvParameterSpec(new byte[16]);
 
    cipher.init(Cipher.DECRYPT_MODE, secretKey, iv);
    return cipher.doFinal(data);
  }
 
 
 
  /**
   * Attempts to compress the data in the provided source array into the given
   * destination array.  If the compressed data will fit into the destination
   * array, then this method will return the number of bytes of compressed data
   * in the array.  Otherwise, it will return -1 to indicate that the
   * compression was not successful.  Note that if -1 is returned, then the data
   * in the destination array should be considered invalid.
   *
   * @param  src  The array containing the raw data to compress.
   * @param  dst  The array into which the compressed data should be written.
   *
   * @return  The number of bytes of compressed data, or -1 if it was not
   *          possible to actually compress the data.
   */
  public int compress(byte[] src, byte[] dst)
  {
    Deflater deflater = new Deflater();
    try
    {
      deflater.setInput(src);
      deflater.finish();
 
      int compressedLength = deflater.deflate(dst);
      if (deflater.finished())
      {
        return compressedLength;
      }
      else
      {
        return -1;
      }
    }
    finally
    {
      deflater.end();
    }
  }
 
 
 
  /**
   * Attempts to uncompress the data in the provided source array into the given
   * destination array.  If the uncompressed data will fit into the given
   * destination array, then this method will return the number of bytes of
   * uncompressed data written into the destination buffer.  Otherwise, it will
   * return a negative value to indicate that the destination buffer was not
   * large enough.  The absolute value of that negative return value will
   * indicate the buffer size required to fully decompress the data.  Note that
   * if a negative value is returned, then the data in the destination array
   * should be considered invalid.
   *
   * @param  src  The array containing the compressed data.
   * @param  dst  The array into which the uncompressed data should be written.
   *
   * @return  A positive value containing the number of bytes of uncompressed
   *          data written into the destination buffer, or a negative value
   *          whose absolute value is the size of the destination buffer
   *          required to fully decompress the provided data.
   *
   * @throws  DataFormatException  If a problem occurs while attempting to
   *                               uncompress the data.
   */
  public int uncompress(byte[] src, byte[] dst)
         throws DataFormatException
  {
    Inflater inflater = new Inflater();
    try
    {
      inflater.setInput(src);
 
      int decompressedLength = inflater.inflate(dst);
      if (inflater.finished())
      {
        return decompressedLength;
      }
      else
      {
        int totalLength = decompressedLength;
 
        while (! inflater.finished())
        {
          totalLength += inflater.inflate(dst);
        }
 
        return -totalLength;
      }
    }
    finally
    {
      inflater.end();
    }
  }
}