View Javadoc
1   package org.apache.fulcrum.jce.crypto;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *   http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
22  import java.io.IOException;
23  import java.io.InputStream;
24  import java.io.OutputStream;
25  import java.security.GeneralSecurityException;
26  
27  import javax.crypto.Cipher;
28  import javax.crypto.CipherInputStream;
29  import javax.crypto.CipherOutputStream;
30  
31  /**
32   * Concrete factory for creating encrypting/decrypting streams. The
33   * implementation uses the JCA (Java Crypto Extension) supplied
34   * by SUN (using SunJCE 1.42).
35   *
36   * The implementation uses as PBEWithMD5AndDES for encryption which
37   * should be sufficent for most applications.
38   *
39   * The implementation also supplies a default password in the case that
40   * the programmer don't want to have additional hassles. It is easy to
41   * reengineer the password being used but much better than a hard-coded
42   * password in the application.
43   *
44   * The code uses parts from Markus Hahn's Blowfish library found at
45   * http://blowfishj.sourceforge.net/
46   *
47   * @author <a href="mailto:siegfried.goeschl@it20one.at">Siegfried Goeschl </a>
48   * @author <a href="mailto:maakus@earthlink.net">Markus Hahn</a>
49   */
50  
51  public abstract class CryptoStreamFactoryTemplate implements CryptoStreamFactory
52  {
53  
54      /** the default instance */
55      protected static CryptoStreamFactory instance;
56      
57      public static CryptoStreamFactory getInstance() 
58      {
59          return instance;
60      }
61  
62      public static void setInstance(CryptoStreamFactory instance) 
63      {
64          CryptoStreamFactoryTemplate.instance = instance;
65      }
66  
67      /**
68       * @see org.apache.fulcrum.jce.crypto.CryptoStreamFactory#getInputStream(java.io.InputStream, String)
69       */
70      public InputStream getInputStream(InputStream is, String decryptionMode) throws GeneralSecurityException, IOException 
71      {
72  
73          InputStream result = null;
74  
75          if( "auto".equalsIgnoreCase(decryptionMode) )
76          {
77              result = getSmartInputStream(is);
78          }
79          else if( "true".equalsIgnoreCase(decryptionMode) )
80          {
81              result = getInputStream(is);
82          }
83          else
84          {
85              result = is;
86          }
87          return result;
88      }
89  
90      /**
91       * @see org.apache.fulcrum.jce.crypto.CryptoStreamFactory#getInputStream(java.io.InputStream, String, char[])
92       */
93      public InputStream getInputStream(InputStream is, String decryptionMode, char[] password) throws GeneralSecurityException, IOException 
94      {
95  
96          InputStream result = null;
97  
98          if( "auto".equalsIgnoreCase(decryptionMode) )
99          {
100             result = getSmartInputStream(is, password);
101         }
102         else if( "true".equalsIgnoreCase(decryptionMode) )
103         {
104             result = getInputStream(is, password);
105         }
106         else
107         {
108             result = is;
109         }
110         return result;
111     }
112 
113     /**
114      * @see org.apache.fulcrum.jce.crypto.CryptoStreamFactory#getInputStream(java.io.InputStream)
115      */
116     public InputStream getInputStream( InputStream is )
117         throws GeneralSecurityException, IOException
118     {
119         Cipher cipher = this.createCipher( Cipher.DECRYPT_MODE, PasswordFactory.getInstance().create() );
120         return new CipherInputStream( is, cipher );
121     }
122 
123     /**
124      * @see org.apache.fulcrum.jce.crypto.CryptoStreamFactory#getInputStream(java.io.InputStream,char[])
125      */
126     public InputStream getInputStream( InputStream is, char[] password )
127         throws GeneralSecurityException, IOException
128     {
129         Cipher cipher = this.createCipher( Cipher.DECRYPT_MODE, password );
130         return new CipherInputStream( is, cipher );
131     }
132 
133     /**
134      * @see org.apache.fulcrum.jce.crypto.CryptoStreamFactory#getSmartInputStream(java.io.InputStream)
135      */
136     public InputStream getSmartInputStream(InputStream is)
137         throws GeneralSecurityException, IOException
138     {
139         return this.getSmartInputStream(
140             is,
141             PasswordFactory.getInstance().create()
142             );
143     }
144 
145     /**
146      * @see org.apache.fulcrum.jce.crypto.CryptoStreamFactory#getSmartInputStream(java.io.InputStream,char[])
147      */
148     public abstract InputStream getSmartInputStream(InputStream is, char[] password )
149         throws GeneralSecurityException, IOException;
150 
151     /**
152      * @see org.apache.fulcrum.jce.crypto.CryptoStreamFactory#getOutputStream(java.io.OutputStream)
153      */
154     public OutputStream getOutputStream( OutputStream os )
155         throws GeneralSecurityException, IOException
156     {
157         Cipher cipher = this.createCipher( Cipher.ENCRYPT_MODE, PasswordFactory.getInstance().create() );
158         return new CipherOutputStream( os, cipher );    }
159 
160     /**
161      * @see org.apache.fulcrum.jce.crypto.CryptoStreamFactory#getOutputStream(java.io.OutputStream, char[])
162      */
163     public OutputStream getOutputStream( OutputStream os, char[] password )
164         throws GeneralSecurityException, IOException
165     {
166         Cipher cipher = this.createCipher( Cipher.ENCRYPT_MODE, password );
167         return new CipherOutputStream( os, cipher );
168     }
169     protected abstract Cipher createCipher(int encryptMode, char[] password) throws GeneralSecurityException, IOException;
170 
171 }