1 /** 2 * 3 * Licensed to the Apache Software Foundation (ASF) under one or more 4 * contributor license agreements. See the NOTICE file distributed with 5 * this work for additional information regarding copyright ownership. 6 * The ASF licenses this file to You under the Apache License, Version 2.0 7 * (the "License"); you may not use this file except in compliance with 8 * the License. You may obtain a copy of the License at 9 * 10 * http://www.apache.org/licenses/LICENSE-2.0 11 * 12 * Unless required by applicable law or agreed to in writing, software 13 * distributed under the License is distributed on an "AS IS" BASIS, 14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 * See the License for the specific language governing permissions and 16 * limitations under the License. 17 */ 18 package org.apache.geronimo.util; 19 20 import java.io.ByteArrayOutputStream; 21 import java.io.ObjectOutputStream; 22 import java.io.Serializable; 23 import java.io.ObjectInputStream; 24 import java.io.ByteArrayInputStream; 25 import javax.crypto.spec.SecretKeySpec; 26 import javax.crypto.Cipher; 27 import javax.crypto.SealedObject; 28 import org.apache.geronimo.util.encoders.Base64; 29 import org.apache.commons.logging.Log; 30 import org.apache.commons.logging.LogFactory; 31 32 /** 33 * This class protects some value BY ENCRYPTING WITH A KNOWN KEY. That is 34 * to say, it's only safe against anyone who can't read the source code. 35 * So the main idea is to protect against casual observers. 36 * 37 * If someone has a better idea for how to implement encryption with a 38 * non-obvious key that the user isn't likely to change during the normal 39 * course of working with the server, I'd be happy to hear it. (But I 40 * assume the SSL keystore is likely to be changed, which would result 41 * in losing all the "encrypted" data. 42 * 43 * @version $Rev: 470572 $ $Date: 2006-11-02 14:36:56 -0800 (Thu, 02 Nov 2006) $ 44 */ 45 public class SimpleEncryption { 46 private final static Log log = LogFactory.getLog(SimpleEncryption.class); 47 private final static SecretKeySpec SECRET_KEY = new SecretKeySpec(new byte[]{(byte)-45,(byte)-15,(byte)100,(byte)-34,(byte)70,(byte)83,(byte)75,(byte)-100,(byte)-75,(byte)61,(byte)26,(byte)114,(byte)-20,(byte)-58,(byte)114,(byte)77}, "AES"); 48 49 50 /** 51 * Gets a String which contains the Base64-encoded form of the source, 52 * encrypted with the known key. 53 */ 54 public static String encrypt(Serializable source) { 55 try { 56 Cipher c = Cipher.getInstance("AES"); 57 c.init(Cipher.ENCRYPT_MODE, SECRET_KEY); 58 SealedObject so = new SealedObject(source, c); 59 ByteArrayOutputStream store = new ByteArrayOutputStream(); 60 ObjectOutputStream out = new ObjectOutputStream(store); 61 out.writeObject(so); 62 out.close(); 63 byte[] data = store.toByteArray(); 64 byte[] textData = Base64.encode(data); 65 return new String(textData, "US-ASCII"); 66 } catch (Exception e) { 67 log.error("Unable to encrypt", e); 68 return null; 69 } 70 } 71 72 /** 73 * Given a String which is the Base64-encoded encrypted data, retrieve 74 * the original Object. 75 */ 76 public static Object decrypt(String source) { 77 try { 78 byte[] data = Base64.decode(source); 79 Cipher c = Cipher.getInstance("AES"); 80 c.init(Cipher.DECRYPT_MODE, SECRET_KEY); 81 ObjectInputStream in = new ObjectInputStream(new ByteArrayInputStream(data)); 82 SealedObject so = (SealedObject) in.readObject(); 83 return so.getObject(c); 84 } catch (Exception e) { 85 log.error("Unable to decrypt", e); 86 return null; 87 } 88 } 89 }