Java工具集-DES加密

代碼示例


import com.simple.util.base.ByteUtil;

import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;
import java.security.Key;
import java.security.SecureRandom;

/**
 * @program: simple_tools
 * @description: DES加密
 * @author: Mr.chen
 * @create: 2020-06-08 16:07
 **/
public class DESEncrypt {
    private static String Algorithm = "DES";

    /**
     * 加密以byte[]明文輸入,byte[]密文輸出
     *
     * @param byteS
     * @param key
     * @return
     */
    private static byte[] getEncCode(byte[] byteS, Key key) {
        byte[] byteFina = null;
        Cipher cipher;
        try {
            cipher = Cipher.getInstance(Algorithm);
            cipher.init(Cipher.ENCRYPT_MODE, key);
            byteFina = cipher.doFinal(byteS);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            cipher = null;
        }
        return byteFina;
    }

    /**
     * 解密以byte[]密文輸入,以byte[]明文輸出
     *
     * @param byteD
     * @return
     */
    private static byte[] getDesCode(byte[] byteD, Key key) {
        Cipher cipher;
        byte[] byteFina = null;
        try {
            cipher = Cipher.getInstance(Algorithm);
            cipher.init(Cipher.DECRYPT_MODE, key);
            byteFina = cipher.doFinal(byteD);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            cipher = null;
        }
        return byteFina;
    }

    /**
     * 加密String明文輸入,String密文輸出
     *
     * @param strEnc
     * @param key
     * @return
     */
    public static String encrypt(String strEnc, Key key) {
        String strMi = "";
        try {
            return ByteUtil.byteArrayToHexString(getEncCode(strEnc.getBytes(), key));
        } catch (Exception e) {
            e.printStackTrace();
        }
        return strMi;
    }

    /**
     * 解密以String密文輸入,String明文輸出
     *
     * @param strEnc
     * @param key
     * @return
     */
    public static String dencrypt(String strEnc, Key key) {
        String strMing = "";
        try {
            return new String(getDesCode(ByteUtil.fromHexString(strEnc), key));
        } catch (Exception e) {
            e.printStackTrace();
        }
        return strMing;
    }

    /**
     * 根據參數生成KEY
     *
     * @param strKey
     * @return
     */
    public static Key getKey(String strKey) {
        Key key = null;
        try {
            KeyGenerator _generator = KeyGenerator.getInstance(Algorithm);
            _generator.init(new SecureRandom(strKey.getBytes()));
            key = _generator.generateKey();
            _generator = null;

        } catch (Exception ex) {
            ex.printStackTrace();
        }
        return key;
    }

    public static void main(String[] args) {
        System.out.println("hello");
        Key key = DESEncrypt.getKey("secureKey");// 生成密匙
        String strEnc = DESEncrypt.encrypt("123456:123456:asddffgghhjjkkkhkhkhkjhkhkhkhkhkhkh:201265656565", key);// 加密字符串,返回String的密文
        System.out.println(strEnc);
        Key key2 = DESEncrypt.getKey("secureKey");// 生成密匙
        String strDes = DESEncrypt.dencrypt(strEnc, key2);// 把String類型的密文解密
        System.out.println(strDes);
    }
}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章