pbefileencrypt.java
来自「一个java开发的非常全面的关于证书发放」· Java 代码 · 共 160 行
JAVA
160 行
/*
Name: PBEFileEncrypt.java
Licensing: LGPL
API: Sun (http://java.sun.com) JCE 1.2.2 API (cleanroom implementation by Bouncy Castle)
Provider: Bouncy Castle (http://www.bouncycastle.org)
Disclaimer:
COVERED CODE IS PROVIDED UNDER THIS LICENSE ON AN "AS IS" BASIS, WITHOUT WARRANTY OF ANY KIND,
EITHER EXPRESSED OR IMPLIED, INCLUDING, WITHOUT LIMITATION, WARRANTIES THAT THE COVERED CODE
IS FREE OF DEFECTS, MERCHANTABLE, FIT FOR A PARTICULAR PURPOSE OR NON-INFRINGING. THE ENTIRE
RISK AS TO THE QUALITY AND PERFORMANCE OF THE COVERED CODE IS WITH YOU. SHOULD ANY COVERED CODE
PROVE DEFECTIVE IN ANY RESPECT, YOU (NOT THE INITIAL DEVELOPER OR ANY OTHER CONTRIBUTOR)
ASSUME THE COST OF ANY NECESSARY SERVICING, REPAIR OR CORRECTION. THIS DISCLAIMER OF WARRANTY
CONSTITUTES AN ESSENTIAL PART OF THIS LICENSE. NO USE OF ANY COVERED CODE IS AUTHORIZED
HEREUNDER EXCEPT UNDER THIS DISCLAIMER.
*/
package net.sourceforge.jcetaglib.taglib.crypto;
import net.sourceforge.jcetaglib.lib.Clean;
import net.sourceforge.jcetaglib.lib.PBECrypt;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.TagSupport;
/**
* JSP tag used for PBE (password-based) encryption/decryption of files
*
* @jsp.tag
* name="pbefileencrypt"
* display-name="PBEFileEncrypt"
* body-content="empty"
* example="
* <%-- Encrypts a file with standard algorithm --%>
* <jce:pbefileencrypt
* 	file=\"c:/zipfile.zip\"
* 	newfile=\"c:/zipfile.zip.encrypted\"
* 	passphrase=\"<%= new StringBuffer(\"password\") %>\"
* 	action=\"encrypt\"/>
*
* <%-- Decrypts the encrypted file back again --%>
* <jce:pbefileencrypt
* 	file=\"c:/zipfile.zip.encrypted\"
* 	newfile=\"c:/zipfile.zip.decrypted\"
* 	passphrase=\"<%= new StringBuffer(\"password\") %>\"
* 	action=\"decrypt\"/>"
*
* description="JSP tag used for PBE (password-based) encryption/decryption of files"
*
* @author Gert Van Ham
* @author hamgert@users.sourceforge.net
* @author http://jcetaglib.sourceforge.net
* @version $Id: PBEFileEncrypt.java,v 1.5 2004/04/15 07:28:36 hamgert Exp $
*/
public class PBEFileEncrypt extends TagSupport {
private static final String ENCRYPT = "encrypt";
private StringBuffer passphrase; // tag attribute
private String algorithm = "PBEWithSHAAndIDEA-CBC"; // tag attribute
private String seed; // tag attribute
private String action = "ENCRYPT"; // tag attribute
private String file; // tag attribute
private String newfile; // tag attribute
public int doStartTag() throws JspException {
// Encrypt or decrypt
try {
if (ENCRYPT.equalsIgnoreCase(action))
if (seed == null) {
PBECrypt.encryptFile(file, newfile, passphrase, null, algorithm);
} else {
PBECrypt.encryptFile(file, newfile, passphrase, seed.getBytes(), algorithm);
}
else
PBECrypt.decryptFile(file, newfile, passphrase, algorithm);
} catch (Exception e) {
// Cleanup sensitive information
Clean.blank(passphrase);
passphrase = null;
throw new JspException("JCE Exception: " + e.getMessage(), e);
}
// Cleanup all sensitive information
Clean.blank(passphrase);
passphrase = null;
return SKIP_BODY;
}
/**
* @jsp.attribute
* description="The passphrase or password to encrypt/decrypt"
* type="java.lang.StringBuffer"
* required="true"
* rtexprvalue="true"
*/
public void setPassphrase(StringBuffer passphrase) {
this.passphrase = passphrase;
}
/**
* @jsp.attribute
* description="The encryption/decryption algorithm. Default is 'PBEWithSHAAndIDEA-CBC'"
* type="java.lang.String"
* required="false"
* rtexprvalue="true"
*/
public void setAlgorithm(String algorithm) {
this.algorithm = algorithm;
}
/**
* @jsp.attribute
* description="Optional seed for SecureRandom "
* type="java.lang.String"
* required="false"
* rtexprvalue="true"
*/
public void setSeed(String seed) {
this.seed = seed;
}
/**
* @jsp.attribute
* description="The action to perform. 'encrypt' for encryption, 'decrypt' for decryption. Default is 'encrypt'"
* type="java.lang.String"
* required="false"
* rtexprvalue="false"
*/
public void setAction(String action) {
this.action = action;
}
/**
* @jsp.attribute
* description="The file to encrypt or decrypt"
* type="java.lang.String"
* required="true"
* rtexprvalue="true"
*/
public void setFile(String file) {
this.file = file;
}
/**
* @jsp.attribute
* description="The file to store the encrypted or decrypted result"
* type="java.lang.String"
* required="true"
* rtexprvalue="true"
*/
public void setNewfile(String newfile) {
this.newfile = newfile;
}
}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?