1 19 20 package net.sourceforge.jcetaglib.taglib.crypto; 21 22 import net.sourceforge.jcetaglib.lib.CertTools; 23 import net.sourceforge.jcetaglib.lib.Clean; 24 import net.sourceforge.jcetaglib.lib.Hybrid; 25 26 import javax.servlet.jsp.JspException ; 27 import javax.servlet.jsp.JspWriter ; 28 import javax.servlet.jsp.PageContext ; 29 import javax.servlet.jsp.tagext.BodyTagSupport ; 30 import java.io.ByteArrayInputStream ; 31 import java.io.IOException ; 32 import java.io.InputStream ; 33 import java.security.cert.X509Certificate ; 34 35 56 public class EncryptWithHMAC extends BodyTagSupport { 57 private static final String PAGE = "page"; 58 private static final String REQUEST = "request"; 59 private static final String SESSION = "session"; 60 private static final String APPLICATION = "application"; 61 62 private StringBuffer value; private String var; private int scope = PageContext.PAGE_SCOPE; 66 private String file; private String newfile; 69 private String algorithm = "AES"; private String seed; private int strength = 256; private String mode = "CBC"; private String padding = "PKCS7Padding"; 75 76 77 private String recpemstring; 80 private String recpemfile; 83 private StringBuffer input; private StringBuffer output; 86 public static int getScope(String scope) { 87 int ret = PageContext.PAGE_SCOPE; 89 if (REQUEST.equalsIgnoreCase(scope)) 90 ret = PageContext.REQUEST_SCOPE; 91 else if (SESSION.equalsIgnoreCase(scope)) 92 ret = PageContext.SESSION_SCOPE; 93 else if (APPLICATION.equalsIgnoreCase(scope)) 94 ret = PageContext.APPLICATION_SCOPE; 95 else if (PAGE.equalsIgnoreCase(scope)) 96 ret = PageContext.PAGE_SCOPE; 97 98 return ret; 99 } 101 public int doEndTag() throws JspException { 102 X509Certificate reccert = null; 103 104 if (value != null) { 106 input = value; 108 } else { 109 if (bodyContent == null || bodyContent.getString() == null) { 111 input = new StringBuffer (""); 112 } else { 113 input = new StringBuffer (bodyContent.getString().trim()); 114 } 115 } 116 117 try { 119 if (recpemfile == null || recpemfile == "") { 120 InputStream pemstream = new ByteArrayInputStream (recpemstring.getBytes()); 122 reccert = CertTools.getCertfromPEM(pemstream); 123 } else { 124 reccert = CertTools.getCertfromPEM(recpemfile); 126 } 127 } catch (Exception e) { 128 throw new JspException ("JCE Exception - PEM could not be loaded: " + e.getMessage(), e); 129 } 130 131 try { 133 if (file != null) { 134 if (seed == null) { 135 Hybrid.encryptFileWithHMAC(file 136 , newfile 137 , reccert.getPublicKey() 138 , algorithm 139 , null 140 , strength 141 , mode 142 , padding); 143 } else { 144 Hybrid.encryptFileWithHMAC(file 145 , newfile 146 , reccert.getPublicKey() 147 , algorithm 148 , seed.getBytes() 149 , strength 150 , mode 151 , padding); 152 } 153 } else { 154 if (seed == null) { 155 output = Hybrid.encryptWithHMAC(input 156 , reccert.getPublicKey() 157 , algorithm 158 , null 159 , strength 160 , mode 161 , padding); 162 } else { 163 output = Hybrid.encryptWithHMAC(input 164 , reccert.getPublicKey() 165 , algorithm 166 , seed.getBytes() 167 , strength 168 , mode 169 , padding); 170 } 171 } 172 } catch (Exception e) { 173 throw new JspException ("JCE Exception: " + e.getMessage(), e); 174 } 175 176 if (var != null) { 178 if (output != null) { 179 pageContext.setAttribute(var, output, scope); 180 } 181 } else { 182 if (file == null || file == "") { 183 if (bodyContent != null) { 184 bodyContent.clearBody(); 185 } 186 187 try { 188 JspWriter w = pageContext.getOut(); 189 w.print(output); 190 } catch (IOException ex) { 191 throw new JspException (ex.getMessage(), ex); 192 } 193 } 194 } 195 196 return EVAL_PAGE; 197 } 198 199 public void release() { 200 Clean.blank(value); 202 Clean.blank(input); 203 Clean.blank(output); 204 205 super.release(); 206 } 208 215 public void setValue(StringBuffer value) { 216 this.value = value; 217 } 218 219 public StringBuffer getValue() { 220 return value; 221 } 222 223 230 public void setVar(String var) { 231 this.var = var; 232 } 233 234 public String getVar() { 235 return var; 236 } 237 238 245 public void setScope(String scope) { 246 this.scope = getScope(scope); 247 } 248 249 256 public void setAlgorithm(String algorithm) { 257 this.algorithm = algorithm; 258 } 259 260 267 public void setSeed(String seed) { 268 this.seed = seed; 269 } 270 271 278 public void setStrength(int strength) { 279 this.strength = strength; 280 } 281 282 289 public void setMode(String mode) { 290 this.mode = mode; 291 } 292 293 300 public void setPadding(String padding) { 301 this.padding = padding; 302 } 303 304 311 public void setRecpemfile(String recpemfile) { 312 this.recpemfile = recpemfile; 313 } 314 315 322 public void setRecpemstring(String recpemstring) { 323 this.recpemstring = recpemstring; 324 } 325 326 333 public void setFile(String file) { 334 this.file = file; 335 } 336 337 344 public void setNewfile(String newfile) { 345 this.newfile = newfile; 346 } 347 } | Popular Tags |