You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@turbine.apache.org by sg...@apache.org on 2008/02/01 22:53:25 UTC

svn commit: r617667 - in /turbine/fulcrum/trunk/yaafi/src: java/org/apache/fulcrum/jce/crypto/ test/org/apache/fulcrum/jce/crypto/

Author: sgoeschl
Date: Fri Feb  1 13:53:16 2008
New Revision: 617667

URL: http://svn.apache.org/viewvc?rev=617667&view=rev
Log:
Changes the implementation so it is impossible to use strong encryption/decryption - we are now limited to DES (56 bit)

Modified:
    turbine/fulcrum/trunk/yaafi/src/java/org/apache/fulcrum/jce/crypto/CryptoParameters.java
    turbine/fulcrum/trunk/yaafi/src/java/org/apache/fulcrum/jce/crypto/CryptoStreamFactoryImpl.java
    turbine/fulcrum/trunk/yaafi/src/java/org/apache/fulcrum/jce/crypto/CryptoUtil.java
    turbine/fulcrum/trunk/yaafi/src/java/org/apache/fulcrum/jce/crypto/PasswordFactory.java
    turbine/fulcrum/trunk/yaafi/src/test/org/apache/fulcrum/jce/crypto/CryptoUtilTest.java
    turbine/fulcrum/trunk/yaafi/src/test/org/apache/fulcrum/jce/crypto/SmartDecryptingInputStreamTest.java

Modified: turbine/fulcrum/trunk/yaafi/src/java/org/apache/fulcrum/jce/crypto/CryptoParameters.java
URL: http://svn.apache.org/viewvc/turbine/fulcrum/trunk/yaafi/src/java/org/apache/fulcrum/jce/crypto/CryptoParameters.java?rev=617667&r1=617666&r2=617667&view=diff
==============================================================================
--- turbine/fulcrum/trunk/yaafi/src/java/org/apache/fulcrum/jce/crypto/CryptoParameters.java (original)
+++ turbine/fulcrum/trunk/yaafi/src/java/org/apache/fulcrum/jce/crypto/CryptoParameters.java Fri Feb  1 13:53:16 2008
@@ -31,28 +31,6 @@
     /** Parameter for PBEParameterSpec */
     int COUNT = 20;
 
-    /**
-     * The algorithm being used
-     *
-     * <ul>
-     *   <li>for SunJCE 1.22 (JDK 1.3) :  PBEWithMD5AndDES</li>
-     *   <li>for SunJCE 1.42 (JDK 1.4) :  PBEWithMD5AndDES, PBEWithMD5AndTripleDES</li>
-     * </ul>
-     */
-    String ALGORITHM = "PBEWithMD5AndDES";
-
-    /**
-     * The JCE provider name known to work. If the value
-     * is set to null an appropriate provider will be
-     * used.
-     *
-     * <ul>
-     *  <li>SunJCE<li>
-     *  <li>BC (Bouncy Castle Provider)<li>
-     * </ul>
-     */
-    String PROVIDERNAME = null;
-
     /** The password salt */
     byte[] SALT = {
         (byte)0xc7, (byte)0x73, (byte)0x21, (byte)0x8c,

Modified: turbine/fulcrum/trunk/yaafi/src/java/org/apache/fulcrum/jce/crypto/CryptoStreamFactoryImpl.java
URL: http://svn.apache.org/viewvc/turbine/fulcrum/trunk/yaafi/src/java/org/apache/fulcrum/jce/crypto/CryptoStreamFactoryImpl.java?rev=617667&r1=617666&r2=617667&view=diff
==============================================================================
--- turbine/fulcrum/trunk/yaafi/src/java/org/apache/fulcrum/jce/crypto/CryptoStreamFactoryImpl.java (original)
+++ turbine/fulcrum/trunk/yaafi/src/java/org/apache/fulcrum/jce/crypto/CryptoStreamFactoryImpl.java Fri Feb  1 13:53:16 2008
@@ -70,24 +70,28 @@
     /** the default instance */
     private static CryptoStreamFactory instance;
 
+    /** The crypto algorithm being used */
+    private static final String ALGORITHM = "PBEWithMD5AndDES";
+
+    /**
+     * The JCE provider name known to work. If the value
+     * is set to null an appropriate provider will be
+     * used.
+     */
+    private static final String PROVIDERNAME = null;
+
     /**
      * Factory method to get a default instance
      * @return an instance of the CryptoStreamFactory
      */
-    public static CryptoStreamFactory getInstance()
+    public synchronized static CryptoStreamFactory getInstance()
     {
         if( CryptoStreamFactoryImpl.instance == null )
         {
-            synchronized( CryptoStreamFactory.class )
-            {
-                if( CryptoStreamFactoryImpl.instance == null )
-                {
-                    CryptoStreamFactoryImpl.instance = new CryptoStreamFactoryImpl();
-                }
-            }
+            CryptoStreamFactoryImpl.instance = new CryptoStreamFactoryImpl();
         }
 
-        return instance;
+        return CryptoStreamFactoryImpl.instance;
     }
 
     /**
@@ -106,8 +110,8 @@
     {
         this.salt = CryptoParameters.SALT;
         this.count = CryptoParameters.COUNT;
-        this.providerName = CryptoParameters.PROVIDERNAME;
-        this.algorithm = CryptoParameters.ALGORITHM;
+        this.providerName = PROVIDERNAME;
+        this.algorithm = ALGORITHM;
     }
 
     /**
@@ -115,19 +119,13 @@
      *
      * @param salt the salt for the PBE algorithm
      * @param count the iteration for PBEParameterSpec
-     * @param algorithm the algorithm to be used
-     * @param providerName the name of the JCE provide to b used
      */
-    public CryptoStreamFactoryImpl(
-        byte[] salt,
-        int count,
-        String algorithm,
-        String providerName )
+    public CryptoStreamFactoryImpl( byte[] salt, int count)
     {
         this.salt = salt;
         this.count = count;
-        this.algorithm = algorithm;
-        this.providerName = providerName;
+        this.providerName = PROVIDERNAME;
+        this.algorithm = ALGORITHM;
     }
 
     /**
@@ -137,8 +135,7 @@
         throws GeneralSecurityException, IOException
     {
         Cipher cipher = this.createCipher( Cipher.DECRYPT_MODE, PasswordFactory.create() );
-        CipherInputStream cis = new CipherInputStream( is, cipher );
-        return cis;
+        return new CipherInputStream( is, cipher );
     }
 
     /**
@@ -148,8 +145,7 @@
         throws GeneralSecurityException, IOException
     {
         Cipher cipher = this.createCipher( Cipher.DECRYPT_MODE, password );
-        CipherInputStream cis = new CipherInputStream( is, cipher );
-        return cis;
+        return new CipherInputStream( is, cipher );
     }
 
     /**
@@ -170,7 +166,7 @@
     public InputStream getSmartInputStream(InputStream is, char[] password )
         throws GeneralSecurityException, IOException
     {
-        SmartDecryptingInputStream result = null;
+        SmartDecryptingInputStream result;
 
         result = new SmartDecryptingInputStream(
             getInstance(),
@@ -188,14 +184,13 @@
         throws GeneralSecurityException, IOException
     {
         Cipher cipher = this.createCipher( Cipher.ENCRYPT_MODE, password );
-        CipherOutputStream cos = new CipherOutputStream( os, cipher );
-        return cos;
+        return new CipherOutputStream( os, cipher );
     }
 
     /**
      * @return Returns the algorithm.
      */
-    private final String getAlgorithm()
+    private String getAlgorithm()
     {
         return algorithm;
     }
@@ -203,7 +198,7 @@
     /**
      * @return Returns the count.
      */
-    private final int getCount()
+    private int getCount()
     {
         return count;
     }
@@ -211,7 +206,7 @@
     /**
      * @return Returns the providerName.
      */
-    private final String getProviderName()
+    private String getProviderName()
     {
         return providerName;
     }
@@ -219,7 +214,7 @@
     /**
      * @return Returns the salt.
      */
-    private final byte [] getSalt()
+    private byte [] getSalt()
     {
         return salt;
     }
@@ -231,10 +226,10 @@
      * @return the key
      * @throws GeneralSecurityException creating the key failed
      */
-    private final Key createKey( char[] password )
+    private Key createKey( char[] password )
         throws GeneralSecurityException
     {
-        SecretKeyFactory keyFactory = null;
+        SecretKeyFactory keyFactory;
         String algorithm = this.getAlgorithm();
         PBEKeySpec keySpec =  new PBEKeySpec(password);
 
@@ -247,8 +242,7 @@
             keyFactory = SecretKeyFactory.getInstance( algorithm, this.getProviderName() );
         }
 
-        Key key = keyFactory.generateSecret(keySpec);
-        return key;
+        return keyFactory.generateSecret(keySpec);
     }
 
     /**
@@ -260,10 +254,10 @@
      * @throws GeneralSecurityException creating a cipher failed
      * @throws IOException creating a cipher failed
      */
-    private final Cipher createCipher( int mode, char[] password )
+    private Cipher createCipher( int mode, char[] password )
         throws GeneralSecurityException, IOException
     {
-        Cipher cipher = null;
+        Cipher cipher;
         PBEParameterSpec paramSpec = new PBEParameterSpec( this.getSalt(), this.getCount() );
         Key key = this.createKey( password );
 

Modified: turbine/fulcrum/trunk/yaafi/src/java/org/apache/fulcrum/jce/crypto/CryptoUtil.java
URL: http://svn.apache.org/viewvc/turbine/fulcrum/trunk/yaafi/src/java/org/apache/fulcrum/jce/crypto/CryptoUtil.java?rev=617667&r1=617666&r2=617667&view=diff
==============================================================================
--- turbine/fulcrum/trunk/yaafi/src/java/org/apache/fulcrum/jce/crypto/CryptoUtil.java (original)
+++ turbine/fulcrum/trunk/yaafi/src/java/org/apache/fulcrum/jce/crypto/CryptoUtil.java Fri Feb  1 13:53:16 2008
@@ -229,7 +229,7 @@
     private static InputStream createInputStream( Object source )
         throws IOException
     {
-        InputStream is = null;
+        InputStream is;
 
         // create an InputStream
 
@@ -279,7 +279,7 @@
     private static OutputStream createOutputStream( Object target )
         throws IOException
     {
-        OutputStream os = null;
+        OutputStream os;
 
         if( target instanceof File )
         {

Modified: turbine/fulcrum/trunk/yaafi/src/java/org/apache/fulcrum/jce/crypto/PasswordFactory.java
URL: http://svn.apache.org/viewvc/turbine/fulcrum/trunk/yaafi/src/java/org/apache/fulcrum/jce/crypto/PasswordFactory.java?rev=617667&r1=617666&r2=617667&view=diff
==============================================================================
--- turbine/fulcrum/trunk/yaafi/src/java/org/apache/fulcrum/jce/crypto/PasswordFactory.java (original)
+++ turbine/fulcrum/trunk/yaafi/src/java/org/apache/fulcrum/jce/crypto/PasswordFactory.java Fri Feb  1 13:53:16 2008
@@ -38,10 +38,11 @@
 
 public class PasswordFactory implements PasswordParameters
 {
+
     /**
      * @return a default password using "xxxx-xxxx-xxxx-xxxxx"
      */
-    public static final char[] create()
+    public static char[] create()
         throws NoSuchAlgorithmException, UnsupportedEncodingException
     {
         return create(
@@ -55,7 +56,7 @@
      * @param seed the default password supplied by the caller
      * @return a password using "xxxx-xxxx-xxxx-xxxxx"
      */
-    public static final char[] create( String seed )
+    public static char[] create( String seed )
         throws NoSuchAlgorithmException, UnsupportedEncodingException
     {
         return create(
@@ -72,8 +73,8 @@
     {
         return create(
             seed,
-            PasswordParameters.SALT,
-            PasswordParameters.COUNT
+            PasswordFactory.SALT,
+            PasswordFactory.COUNT
             );
     }
 
@@ -87,7 +88,7 @@
      * @throws NoSuchAlgorithmException the encryption algorithm is not supported
      * @throws UnsupportedEncodingException the requested encoding is not supported
      */
-    public static final char [] create( char[] password, byte[] salt, int count )
+    public static char [] create( char[] password, byte[] salt, int count )
         throws NoSuchAlgorithmException, UnsupportedEncodingException
     {
         char [] result = null;
@@ -155,7 +156,7 @@
      * @param nOfs index from where to read the data
      * @return the 64bit integer
      */
-    private static final long createLong(byte [] buf, int nOfs)
+    private static long createLong(byte [] buf, int nOfs)
     {
         return
             ((long)(( buf[nOfs    ]          << 24) |

Modified: turbine/fulcrum/trunk/yaafi/src/test/org/apache/fulcrum/jce/crypto/CryptoUtilTest.java
URL: http://svn.apache.org/viewvc/turbine/fulcrum/trunk/yaafi/src/test/org/apache/fulcrum/jce/crypto/CryptoUtilTest.java?rev=617667&r1=617666&r2=617667&view=diff
==============================================================================
--- turbine/fulcrum/trunk/yaafi/src/test/org/apache/fulcrum/jce/crypto/CryptoUtilTest.java (original)
+++ turbine/fulcrum/trunk/yaafi/src/test/org/apache/fulcrum/jce/crypto/CryptoUtilTest.java Fri Feb  1 13:53:16 2008
@@ -52,7 +52,8 @@
 
         this.password = "mysecret";
         this.testDataDirectory = new File( "./src/test/data" );
-        this.tempDataDirectory = new File( "./temp" );
+        this.tempDataDirectory = new File( "./target/temp" );
+        this.tempDataDirectory.mkdirs();
     }
 
     /**
@@ -67,9 +68,7 @@
     {
         CryptoStreamFactoryImpl factory = new CryptoStreamFactoryImpl(
             CryptoParameters.SALT,
-            CryptoParameters.COUNT,
-            "PBEWithMD5AndDES",
-            CryptoParameters.PROVIDERNAME
+            CryptoParameters.COUNT
             );
 
         CryptoStreamFactoryImpl.setInstance( factory );

Modified: turbine/fulcrum/trunk/yaafi/src/test/org/apache/fulcrum/jce/crypto/SmartDecryptingInputStreamTest.java
URL: http://svn.apache.org/viewvc/turbine/fulcrum/trunk/yaafi/src/test/org/apache/fulcrum/jce/crypto/SmartDecryptingInputStreamTest.java?rev=617667&r1=617666&r2=617667&view=diff
==============================================================================
--- turbine/fulcrum/trunk/yaafi/src/test/org/apache/fulcrum/jce/crypto/SmartDecryptingInputStreamTest.java (original)
+++ turbine/fulcrum/trunk/yaafi/src/test/org/apache/fulcrum/jce/crypto/SmartDecryptingInputStreamTest.java Fri Feb  1 13:53:16 2008
@@ -64,9 +64,7 @@
     {
         CryptoStreamFactoryImpl factory = new CryptoStreamFactoryImpl(
             CryptoParameters.SALT,
-            CryptoParameters.COUNT,
-            "PBEWithMD5AndDES",
-            CryptoParameters.PROVIDERNAME
+            CryptoParameters.COUNT
             );
 
         CryptoStreamFactoryImpl.setInstance( factory );