1 19 20 package net.sourceforge.jcetaglib.taglib.crypto; 21 22 import net.sourceforge.jcetaglib.lib.Clean; 23 import net.sourceforge.jcetaglib.lib.PBECrypt; 24 25 import javax.servlet.jsp.JspException ; 26 import javax.servlet.jsp.JspWriter ; 27 import javax.servlet.jsp.PageContext ; 28 import javax.servlet.jsp.tagext.BodyTagSupport ; 29 import java.io.IOException ; 30 31 66 public class PBEEncrypt extends BodyTagSupport { 67 68 private static final String PAGE = "page"; 69 private static final String REQUEST = "request"; 70 private static final String SESSION = "session"; 71 private static final String APPLICATION = "application"; 72 73 private static final String ENCRYPT = "encrypt"; 74 75 private StringBuffer value; private String var; private int scope = PageContext.PAGE_SCOPE; 79 private StringBuffer passphrase; 81 private String algorithm = "PBEWithSHAAndIDEA-CBC"; private String seed; 84 private String action = "ENCRYPT"; 86 private StringBuffer input; private StringBuffer output; 89 public static int getScope(String scope) { 90 int ret = PageContext.PAGE_SCOPE; 92 if (REQUEST.equalsIgnoreCase(scope)) 93 ret = PageContext.REQUEST_SCOPE; 94 else if (SESSION.equalsIgnoreCase(scope)) 95 ret = PageContext.SESSION_SCOPE; 96 else if (APPLICATION.equalsIgnoreCase(scope)) 97 ret = PageContext.APPLICATION_SCOPE; 98 else if (PAGE.equalsIgnoreCase(scope)) 99 ret = PageContext.PAGE_SCOPE; 100 101 return ret; 102 } 104 public int doEndTag() throws JspException { 105 if (value != null) { 107 input = value; 109 } else { 110 if (bodyContent == null || bodyContent.getString() == null) { 112 input = new StringBuffer (""); 113 } else { 114 input = new StringBuffer (bodyContent.getString().trim()); 115 } 116 } 117 118 try { 120 if (ENCRYPT.equalsIgnoreCase(action)) { 121 if (seed == null) { 122 output = PBECrypt.encrypt(input, passphrase, null, algorithm); 123 } else { 124 output = PBECrypt.encrypt(input, passphrase, seed.getBytes(), algorithm); 125 } 126 } else { 127 output = PBECrypt.decrypt(input, passphrase, algorithm); 128 } 129 } catch (Exception e) { 130 throw new JspException ("JCE Exception: " + e.toString(), e); 131 } 132 133 if (var != null) { 135 if (output != null) { 136 pageContext.setAttribute(var, output, scope); 137 } 138 } else { 139 if (bodyContent != null) { 140 bodyContent.clearBody(); 141 } 142 143 try { 144 JspWriter w = pageContext.getOut(); 145 w.print(output); 146 } catch (IOException ex) { 147 throw new JspException (ex.getMessage(), ex); 148 } 149 } 150 151 return EVAL_PAGE; 152 } 154 public void release() { 155 Clean.blank(value); 157 Clean.blank(passphrase); 158 Clean.blank(input); 159 Clean.blank(output); 160 161 super.release(); 162 } 164 171 public void setValue(StringBuffer value) { 172 this.value = value; 173 } 174 175 public StringBuffer getValue() { 176 return value; 177 } 178 179 186 public void setVar(String var) { 187 this.var = var; 188 } 189 190 public String getVar() { 191 return var; 192 } 193 194 201 public void setScope(String scope) { 202 this.scope = getScope(scope); 203 } 204 205 212 public void setPassphrase(StringBuffer passphrase) { 213 this.passphrase = passphrase; 214 } 215 216 223 public void setAlgorithm(String algorithm) { 224 this.algorithm = algorithm; 225 } 226 227 234 public void setSeed(String seed) { 235 this.seed = seed; 236 } 237 238 245 public void setAction(String action) { 246 this.action = action; 247 } 248 } | Popular Tags |