免费视频淫片aa毛片_日韩高清在线亚洲专区vr_日韩大片免费观看视频播放_亚洲欧美国产精品完整版

打開APP
userphoto
未登錄

開通VIP,暢享免費(fèi)電子書等14項(xiàng)超值服

開通VIP
DesManager.java
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)擊舉報。
打開APP,閱讀全文并永久保存 查看更多類似文章
猜你喜歡
類似文章
Java中常用的加密方法(JDK)
InvalidKeyException java.security.InvalidKeyException:沒有安裝的提供程序支持此密鑰:(空)
非對稱加解密——RSA加密、解密以及數(shù)字簽名
Android 和 PHP 之間進(jìn)行數(shù)據(jù)加密傳輸
RSA加解密的java實(shí)現(xiàn)(及中途的各種異常情況分析)
java md5加密
更多類似文章 >>
生活服務(wù)
分享 收藏 導(dǎo)長圖 關(guān)注 下載文章
綁定賬號成功
后續(xù)可登錄賬號暢享VIP特權(quán)!
如果VIP功能使用有故障,
可點(diǎn)擊這里聯(lián)系客服!

聯(lián)系客服