⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 fileencrypt.java

📁 一个java开发的非常全面的关于证书发放
💻 JAVA
字号:
/*
  Name:         FileEncrypt.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.Crypt;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.TagSupport;

/**
 * JSP tag used for symmetric key encryption/decryption of files
 *
 * @jsp.tag
 * name="fileencrypt"
 * display-name="FileEncrypt"
 * body-content="empty"
 * example="
 *		<%-- Encrypts a file with standard algorithm --%>

 *		<jce:fileencrypt

 *			file=\"c:/zipfile.zip\"

 *			newfile=\"c:/zipfile.zip.encrypted\" 

 *			keyfile=\"c:/jspkey.key\"

 *			passphrase=\"<%= new StringBuffer(\"password\") %>\"

 *			action=\"encrypt\"/> 


 *
 *		<%-- Decrypts the encrypted file back again --%>

 *		<jce:fileencrypt

 *			file=\"c:/zipfile.zip.encrypted\"

 *			newfile=\"c:/zipfile.zip.decrypted\"

 *			keyfile=\"c:/jspkey.key\"

 *			passphrase=\"<%= new StringBuffer(\"password\") %>\"

 *			action=\"decrypt\"/>"
 *
 * description="JSP tag used for symmetric key encryption/decryption of files"
 *
 * @author Gert Van Ham
 * @author hamgert@users.sourceforge.net
 * @author http://jcetaglib.sourceforge.net
 * @version $Id: FileEncrypt.java,v 1.5 2004/04/15 07:28:35 hamgert Exp $
 */
public class FileEncrypt extends TagSupport {
    private static final String ENCRYPT = "encrypt";

    private String file;    // tag attribute
    private String newfile; // tag attribute

    private String algorithm = "AES";          // tag attribute
    private String mode = "CBC";               // tag attribute
    private String padding = "PKCS7Padding";   // tag attribute
    private String keyfile;                    // tag attribute
    private StringBuffer passphrase;                 // tag attribute

    private String action = "ENCRYPT";  // tag attribute

    public int doStartTag() throws JspException {

        // Encrypt or decrypt
        try {
            if (ENCRYPT.equalsIgnoreCase(action))
                Crypt.encryptFile(file, newfile, keyfile, passphrase, algorithm, mode, padding, null);
            else
                Crypt.encryptFile(file, newfile, keyfile, passphrase, algorithm, mode, padding, null);
        } catch (Exception e) {
            throw new JspException("JCE Exception: " + e.getMessage(), e);
        }

        return SKIP_BODY;
    } //doStartTag()

    public void release() {
        // Cleanup all sensitive information
        Clean.blank(passphrase);

        super.release();
    } //release(

    /**
     * @jsp.attribute
     *     description="The key algorithm. Default is 'AES'"
     *     type="java.lang.String"
     *     required="false"
     *     rtexprvalue="true"
     */
    public void setAlgorithm(String algorithm) {
        this.algorithm = algorithm;
    }

    /**
     * @jsp.attribute
     *     description="Encryption mode. Default is 'CBC'"
     *     type="java.lang.String"
     *     required="false"
     *     rtexprvalue="true"
     */
    public void setMode(String mode) {
        this.mode = mode;
    }

    /**
     * @jsp.attribute
     *     description="Padding scheme. Default is 'PKCS7Padding'"
     *     type="java.lang.String"
     *     required="false"
     *     rtexprvalue="true"
     */
    public void setPadding(String padding) {
        this.padding = padding;
    }

    /**
     * @jsp.attribute
     *     description="The symmetric key file(name)"
     *     type="java.lang.String"
     *     required="true"
     *     rtexprvalue="true"
     */
    public void setKeyfile(String keyfile) {
        this.keyfile = keyfile;
    }

    /**
     * @jsp.attribute
     *     description="Keystore passphrase"
     *     type="java.lang.StringBuffer"
     *     required="true"
     *     rtexprvalue="true"
     */
    public void setPassphrase(StringBuffer passphrase) {
        this.passphrase = passphrase;
    }

    /**
     * @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 + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -