package common.crypto; import java.io.IOException; import java.io.Serializable; import java.security.InvalidKeyException; import java.security.NoSuchAlgorithmException; import java.util.HashMap; import java.util.Map; import javax.crypto.BadPaddingException; import javax.crypto.Cipher; import javax.crypto.IllegalBlockSizeException; import javax.crypto.KeyGenerator; import javax.crypto.NoSuchPaddingException; import javax.crypto.SecretKey; import org.apache.hivemind.util.Defense; import org.apache.tapestry.IComponent; import org.apache.tapestry.IPage; import org.apache.tapestry.IRequestCycle; import org.apache.tapestry.engine.IEngineService; import org.apache.tapestry.engine.ILink; import org.apache.tapestry.engine.state.StateObjectFactory; import org.apache.tapestry.error.RequestExceptionReporter; import org.apache.tapestry.services.LinkFactory; import org.apache.tapestry.services.ServiceConstants; import org.apache.tapestry.web.WebResponse; import common.ExceptionFormatter; public class CryptoService implements Serializable, ICrypto, IEngineService, StateObjectFactory { public static final String SERVICE_NAME = "CryptoService"; private RequestExceptionReporter exceptionReporter; private LinkFactory linkFactory; private WebResponse response; private Cipher cipher; private SecretKey encryptionKey; public CryptoService() { generateKey(); createCipher(); } public ILink getLink(boolean post, Object parameter) { Defense.isAssignable(parameter, IComponent.class, "parameter"); IComponent component = (IComponent) parameter; Map parameters = new HashMap(); parameters.put(ServiceConstants.SERVICE, getName()); parameters .put(ServiceConstants.PAGE, component.getPage().getPageName()); parameters.put(ServiceConstants.COMPONENT, component.getIdPath()); return linkFactory.constructLink(this, false, parameters, true); } public void service(IRequestCycle cycle) throws IOException { String pageName = cycle.getParameter(ServiceConstants.PAGE); String componentId = cycle.getParameter(ServiceConstants.COMPONENT); IPage page = cycle.getPage(pageName); IComponent component = page.getNestedComponent(componentId); try { } catch (ClassCastException ex) { exceptionReporter.reportRequestException("Component " + component.getExtendedId() + " does not implement IChartProvider.", ex); return; } catch (Throwable ex) { exceptionReporter.reportRequestException( "Error creating JPEG stream.", ex); return; } return; } public String getName() { return SERVICE_NAME; } public Object createStateObject() { return new CryptoService(); } public SecretKey generateKey() { try { KeyGenerator keygen = KeyGenerator.getInstance("DES"); encryptionKey = keygen.generateKey(); } catch (NoSuchAlgorithmException ex) { System.out.println(ExceptionFormatter.logException(ex)); } return encryptionKey; } public Cipher createCipher() { try { cipher = Cipher.getInstance("DES/ECB/PKCS5Padding"); } catch (NoSuchAlgorithmException ex) { System.out.println(ExceptionFormatter.logException(ex)); } catch (NoSuchPaddingException ex) { System.out.println(ExceptionFormatter.logException(ex)); } return cipher; } public String enCipher(String value) { String result = ""; try { cipher.init(Cipher.ENCRYPT_MODE, encryptionKey); byte[] text = cipher.doFinal(value.getBytes()); result = new String(text.toString()); } catch (InvalidKeyException ex) { System.out.println(ExceptionFormatter.logException(ex)); } catch (BadPaddingException ex) { System.out.println(ExceptionFormatter.logException(ex)); } catch (IllegalBlockSizeException ex) { System.out.println(ExceptionFormatter.logException(ex)); } return result; } public String deCipher(String value) { String result = ""; try { cipher.init(Cipher.DECRYPT_MODE, encryptionKey); // Decrypt the ciphertext byte[] text = cipher.doFinal(value.getBytes()); result = new String(text.toString()); } catch (InvalidKeyException ex) { System.out.println(ExceptionFormatter.logException(ex)); } catch (BadPaddingException ex) { System.out.println(ExceptionFormatter.logException(ex)); } catch (IllegalBlockSizeException ex) { System.out.println(ExceptionFormatter.logException(ex)); } return result; } public Cipher getCipher() { return cipher; } public void setCipher(Cipher cipher) { this.cipher = cipher; } public SecretKey getEncryptionKey() { return encryptionKey; } public void setEncryptionKey(SecretKey key) { this.encryptionKey = key; } public RequestExceptionReporter getExceptionReporter() { return exceptionReporter; } public void setExceptionReporter(RequestExceptionReporter exceptionReporter) { this.exceptionReporter = exceptionReporter; } public LinkFactory getLinkFactory() { return linkFactory; } public void setLinkFactory(LinkFactory linkFactory) { this.linkFactory = linkFactory; } public WebResponse getResponse() { return response; } public void setResponse(WebResponse response) { this.response = response; } }