1 19 20 package net.sourceforge.jcetaglib.taglib.crypto; 21 22 import net.sourceforge.jcetaglib.lib.Clean; 23 import net.sourceforge.jcetaglib.lib.Crypt; 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 54 public class Encrypt extends BodyTagSupport { 55 56 private static final String PAGE = "page"; 57 private static final String REQUEST = "request"; 58 private static final String SESSION = "session"; 59 private static final String APPLICATION = "application"; 60 61 private static final String ENCRYPT = "encrypt"; 62 63 private StringBuffer value; private String var; private int scope = PageContext.PAGE_SCOPE; 67 private String algorithm = "AES"; private String mode = "CBC"; private String padding = "PKCS7Padding"; private String keyfile; private StringBuffer passphrase; 73 private String action = "ENCRYPT"; 75 private StringBuffer input; private StringBuffer output; 78 public static int getScope(String scope) { 79 int ret = PageContext.PAGE_SCOPE; 81 if (REQUEST.equalsIgnoreCase(scope)) 82 ret = PageContext.REQUEST_SCOPE; 83 else if (SESSION.equalsIgnoreCase(scope)) 84 ret = PageContext.SESSION_SCOPE; 85 else if (APPLICATION.equalsIgnoreCase(scope)) 86 ret = PageContext.APPLICATION_SCOPE; 87 else if (PAGE.equalsIgnoreCase(scope)) 88 ret = PageContext.PAGE_SCOPE; 89 90 return ret; 91 } 93 public int doEndTag() throws JspException { 94 if (value != null) { 96 input = value; 98 } else { 99 if (bodyContent == null || bodyContent.getString() == null) { 101 input = new StringBuffer (""); 102 } else { 103 input = new StringBuffer (bodyContent.getString().trim()); 104 } 105 } 106 107 try { 109 if (ENCRYPT.equalsIgnoreCase(action)) { 110 output = Crypt.encrypt(input, keyfile, passphrase, algorithm, mode, padding, null); 111 } else { 112 output = Crypt.decrypt(input, keyfile, passphrase, algorithm, mode, padding); 113 } 114 } catch (Exception e) { 115 throw new JspException ("JCE Exception: " + e.getMessage(), e); 116 } 117 118 if (var != null) { 120 if (output != null) { 121 pageContext.setAttribute(var, output, scope); 122 } 123 } else { 124 if (bodyContent != null) { 125 bodyContent.clearBody(); 126 } 127 128 try { 129 JspWriter w = pageContext.getOut(); 130 w.print(output); 131 } catch (IOException ex) { 132 throw new JspException (ex.getMessage(), ex); 133 } 134 } 135 136 return EVAL_PAGE; 137 } 139 public void release() { 140 Clean.blank(value); 142 Clean.blank(passphrase); 143 Clean.blank(input); 144 Clean.blank(output); 145 146 super.release(); 147 } 149 156 public void setValue(StringBuffer value) { 157 this.value = value; 158 } 159 160 public StringBuffer getValue() { 161 return value; 162 } 163 164 171 public void setVar(String var) { 172 this.var = var; 173 } 174 175 public String getVar() { 176 return var; 177 } 178 179 186 public void setScope(String scope) { 187 this.scope = getScope(scope); 188 } 189 190 197 public void setAlgorithm(String algorithm) { 198 this.algorithm = algorithm; 199 } 200 201 208 public void setMode(String mode) { 209 this.mode = mode; 210 } 211 212 219 public void setPadding(String padding) { 220 this.padding = padding; 221 } 222 223 230 public void setKeyfile(String keyfile) { 231 this.keyfile = keyfile; 232 } 233 234 241 public void setPassphrase(StringBuffer passphrase) { 242 this.passphrase = passphrase; 243 } 244 245 252 public void setAction(String action) { 253 this.action = action; 254 } 255 } | Popular Tags |