import java.security.InvalidKeyException;
import java.security.Key;
import java.security.NoSuchAlgorithmException;
import java.security.SecureRandom;
import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.KeyGenerator;
import javax.crypto.NoSuchPaddingException;
public class DESManager {
private static final String ALGORITHM = ConstantFactory.PASSWORD_ALGORITHM;
private static final String BLOWFISH = ConstantFactory.PASSWORD_ENCRYPT_KEY;
private static Key key;
static {
try {
KeyGenerator generator = KeyGenerator.getInstance(ALGORITHM);
generator.init(new SecureRandom(BLOWFISH.getBytes()));
key = generator.generateKey();
generator = null;
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
}
}
private DESManager() {
super();
}
/**
* @param input
* @return the encrypted output
* @throws InvalidKeyException
* @throws NoSuchAlgorithmException
* @throws NoSuchPaddingException
* @throws IllegalBlockSizeException
* @throws BadPaddingException
*/
public static String encrypt(String input) throws InvalidKeyException,
NoSuchAlgorithmException, NoSuchPaddingException, IllegalBlockSizeException,
BadPaddingException {
return byte2hex(getEncCode(input.getBytes()));
}
/**
* @param input
* @return the decrypted output
* @throws InvalidKeyException
* @throws NoSuchAlgorithmException
* @throws NoSuchPaddingException
* @throws IllegalBlockSizeException
* @throws BadPaddingException
*/
public static String decrypt(String input) throws InvalidKeyException,
NoSuchAlgorithmException, NoSuchPaddingException, IllegalBlockSizeException,
BadPaddingException {
return new String(getDecCode(hex2byte(input.getBytes())));
}
/**
* @param byteS
* @return the enc code
* @throws NoSuchAlgorithmException
* @throws NoSuchPaddingException
* @throws InvalidKeyException
* @throws IllegalBlockSizeException
* @throws BadPaddingException
*/
private static byte[] getEncCode(byte[] byteS) throws NoSuchAlgorithmException,
NoSuchPaddingException, InvalidKeyException, IllegalBlockSizeException,
BadPaddingException {
Cipher cipher = Cipher.getInstance(ALGORITHM);
cipher.init(Cipher.ENCRYPT_MODE, key);
return cipher.doFinal(byteS);
}
/**
* @param byteD
* @return the dec code
* @throws NoSuchAlgorithmException
* @throws NoSuchPaddingException
* @throws InvalidKeyException
* @throws IllegalBlockSizeException
* @throws BadPaddingException
*/
private static byte[] getDecCode(byte[] byteD) throws NoSuchAlgorithmException,
NoSuchPaddingException, InvalidKeyException, IllegalBlockSizeException,
BadPaddingException {
Cipher cipher = Cipher.getInstance(ALGORITHM);
cipher.init(Cipher.DECRYPT_MODE, key);
return cipher.doFinal(byteD);
}
/**
* @param b
* @return the uppercase hex
*/
private static String byte2hex(byte[] b) {
String hex = "";
String stmp = "";
for (int i = 0; i < b.length; i++) {
stmp = Integer.toHexString(b[i] & 0XFF);
if (stmp.length() == 1) {
hex = hex + '0' + stmp;
} else {
hex = hex + stmp;
}
}
return hex.toUpperCase();
}
/**
* @param b
* @return the byteFinal
*/
private static byte[] hex2byte(byte[] b) {
if (b.length % 2 != 0) {
throw new IllegalArgumentException("b.length % 2 != 0");
}
byte[] byteFinal = new byte[b.length / 2];
for (int i = 0; i < b.length; i += 2) {
String item = new String(b, i, 2);
byteFinal[i / 2] = (byte) Integer.parseInt(item, 16);
}
return byteFinal;
}
}
本站僅提供存儲服務(wù),所有內(nèi)容均由用戶發(fā)布,如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請
點(diǎn)擊舉報。