[Webfunds-commits] java/webfunds/sox Crypto.java

Jeroen C. van Gelderen gelderen@cypherpunks.ai
Mon, 31 Jul 2000 13:22:33 -0400 (AST)


gelderen    00/07/31 13:22:33

  Modified:    webfunds/sox Crypto.java
  Log:
  Checkpoint: Use JCE for symmetric ENcryption.

Revision  Changes    Path
1.42      +38 -34    java/webfunds/sox/Crypto.java

Index: Crypto.java
===================================================================
RCS file: /home/webfunds/cvsroot/java/webfunds/sox/Crypto.java,v
retrieving revision 1.41
retrieving revision 1.42
diff -u -r1.41 -r1.42
--- Crypto.java	2000/07/30 03:59:58	1.41
+++ Crypto.java	2000/07/31 17:22:32	1.42
@@ -1,4 +1,4 @@
-/* $Id: Crypto.java,v 1.41 2000/07/30 03:59:58 gelderen Exp $
+/* $Id: Crypto.java,v 1.42 2000/07/31 17:22:32 gelderen Exp $
  *
  * Copyright (c) Systemics Inc. 1995-2000 on behalf of
  * The WebFunds Development Team.  All Rights Reserved.
@@ -9,6 +9,7 @@
 import java.io.*;
 import java.math.BigInteger;
 import java.security.GeneralSecurityException;
+import java.security.InvalidAlgorithmParameterException;
 import java.security.InvalidKeyException;
 import java.security.Key;
 import java.security.KeyException;
@@ -17,6 +18,7 @@
 import java.security.MessageDigest;
 import java.security.Mode;
 import java.security.NoSuchAlgorithmException;
+import java.security.NoSuchProviderException;
 import java.security.PaddingScheme;
 import java.security.PrivateKey;
 import java.security.ProviderException;
@@ -28,7 +30,12 @@
 import java.util.zip.GZIPInputStream;
 import java.util.zip.GZIPOutputStream;
 
+import javax.crypto.BadPaddingException;
+import javax.crypto.Cipher;
+import javax.crypto.IllegalBlockSizeException;
 import javax.crypto.KeyGenerator;
+import javax.crypto.NoSuchPaddingException;
+import javax.crypto.spec.IvParameterSpec;
 
 import cryptix.provider.key.RawSecretKey;
 import cryptix.provider.rsa.RawRSAPrivateKey;
@@ -50,7 +57,7 @@
  *
  * Centralized crypto methods. Currently being overhauled.
  *
- * @version $Revision: 1.41 $
+ * @version $Revision: 1.42 $
  */
 public final class Crypto
 {
@@ -272,39 +279,36 @@
      */
     public static byte[] encrypt(Key key, byte[] data, int offset, int len)
     {
-      try {
-        java.security.Cipher cipher = 
-            java.security.Cipher.getInstance(
-                java.security.Cipher.getInstance(cipher_alg),
-                (Mode)Mode.getInstance(cipher_mode),
-                PaddingScheme.getInstance(cipher_padding) );
-                                    
+        logDebug("encrypt() called");
+        try {
+            Cipher cipher = Cipher.getInstance("DESede/CBC/PKCS#5",
+                                               "CryptixCrypto");
                                     
-        ByteArrayOutputStream baos = new ByteArrayOutputStream();
-        GZIPOutputStream gzos = new GZIPOutputStream(baos);
-        gzos.write(data, offset, len);
-        gzos.finish();
-        byte[] zipped = baos.toByteArray();
-
-        cipher.initEncrypt(key);
-        byte[] tmp = new byte[ cipher.outBufferSizeFinal(zipped.length) ];
-        cipher.doFinal(zipped, 0, zipped.length, tmp, 0);
-
-
-        return tmp;
-      }
-      catch( IOException e ) { //FIXME: IOException
-        throw new InternalError("Unexpected IOException ("+e.getMessage()+")");
-      }
-      catch(KeyException e) {
-        // KeyException is turned into an IllegalArgumentException
-        // since the caller should be aware of the value.
-        throw new IllegalArgumentException("Invalid key ("+e.getMessage()+")");
-      }
-      catch (NoSuchAlgorithmException e) {
-        throw new ProviderException(
-            "Symmetric Algorithm not found ("+e.getMessage()+")");
-      }
+            ByteArrayOutputStream baos = new ByteArrayOutputStream();
+            GZIPOutputStream gzos = new GZIPOutputStream(baos);
+            gzos.write(data, offset, len);
+            gzos.finish();
+            byte[] zipped = baos.toByteArray();
+
+            // XXX: IV of all zeroes is a minor security problem
+            IvParameterSpec iv = new IvParameterSpec( new byte[8] );
+            cipher.init(Cipher.ENCRYPT_MODE, key, iv);
+            return cipher.doFinal(zipped);
+        }
+        catch( IOException e ) { //FIXME: IOException
+            throw new InternalError(
+                "Unexpected IOException ("+e.getMessage()+")");
+        }
+        catch(InvalidKeyException e) {
+            throw new IllegalArgumentException("key: " + key);
+        }
+        catch(Exception e) {
+            // XXX: should catch GeneralSecurityException but that doesn't
+            //      work on JDK 1.1.
+
+            e.printStackTrace();
+            throw new ProviderException("Exception during decrypt.");
+        }
     }