前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Java工具集-DES加密工具

Java工具集-DES加密工具

作者头像
cwl_java
发布2020-06-10 10:14:24
2.5K0
发布2020-06-10 10:14:24
举报
文章被收录于专栏:cwl_Java
代码示例
代码语言:javascript
复制
import com.simple.util.base.ByteUtil;
import com.simple.util.base.StringUtil;

import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;
import javax.crypto.SecretKey;
import javax.crypto.spec.SecretKeySpec;
import java.security.NoSuchAlgorithmException;
import java.security.SecureRandom;
import java.security.Security;

/**
 * @program: simple_tools
 * @description: DES加密
 * @author: Mr.chen
 * @create: 2020-06-08 16:04
 **/
public class DESedeEncrypt {

    private static String Algorithm = "DES";
    private static String Algorithm_mode = "DES/ECB/NOPADDING";

    static {
        Security.addProvider(new com.sun.crypto.provider.SunJCE());
    }

    /**
     * 加密
     *
     * @param input
     * @param keyStr
     * @return
     * @throws Exception
     */
    public static String encrypt(String input, String keyStr) throws Exception {
        byte[] inputBytes = StringUtil.fillStr(input, 8).getBytes();
        byte[] keyBytes = ByteUtil.fromHexString(keyStr);
        Cipher cipher = Cipher.getInstance(Algorithm_mode); // TripleDES/ECB/NoPadding
        SecretKeySpec key = new SecretKeySpec(keyBytes, Algorithm);
        cipher.init(Cipher.ENCRYPT_MODE, key);
        byte[] code = cipher.doFinal(inputBytes);
        return ByteUtil.byteArrayToHexString(code);
    }

    /**
     * 加密
     *
     * @param input
     * @param keyBytes
     * @return
     * @throws Exception
     */
    public static String encrypt(String input, byte[] keyBytes) throws Exception {
        byte[] inputBytes = StringUtil.fillStr(input, 8).getBytes();
        Cipher cipher = Cipher.getInstance(Algorithm_mode); // TripleDES/ECB/NoPadding
        SecretKeySpec key = new SecretKeySpec(keyBytes, Algorithm);
        cipher.init(Cipher.ENCRYPT_MODE, key);
        byte[] code = cipher.doFinal(inputBytes);
        return ByteUtil.byteArrayToHexString(code);
    }

    /**
     * 加密
     *
     * @param inputBytes
     * @param keyBytes
     * @return
     * @throws Exception
     */
    public static String encrypt(byte[] inputBytes, byte[] keyBytes) throws Exception {
        Cipher cipher = Cipher.getInstance(Algorithm_mode); // TripleDES/ECB/NoPadding
        SecretKeySpec key = new SecretKeySpec(keyBytes, Algorithm);
        cipher.init(Cipher.ENCRYPT_MODE, key);
        byte[] code = cipher.doFinal(inputBytes);
        return ByteUtil.byteArrayToHexString(code);
    }

    /**
     * 解密
     *
     * @param input
     * @param keyBytes
     * @return
     * @throws Exception
     */
    public static String decrypt(String input, byte[] keyBytes) throws Exception {
        Cipher cipher = Cipher.getInstance(Algorithm_mode);
        SecretKeySpec key = new SecretKeySpec(keyBytes, Algorithm);
        cipher.init(Cipher.DECRYPT_MODE, key);
        byte[] code = cipher.doFinal(ByteUtil.fromHexString(input));
        return new String(code).trim();
    }

    /**
     * 解密
     *
     * @param input
     * @param keyStr
     * @return
     * @throws Exception
     */
    public static String decrypt(String input, String keyStr) throws Exception {
        Cipher cipher = Cipher.getInstance(Algorithm_mode);
        byte[] keyBytes = ByteUtil.fromHexString(keyStr);
        SecretKeySpec key = new SecretKeySpec(keyBytes, Algorithm);
        cipher.init(Cipher.DECRYPT_MODE, key);
        byte[] code = cipher.doFinal(ByteUtil.fromHexString(input));
        return new String(code).trim();
    }

    /**
     * 解密
     *
     * @param inputBytes
     * @param keyBytes
     * @return
     * @throws Exception
     */
    public static String decrypt(byte[] inputBytes, byte[] keyBytes) throws Exception {
        Cipher cipher = Cipher.getInstance(Algorithm_mode);
        SecretKeySpec key = new SecretKeySpec(keyBytes, Algorithm);
        cipher.init(Cipher.DECRYPT_MODE, key);
        byte[] code = cipher.doFinal(inputBytes);
        return ByteUtil.byte2Str(code).trim();
    }

    /**
     * 获取随机密钥串
     *
     * @return
     * @throws NoSuchAlgorithmException
     */
    public static String getKey() throws NoSuchAlgorithmException {
        SecureRandom sr = new SecureRandom();
        // 为我们选择的DES算法生成一个KeyGenerator对象
        KeyGenerator kg = KeyGenerator.getInstance(Algorithm);
        kg.init(sr);
        // 生成密钥
        SecretKey key = kg.generateKey();
        return ByteUtil.byteArrayToHexString(key.getEncoded());
    }

    /**
     * 根据参数生成KEY
     *
     * @param strKey
     * @return
     * @throws NoSuchAlgorithmException
     */
    public static String getKey(String strKey) throws NoSuchAlgorithmException {
        SecureRandom sr = new SecureRandom(strKey.getBytes());
        // 为我们选择的DES算法生成一个KeyGenerator对象
        KeyGenerator kg = KeyGenerator.getInstance(Algorithm);
        kg.init(sr);
        // 生成密钥
        SecretKey key = kg.generateKey();
        return ByteUtil.byteArrayToHexString(key.getEncoded());
    }

    public static void main(String[] args) {
        String input = "sap123456";
        String key = "secureKey";
        try {
            String keyStr = DESedeEncrypt.getKey(key);
            // System.out.println(keyStr);
            // String codeStr = DESedeEncrypt.encrypt(input, keyStr);
            // System.out.println(codeStr);
            String decode = DESedeEncrypt.decrypt("O6XEOFJqZjE5svZfsWjsYg==", keyStr);
            System.out.println(decode);
        } catch (Exception e) {
            // TODO Auto-generated catch block
        }
    }
}
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2020/06/08 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 代码示例
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档