001    /*
002     * Licensed to the Apache Software Foundation (ASF) under one
003     * or more contributor license agreements.  See the NOTICE file
004     * distributed with this work for additional information
005     * regarding copyright ownership.  The ASF licenses this file
006     * to you under the Apache License, Version 2.0 (the
007     * "License"); you may not use this file except in compliance
008     * with the License.  You may obtain a copy of the License at
009     *
010     *  http://www.apache.org/licenses/LICENSE-2.0
011     *
012     * Unless required by applicable law or agreed to in writing,
013     * software distributed under the License is distributed on an
014     * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015     * KIND, either express or implied.  See the License for the
016     * specific language governing permissions and limitations
017     * under the License.
018     */
019    
020    
021    package org.apache.geronimo.crypto;
022    
023    import java.io.Serializable;
024    import java.io.ByteArrayOutputStream;
025    import java.io.ObjectOutputStream;
026    import java.io.ObjectInputStream;
027    import java.io.ByteArrayInputStream;
028    
029    import javax.crypto.spec.SecretKeySpec;
030    import javax.crypto.Cipher;
031    import javax.crypto.SealedObject;
032    
033    import org.apache.geronimo.crypto.encoders.Base64;
034    import org.apache.commons.logging.Log;
035    import org.apache.commons.logging.LogFactory;
036    
037    /**
038     * @version $Rev: 706640 $ $Date: 2008-10-21 14:44:05 +0000 (Tue, 21 Oct 2008) $
039     */
040    public abstract class AbstractEncryption implements Encryption {
041        private final static Log log = LogFactory.getLog(SimpleEncryption.class);
042    
043        /**
044         * Gets a String which contains the Base64-encoded form of the source,
045         * encrypted with the key from getSecretKeySpec().
046         */
047        public String encrypt(Serializable source) {
048            SecretKeySpec spec = getSecretKeySpec();
049            try {
050                Cipher c = Cipher.getInstance(spec.getAlgorithm());
051                c.init(Cipher.ENCRYPT_MODE, spec);
052                SealedObject so = new SealedObject(source, c);
053                ByteArrayOutputStream store = new ByteArrayOutputStream();
054                ObjectOutputStream out = new ObjectOutputStream(store);
055                out.writeObject(so);
056                out.close();
057                byte[] data = store.toByteArray();
058                byte[] textData = Base64.encode(data);
059                return new String(textData, "US-ASCII");
060            } catch (Exception e) {
061                log.error("Unable to encrypt", e);
062                return null;
063            }
064        }
065    
066        /**
067         * Given a String which is the Base64-encoded encrypted data, retrieve
068         * the original Object.
069         */
070        public Serializable decrypt(String source) {
071            SecretKeySpec spec = getSecretKeySpec();
072            try {
073                byte[] data = Base64.decode(source);
074                Cipher c = Cipher.getInstance(spec.getAlgorithm());
075                c.init(Cipher.DECRYPT_MODE, spec);
076                ObjectInputStream in = new ObjectInputStream(new ByteArrayInputStream(data));
077                SealedObject so = (SealedObject) in.readObject();
078                return (Serializable) so.getObject(c);
079            } catch (Exception e) {
080                log.error("Unable to decrypt", e);
081                return null;
082            }
083        }
084    
085        protected abstract SecretKeySpec getSecretKeySpec();
086    }