📄 elfunctions.java
字号:
/*
Name: ELFunctions.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.
(C) Copyright 2004 Gert Van Ham
*/
package net.sourceforge.jcetaglib.jsp20;
import net.sourceforge.jcetaglib.exceptions.CryptoException;
import net.sourceforge.jcetaglib.exceptions.KeystoreException;
import net.sourceforge.jcetaglib.lib.Crypt;
import net.sourceforge.jcetaglib.lib.Digesters;
import net.sourceforge.jcetaglib.lib.Macs;
import net.sourceforge.jcetaglib.lib.PBECrypt;
import java.io.IOException;
/**
* JSP 2.0 EL (Expression Language) based JCE functions
*
* @author Gert Van Ham
* @author hamgert@users.sourceforge.net
* @author http://jcetaglib.sourceforge.net
* @version $Id: ELFunctions.java,v 1.1 2004/04/14 09:48:29 hamgert Exp $
*/
public class ELFunctions {
/**
* Creates a digest from a string
*
* @param text creates the digest from this text
* @param algorithm the digest algorithm. 'SHA-1' if null
* @return the generated digest string in BASE64
* @throws CryptoException for all encryption errors
*/
public static String digest(String text,
String algorithm) throws CryptoException {
if (algorithm == null || algorithm.equalsIgnoreCase("")) {
return new String(Digesters.hash(new StringBuffer(text), "SHA-1"));
} else {
return new String(Digesters.hash(new StringBuffer(text), algorithm));
}
}
/**
* Creates a form digest from a parameter string
*
* @param text create the form digest from this parameter string
* @param digest the digest algorithm. 'SHA-1' if null
* @param keyfile the symmetric key file
* @param passphrase the symmetric key file passphrase
* @param algorithm the symmetric key algorithm
* @return form digest in BASE64 format
* @throws CryptoException for all encryption errors
* @throws IOException I/O errors
*/
public static String formdigest(String text,
String digest,
String keyfile,
String passphrase,
String algorithm) throws CryptoException, IOException {
if (digest == null || digest.equalsIgnoreCase("")) {
return new String(Digesters.formDigest(new StringBuffer(text), "SHA-1", keyfile, new StringBuffer(passphrase), algorithm));
} else {
return new String(Digesters.formDigest(new StringBuffer(text), digest, keyfile, new StringBuffer(passphrase), algorithm));
}
}
/**
* Encrypts a string with PBE and returns
* the ciphered text in BASE64 format.
*
* @param text the plain text
* @param passphrase the password or passphrase
* @param algorithm the PBE algorithm. "PBEWithSHAAndIDEA-CBC" if null
* @return the cipherstring in BASE64 format
* @throws CryptoException for all encryption errors
*/
public static String pbeencrypt(String text,
String passphrase,
String algorithm) throws CryptoException {
if (algorithm == null || algorithm.equalsIgnoreCase("")) {
return new String(PBECrypt.encrypt(new StringBuffer(text), new StringBuffer(passphrase), "PBEWithSHAAndIDEA-CBC"));
} else {
return new String(PBECrypt.encrypt(new StringBuffer(text), new StringBuffer(passphrase), algorithm));
}
}
/**
* Decrypts a ciphered BASE64 string with PBE
*
* @param ciphertext the text to decipher
* @param passphrase password or passphrase
* @param algorithm the PBE algorithm. "PBEWithSHAAndIDEA-CBC" if null
* @return the plain text
* @throws CryptoException for all encryption errors
*/
public static String pbedecrypt(String ciphertext,
String passphrase,
String algorithm) throws CryptoException {
if (algorithm == null || algorithm.equalsIgnoreCase("")) {
return new String(PBECrypt.decrypt(new StringBuffer(ciphertext), new StringBuffer(passphrase), "PBEWithSHAAndIDEA-CBC"));
} else {
return new String(PBECrypt.decrypt(new StringBuffer(ciphertext), new StringBuffer(passphrase), algorithm));
}
}
/**
* Encrypts a string with a symmetric key and returns
* the ciphered text in BASE64 format.
*
* @param text the string to encrypt
* @param keyfile the symmetric key file
* @param passphrase symmetric key file passphrase
* @param algorithm symmetric key file algorithm. AES if null
* @param mode encryption mode. CBC if null
* @param padding encryption padding. PKCS7Padding if null
* @return the encrypted text in BASE64 format
* @throws CryptoException encryption errors
* @throws KeystoreException keystore errors
*/
public static String encrypt(String text,
String keyfile,
String passphrase,
String algorithm,
String mode,
String padding) throws CryptoException, KeystoreException {
String t_algorithm = algorithm;
String t_mode = mode;
String t_padding = padding;
if (algorithm == null || algorithm.equalsIgnoreCase("")) {
t_algorithm = "AES";
}
if (mode == null || mode.equalsIgnoreCase("")) {
t_mode = "CBC";
}
if (padding == null || padding.equalsIgnoreCase("")) {
t_padding = "PKCS7Padding";
}
return new String(Crypt.encrypt(new StringBuffer(text), keyfile, new StringBuffer(passphrase), t_algorithm, t_mode, t_padding, null));
}
/**
* Decrypts a ciphered BASE64 string with a symmetric key
*
* @param ciphertext the ciphered BASE64 string
* @param keyfile the symmetric key file
* @param passphrase symmetric key file passphrase
* @param algorithm symmetric key file algorithm. AES if null
* @param mode encryption mode. CBC if null
* @param padding encryption padding. PKCS7Padding if null
* @return the plaintext
* @throws CryptoException encryption errors
* @throws KeystoreException keystore errors
*/
public static String decrypt(String ciphertext,
String keyfile,
String passphrase,
String algorithm,
String mode,
String padding) throws CryptoException, KeystoreException {
String t_algorithm = algorithm;
String t_mode = mode;
String t_padding = padding;
if (algorithm == null || algorithm.equalsIgnoreCase("")) {
t_algorithm = "AES";
}
if (mode == null || mode.equalsIgnoreCase("")) {
t_mode = "CBC";
}
if (padding == null || padding.equalsIgnoreCase("")) {
t_padding = "PKCS7Padding";
}
return new String(Crypt.decrypt(new StringBuffer(ciphertext), keyfile, new StringBuffer(passphrase), t_algorithm, t_mode, t_padding));
}
/**
* Create a MAC from a string
*
* @param text the text string
* @param keyfile the symmetric key file location
* @param passphrase the symmetric key file passphrase
* @param algorithm the symmetric key file algorithm
* @param macname MAC algorithm. "HMac-SHA512" if null
* @return MAC code
* @throws CryptoException MAC errors
*/
public static String mac(String text,
String keyfile,
String passphrase,
String algorithm,
String macname) throws CryptoException {
if (macname == null || macname.equalsIgnoreCase("")) {
return new String(Macs.generateMAC(new StringBuffer(text), keyfile, new StringBuffer(passphrase), algorithm, "HMac-SHA512"));
} else {
return new String(Macs.generateMAC(new StringBuffer(text), keyfile, new StringBuffer(passphrase), algorithm, macname));
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -