01: /*
02: * Licensed to the Apache Software Foundation (ASF) under one
03: * or more contributor license agreements. See the NOTICE file
04: * distributed with this work for additional information
05: * regarding copyright ownership. The ASF licenses this file
06: * to you under the Apache License, Version 2.0 (the
07: * "License"); you may not use this file except in compliance
08: * with the License. You may obtain a copy of the License at
09: *
10: * http://www.apache.org/licenses/LICENSE-2.0
11: *
12: * Unless required by applicable law or agreed to in writing,
13: * software distributed under the License is distributed on an
14: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15: * KIND, either express or implied. See the License for the
16: * specific language governing permissions and limitations
17: * under the License.
18: */
19:
20: package org.apache.geronimo.crypto;
21:
22: import java.io.Serializable;
23: import java.io.ByteArrayOutputStream;
24: import java.io.ObjectOutputStream;
25: import java.io.ObjectInputStream;
26: import java.io.ByteArrayInputStream;
27:
28: import javax.crypto.spec.SecretKeySpec;
29: import javax.crypto.Cipher;
30: import javax.crypto.SealedObject;
31:
32: import org.apache.geronimo.crypto.encoders.Base64;
33: import org.apache.commons.logging.Log;
34: import org.apache.commons.logging.LogFactory;
35:
36: /**
37: * @version $Rev: 617735 $ $Date: 2008-02-01 17:20:02 -0800 (Fri, 01 Feb 2008) $
38: */
39: public abstract class AbstractEncryption implements Encryption {
40: private final static Log log = LogFactory
41: .getLog(SimpleEncryption.class);
42:
43: /**
44: * Gets a String which contains the Base64-encoded form of the source,
45: * encrypted with the key from getSecretKeySpec().
46: */
47: public String encrypt(Serializable source) {
48: SecretKeySpec spec = getSecretKeySpec();
49: try {
50: Cipher c = Cipher.getInstance(spec.getAlgorithm());
51: c.init(Cipher.ENCRYPT_MODE, spec);
52: SealedObject so = new SealedObject(source, c);
53: ByteArrayOutputStream store = new ByteArrayOutputStream();
54: ObjectOutputStream out = new ObjectOutputStream(store);
55: out.writeObject(so);
56: out.close();
57: byte[] data = store.toByteArray();
58: byte[] textData = Base64.encode(data);
59: return new String(textData, "US-ASCII");
60: } catch (Exception e) {
61: log.error("Unable to encrypt", e);
62: return null;
63: }
64: }
65:
66: /**
67: * Given a String which is the Base64-encoded encrypted data, retrieve
68: * the original Object.
69: */
70: public Serializable decrypt(String source) {
71: SecretKeySpec spec = getSecretKeySpec();
72: try {
73: byte[] data = Base64.decode(source);
74: Cipher c = Cipher.getInstance(spec.getAlgorithm());
75: c.init(Cipher.DECRYPT_MODE, spec);
76: ObjectInputStream in = new ObjectInputStream(
77: new ByteArrayInputStream(data));
78: SealedObject so = (SealedObject) in.readObject();
79: return (Serializable) so.getObject(c);
80: } catch (Exception e) {
81: log.error("Unable to decrypt", e);
82: return null;
83: }
84: }
85:
86: protected abstract SecretKeySpec getSecretKeySpec();
87: }
|