001    package org.apache.fulcrum.jce.crypto;
002    
003    /*
004     * Licensed to the Apache Software Foundation (ASF) under one
005     * or more contributor license agreements.  See the NOTICE file
006     * distributed with this work for additional information
007     * regarding copyright ownership.  The ASF licenses this file
008     * to you under the Apache License, Version 2.0 (the
009     * "License"); you may not use this file except in compliance
010     * with the License.  You may obtain a copy of the License at
011     *
012     *   http://www.apache.org/licenses/LICENSE-2.0
013     *
014     * Unless required by applicable law or agreed to in writing,
015     * software distributed under the License is distributed on an
016     * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
017     * KIND, either express or implied.  See the License for the
018     * specific language governing permissions and limitations
019     * under the License.
020     */
021    
022    
023    import java.io.ByteArrayOutputStream;
024    import java.io.IOException;
025    import java.io.InputStream;
026    import java.io.OutputStream;
027    import java.security.GeneralSecurityException;
028    
029    /**
030     * Helper class to provde generic functions to work with CryptoStreams.
031     *
032     * The code uses parts from Markus Hahn's Blowfish library found at
033     * http://blowfishj.sourceforge.net/
034     *
035     * @author <a href="mailto:siegfried.goeschl@it20one.at">Siegfried Goeschl </a>
036     * @author <a href="mailto:maakus@earthlink.net">Markus Hahn</a>
037     */
038    
039    public final class CryptoUtil
040    {
041        /**
042         * Copies from a source to a target object using encryption
043         *
044         * @param source the source object
045         * @param target the target object
046         * @param password the password to use for encryption
047         * @throws GeneralSecurityException accessing JCE failed
048         * @throws IOException accessing the souce failed
049         *
050         */
051        public static void encrypt( Object source, Object target, char[] password )
052            throws GeneralSecurityException, IOException
053        {
054            CryptoUtil.encrypt(
055                CryptoUtil.getCryptoStreamFactory(),
056                source,
057                target,
058                password
059                );
060        }
061    
062        /**
063         * Copies from a source to a target object using encryption and a
064         * caller supplied CryptoStreamFactory.
065         *
066         * @param factory the factory to create the crypto streams
067         * @param source the source object
068         * @param target the target object
069         * @param password the password to use for encryption
070         * @throws GeneralSecurityException accessing JCE failed
071         * @throws IOException accessing the souce failed
072         */
073        public static void encrypt(
074            CryptoStreamFactory factory, Object source, Object target, char[] password )
075            throws GeneralSecurityException, IOException
076        {
077            InputStream is = StreamUtil.createInputStream( source );
078            OutputStream os = StreamUtil.createOutputStream( target );
079            OutputStream eos = factory.getOutputStream( os, password );
080            StreamUtil.copy( is, eos );
081        }
082    
083        /**
084         * Copies from a source to a target object using decryption.
085         *
086         * @param source the source object
087         * @param target the target object
088         * @param password the password to use for decryption
089         * @throws GeneralSecurityException accessing JCE failed
090         * @throws IOException accessing the souce failed
091         */
092        public static void decrypt( Object source, Object target, char[] password )
093            throws GeneralSecurityException, IOException
094        {
095            CryptoUtil.decrypt(
096                CryptoUtil.getCryptoStreamFactory(),
097                source,
098                target,
099                password
100                );
101        }
102    
103        /**
104         * Copies from a source to a target object using decryption and a
105         * caller-suppier CryptoStreamFactory.
106         *
107         * @param factory the factory to create the crypto streams
108         * @param source the source object
109         * @param target the target object
110         * @param password the password to use for decryption
111         * @throws GeneralSecurityException accessing JCE failed
112         * @throws IOException accessing the souce failed
113         */
114        public static void decrypt(
115            CryptoStreamFactory factory, Object source, Object target, char[] password )
116            throws GeneralSecurityException, IOException
117        {
118            InputStream is = StreamUtil.createInputStream( source );
119            OutputStream os = StreamUtil.createOutputStream( target );
120            InputStream dis = factory.getInputStream( is, password );
121            StreamUtil.copy( dis, os );
122        }
123    
124        /**
125         * Encrypts a string into a hex string.
126         *
127         * @param plainText the plain text to be encrypted
128         * @param password the password for encryption
129         * @return the encrypted string
130         * @throws GeneralSecurityException accessing JCE failed
131         * @throws IOException accessing the souce failed
132         */
133        public static String encryptString( String plainText, char[] password )
134            throws GeneralSecurityException, IOException
135        {
136            return CryptoUtil.encryptString(
137                CryptoUtil.getCryptoStreamFactory(),
138                plainText,
139                password
140                );
141        }
142    
143        /**
144         * Encrypts a string into a hex string.
145         *
146         * @param factory the factory to create the crypto streams
147         * @param plainText the plain text to be encrypted
148         * @param password the password for encryption
149         * @return the encrypted string
150         * @throws GeneralSecurityException accessing JCE failed
151         * @throws IOException accessing the souce failed
152         */
153        public static String encryptString(
154            CryptoStreamFactory factory, String plainText, char[] password )
155            throws GeneralSecurityException, IOException
156        {
157            ByteArrayOutputStream bais = new ByteArrayOutputStream();
158            CryptoUtil.encrypt( factory, plainText, bais, password );
159            return HexConverter.toString( bais.toByteArray() );
160        }
161    
162        /**
163         * Decrypts an encrypted string into the plain text. The encrypted
164         * string must be a hex string created by encryptString.
165         *
166         * @param cipherText the encrypted text to be decrypted
167         * @param password the password for decryption
168         * @return the decrypted string
169         * @throws GeneralSecurityException accessing JCE failed
170         * @throws IOException accessing the souce failed
171         */
172        public static String decryptString( String cipherText, char[] password )
173            throws GeneralSecurityException, IOException
174        {
175            return CryptoUtil.decryptString(
176                CryptoUtil.getCryptoStreamFactory(),
177                cipherText,
178                password
179                );
180        }
181    
182        /**
183         * Decrypts an encrypted string into the plain text. The encrypted
184         * string must be a hex string created by encryptString.
185         *
186         * @param factory the factory to create the crypto streams
187         * @param cipherText the encrypted text to be decrypted
188         * @param password the password for decryption
189         * @return the decrypted string
190         * @throws GeneralSecurityException accessing JCE failed
191         * @throws IOException accessing the souce failed
192         */
193        public static String decryptString(
194            CryptoStreamFactory factory, String cipherText, char[] password )
195            throws GeneralSecurityException, IOException
196        {
197            byte[] buffer = HexConverter.toBytes( cipherText );
198            ByteArrayOutputStream bais = new ByteArrayOutputStream();
199            CryptoUtil.decrypt( factory, buffer, bais, password );
200            return new String( bais.toByteArray(), "utf-8" );
201        }
202    
203        /**
204         * Pumps the input stream to the output stream.
205         *
206         * @param is the source input stream
207         * @param os the target output stream
208         * @return the number of bytes copied
209         * @throws IOException the copying failed
210         * @deprecated use StreamUtil instead
211         */
212        public static long copy( InputStream is, OutputStream os )
213            throws IOException
214        {
215            return StreamUtil.copy(is, os);
216        }
217    
218        /**
219         * @return the CryptoStreamFactory to be used
220         */
221        public static CryptoStreamFactory getCryptoStreamFactory()
222        {
223            return CryptoStreamFactoryImpl.getInstance();
224        }
225    }