DES加密解密工具类

时间:2021-05-23 20:06:07

转载请注明:https://blog.csdn.net/qfashly/article/details/79815134

public class DESUtil {
    private Key key;
    private static final String DES = "DES";
    public static final String[] KEY_ARR = { "IeJSB9d1lI02fswfds2", "zafZ6khrle5W8hATjKBAqQ", "so7uFYz4n0yk3faYxVHhiRB" };

    /** * <p>Title: 密匙</p> * <p>Description: </p> * @param str */
    public DESUtil(String str) {
        setKey(str); // 生成密匙
    }

    public Key getKey() {
        return key;
    }

    public void setKey(Key key) {
        this.key = key;
    }

    /** * * @Title: setKey * @Description: 根据参数生成 KEY * @param strKey * @author chenxy * @date 2018-4-4 上午11:14:46 */
    public void setKey(String strKey) {
        try {
            KeyGenerator generator = KeyGenerator.getInstance(DES);
            generator.init(new SecureRandom(strKey.getBytes()));
            this.key = generator.generateKey();
            generator = null;
        } catch (Exception e) {
            throw new RuntimeException("Error initializing SqlMap class. Cause: " + e);
        }
    }

    /** * @Title: encryptStr * @Description: 加密 String, 明文输入, String密文输出 * @param strMing * @return * @author chenxy * @date 2018-4-4 上午11:16:04 */
    public String encryptStr(String strMing) {
        byte[] byteMi = null;
        byte[] byteMing = null;
        String strMi = "";
        BASE64Encoder base64en = new BASE64Encoder();
        try {
            byteMing = strMing.getBytes("UTF8");
            byteMi = this.encryptByte(byteMing);
            strMi = base64en.encode(byteMi);
        } catch (Exception e) {
            throw new RuntimeException(
                    "Error initializing SqlMap class. Cause: " + e);
        } finally {
            base64en = null;
            byteMing = null;
            byteMi = null;
        }
        return strMi;
    }

    /** * @Title: decryptStr * @Description: 解密, 以String密文输入, String明文输出 * @param strMi * @return * @author chenxy * @date 2018-4-4 上午11:16:32 */
    public String decryptStr(String strMi) {
        BASE64Decoder base64De = new BASE64Decoder();
        byte[] byteMing = null;
        byte[] byteMi = null;
        String strMing = "";
        try {
            byteMi = base64De.decodeBuffer(strMi);
            byteMing = this.decryptByte(byteMi);
            strMing = new String(byteMing, "UTF8");
        } catch (Exception e) {
            throw new RuntimeException("Error initializing SqlMap class. Cause: " + e);
        } finally {
            base64De = null;
            byteMing = null;
            byteMi = null;
        }
        return strMing;
    }

    /** * @Title: encryptByte * @Description: 加密, 以byte[]明文输入, byte[]密文输出 * @param byteS * @return * @author chenxy * @date 2018-4-4 上午11:17:08 */
    private byte[] encryptByte(byte[] byteS) {
        byte[] byteFina = null;
        Cipher cipher;
        try {
            cipher = Cipher.getInstance(DES);
            cipher.init(Cipher.ENCRYPT_MODE, key);
            byteFina = cipher.doFinal(byteS);
        } catch (Exception e) {
            throw new RuntimeException("Error initializing SqlMap class. Cause: " + e);
        } finally {
            cipher = null;
        }
        return byteFina;
    }

    /** * * @Title: decryptByte * @Description: 解密, 以byte[]密文输入, 以 byte[] 明文输出 * @param byteD * @return * @author chenxy * @date 2018-4-4 上午11:17:39 */
    private byte[] decryptByte(byte[] byteD) {
        Cipher cipher;
        byte[] byteFina = null;
        try {
            cipher = Cipher.getInstance(DES);
            cipher.init(Cipher.DECRYPT_MODE, key);
            byteFina = cipher.doFinal(byteD);
        } catch (Exception e) {
            throw new RuntimeException("Error initializing SqlMap class. Cause: " + e);
        } finally {
            cipher = null;
        }
        return byteFina;
    }

    /** * * @Title: encryptFile * @Description: 文件file进行加密并保存目标文件destFile中 * @param file 要加密的文件 * @param destFile 加密后存放的文件名 * @throws Exception * @author chenxy * @date 2018-4-4 上午11:18:05 */
    public void encryptFile(String file, String destFile) throws Exception {
        Cipher cipher = Cipher.getInstance(DES);
        cipher.init(Cipher.ENCRYPT_MODE, this.key);
        InputStream is = new FileInputStream(file);
        OutputStream out = new FileOutputStream(destFile);
        CipherInputStream cis = new CipherInputStream(is, cipher);
        byte[] buffer = new byte[1024];
        int r;
        while ((r = cis.read(buffer)) > 0) {
            out.write(buffer, 0, r);
        }
        cis.close();
        is.close();
        out.close();
    }

    /** * * @Title: decryptFile * @Description: 文件采用DES算法解密文件 * @param file 已加密的文件 * @param dest 解密后存放的文件 * @throws Exception * @author chenxy * @date 2018-4-4 上午11:19:00 */
    public void decryptFile(String file, String dest) throws Exception {
        Cipher cipher = Cipher.getInstance(DES);
        cipher.init(Cipher.DECRYPT_MODE, this.key);
        InputStream is = new FileInputStream(file);
        OutputStream out = new FileOutputStream(dest);
        CipherOutputStream cos = new CipherOutputStream(out, cipher);
        byte[] buffer = new byte[1024];
        int r;
        while ((r = is.read(buffer)) >= 0) {
            cos.write(buffer, 0, r);
        }
        cos.close();
        out.close();
        is.close();
    }

    public static void main(String[] args) throws Exception {
        DESUtil des = new DESUtil(DESUtil.KEY_ARR[0]);
        // DES 待加密字符串
        String encryStr1 = "java是应用最广泛的编程语言";
        // DES 加密后字符串
        String decryStr1 = des.encryptStr(encryStr1);
        System.out.println("待加密字符串: " + encryStr1);
        System.out.println("加密后字符串: " + decryStr1);
        System.out.println("----------------------------");
        // DES 待解密字符串
        String decryStr2 = "dy0DrwjwlWMg0PwTrx0alcnAGo6gy7XIE6nqslMpf3sSgFp20pL/mg==";
        // DES 解密后字符串
        String encryStr2 = des.decryptStr(decryStr2);
        System.out.println("待解密字符串: " + decryStr2);
        System.out.println("解密后字符串: " + encryStr2);
    }
}

结果输出:
待加密字符串: java是应用最广泛的编程语言
加密后字符串: PYeeAdRgSJAsX4BNOOQWbgHmLn4h9aKr7Dz3OP9v+uA2309/GY4oCQ==
待解密字符串: dy0DrwjwlWMg0PwTrx0alcnAGo6gy7XIE6nqslMpf3sSgFp20pL/mg==
解密后字符串: PHP是世界上最好的编程语言