1 10 11 package org.mmbase.util.transformers; 12 13 import java.io.Reader ; 14 import java.io.Writer ; 15 import java.security.GeneralSecurityException ; 16 17 import javax.crypto.Cipher; 18 import javax.crypto.spec.SecretKeySpec; 19 20 import org.mmbase.util.functions.Parameter; 21 import org.mmbase.util.functions.Parameters; 22 import org.mmbase.util.logging.Logger; 23 import org.mmbase.util.logging.Logging; 24 25 33 34 public class EncryptionTransformerFactory implements ParameterizedTransformerFactory { 35 private static final Logger log = Logging.getLoggerInstance(EncryptionTransformerFactory.class); 36 37 41 public static String encode(byte input[], String format) { 42 if ("hex".equalsIgnoreCase(format)) { 43 Hex h = new Hex(); 44 String output = h.transform(input); 45 return output; 46 } else if ("base64".equalsIgnoreCase(format)) { 47 Base64 b = new Base64(); 48 String output = b.transform(input); 49 return output; 50 } 51 return ""; 52 } 53 54 59 public static byte[] decode(String input, String format) throws IllegalArgumentException { 60 if ("hex".equalsIgnoreCase(format)) { 61 Hex h = new Hex(); 62 byte[] output = h.transformBack(input); 63 return output; 64 } else if ("base64".equalsIgnoreCase(format)) { 65 Base64 b = new Base64(); 66 byte[] output = b.transformBack(input); 67 return output; 68 } 69 return new byte[0]; 70 } 71 72 protected final static Parameter[] PARAMS = { 73 new Parameter("key", String .class, "1234567890abcdef"), 74 new Parameter("format", String .class, "hex"), 75 new Parameter("algorithm", String .class, "AES"), 76 new Parameter("direction", String .class, "encrypt") 77 }; 78 79 public Parameters createParameters() { 80 return new Parameters(PARAMS); 81 } 82 83 92 public Transformer createTransformer(Parameters parameters) { 93 String direction = (String ) parameters.get("direction"); 94 if ("encrypt".equalsIgnoreCase(direction)) { 95 return new Encryption((String ) parameters.get("key"), (String ) parameters.get("format"), (String ) parameters.get("algorithm")); 96 } else if ("decrypt".equalsIgnoreCase(direction)) { 97 return new Decryption((String ) parameters.get("key"), (String ) parameters.get("format"), (String ) parameters.get("algorithm")); 98 } else { 99 throw new UnsupportedOperationException ("Unknown value for attribute 'direction', (known are 'encrypt' and 'decrypt')"); 100 } 101 } 102 103 class Encryption extends ReaderTransformer { 104 private String key; 105 private String format; 106 private String algorithm; 107 108 Encryption(String key, String format, String algorithm) { 109 this.key = key; 110 this.format = format; 111 this.algorithm = algorithm; 112 113 if ((!"base64".equalsIgnoreCase(format)) & (!"hex".equalsIgnoreCase(format))) { 114 throw new UnsupportedOperationException ("Unknown value for attribute 'format', (known are 'base64' and 'hex')"); 115 } 116 } 117 118 public Writer transform(Reader r, Writer w) { 119 StringBuffer sb = new StringBuffer (); 120 121 try { 122 int i; 123 while ((i = r.read()) > -1) { 124 sb.append((char) i); 125 } 126 byte input[] = sb.toString().getBytes(); 127 128 byte[] secretKey = key.getBytes(); 129 SecretKeySpec skeySpec = new SecretKeySpec(secretKey, algorithm); 130 131 Cipher cipher = Cipher.getInstance(algorithm); 132 cipher.init(Cipher.ENCRYPT_MODE, skeySpec); 133 byte encrypted[] = cipher.doFinal(input); 134 135 String output = encode(encrypted, format); 136 137 w.write(output); 138 return w; 139 140 } catch (IllegalArgumentException h) { 141 throw new UnsupportedOperationException (h.getMessage()); 142 } catch (SecurityException g) { 143 throw new UnsupportedOperationException (g.getMessage()); 144 } catch (GeneralSecurityException f) { 145 throw new UnsupportedOperationException (f.getMessage()); 146 } catch (Exception e) { 147 log.error(e.getMessage() + Logging.stackTrace(e)); 148 } 149 return w; 150 } 151 } 152 153 class Decryption extends ReaderTransformer { 154 private String key; 155 private String format; 156 private String algorithm; 157 158 Decryption(String key, String format, String algorithm) { 159 this.key = key; 160 this.format = format; 161 this.algorithm = algorithm; 162 163 if ((!"base64".equalsIgnoreCase(format)) & (!"hex".equalsIgnoreCase(format))) { 164 throw new UnsupportedOperationException ("Unknown value for attribute 'format', (known are 'base64' and 'hex')"); 165 } 166 } 167 168 public Writer transform(Reader r, Writer w) { 169 StringBuffer sb = new StringBuffer (); 170 171 try { 172 int i; 173 while ((i = r.read()) > -1) { 174 sb.append((char) i); 175 } 176 177 byte[] input = decode(sb.toString(), format); 178 179 byte[] secretKey = key.getBytes(); 180 SecretKeySpec skeySpec = new SecretKeySpec(secretKey, algorithm); 181 182 Cipher cipher = Cipher.getInstance(algorithm); 183 cipher.init(Cipher.DECRYPT_MODE, skeySpec); 184 byte decrypted[] = cipher.doFinal(input); 185 String output = new String (decrypted); 186 w.write(output); 187 return w; 188 189 } catch (IllegalArgumentException h) { 190 throw new UnsupportedOperationException (h.getMessage()); 191 } catch (SecurityException g) { 192 throw new UnsupportedOperationException (g.getMessage()); 193 } catch (GeneralSecurityException f) { 194 throw new UnsupportedOperationException (f.getMessage()); 195 } catch (Exception e) { 196 log.error(e.getMessage() + Logging.stackTrace(e)); 197 } 198 return w; 199 } 200 } 201 } 202 | Popular Tags |