1 17 package org.apache.geronimo.util; 18 19 import java.io.ByteArrayOutputStream ; 20 import java.io.ObjectOutputStream ; 21 import java.io.Serializable ; 22 import java.io.ObjectInputStream ; 23 import java.io.ByteArrayInputStream ; 24 import javax.crypto.spec.SecretKeySpec; 25 import javax.crypto.Cipher; 26 import javax.crypto.SealedObject; 27 import org.apache.geronimo.util.encoders.Base64; 28 import org.apache.commons.logging.Log; 29 import org.apache.commons.logging.LogFactory; 30 31 44 public class SimpleEncryption { 45 private final static Log log = LogFactory.getLog(SimpleEncryption.class); 46 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"); 47 48 49 53 public static String encrypt(Serializable source) { 54 try { 55 Cipher c = Cipher.getInstance("AES"); 56 c.init(Cipher.ENCRYPT_MODE, SECRET_KEY); 57 SealedObject so = new SealedObject(source, c); 58 ByteArrayOutputStream store = new ByteArrayOutputStream (); 59 ObjectOutputStream out = new ObjectOutputStream (store); 60 out.writeObject(so); 61 out.close(); 62 byte[] data = store.toByteArray(); 63 byte[] textData = Base64.encode(data); 64 return new String (textData, "US-ASCII"); 65 } catch (Exception e) { 66 log.error("Unable to encrypt", e); 67 return null; 68 } 69 } 70 71 75 public static Object decrypt(String source) { 76 try { 77 byte[] data = Base64.decode(source); 78 Cipher c = Cipher.getInstance("AES"); 79 c.init(Cipher.DECRYPT_MODE, SECRET_KEY); 80 ObjectInputStream in = new ObjectInputStream (new ByteArrayInputStream (data)); 81 SealedObject so = (SealedObject) in.readObject(); 82 return so.getObject(c); 83 } catch (Exception e) { 84 log.error("Unable to decrypt", e); 85 return null; 86 } 87 } 88 } 89 | Popular Tags |