分享

DesManager.java

 魔奇工作室 2014-04-29
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;
    }
}

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多