加密算法 - DES

1 篇文章 0 订阅

加密算法 - DES

/**
 * DES
 * @author
 */
@Slf4j
@Component
public class DESUtil {

    private static final String ALGORITHM = "DESede";
    
    public static String key;

    // 配置文件,配置密钥,也可以写死一个 String key = "";
    @Autowired
    private CustomPropertiesConfig systemPropertiesConfig;

    @PostConstruct
    public void init() {
        // 从配置文件读取,区分不同环境
        key = systemPropertiesConfig.getDesKey();
    }

    private DESUtil() {

    }

    /**
     * 生成密钥
     * @param key
     * @return
     */
    public static String generateKey(String key) {
        SecretKey secretKey = generateSecretKey(key);
        return new String(secretKey.getEncoded());
    }

/**
     * 生成密钥
     * @param key
     * @return
     */
    private static SecretKey generateSecretKey(String key) {
        SecretKey secretKey = null;
        try {
            MessageDigest md = MessageDigest.getInstance("MD5");
            byte[] bytes = key.getBytes();
            md.update(bytes, 0, bytes.length);
            // Generate 16 bytes whenever
            byte[] mdBytes = md.digest();
            // Generate 24 bytes for DESedeKeySpec
            byte[] encodedBytes = Base64.encodeBase64(mdBytes);
            DESedeKeySpec keySpec = new DESedeKeySpec(encodedBytes);
            SecretKeyFactory keyFactory = SecretKeyFactory.getInstance(ALGORITHM);
            secretKey = keyFactory.generateSecret(keySpec);
        } catch (Exception e) {
            e.printStackTrace();
        }

        return secretKey;
    }

    /**
     * 3DES加密
     * @param str    加密的字符串
     * @return
     */
    public static String encode3Des(String str) throws Exception{
        byte[] bytes = toHexByte(key);
        byte[] strBytes = str.getBytes();
        try {
            //生成密钥
            SecretKey secretKey = new SecretKeySpec(bytes, ALGORITHM);
            //加密
            Cipher c1 = Cipher.getInstance(ALGORITHM);
            c1.init(Cipher.ENCRYPT_MODE, secretKey);

            return Base64.encodeBase64String(c1.doFinal(strBytes));
        } catch (NoSuchAlgorithmException e1) {
            log.error("encode3Des NoSuchAlgorithmException error:{}", e1.getMessage());
            throw new Exception(-1, "加密时,没有找到指定的加密算法");
        } catch(Exception e3) {
            log.error("encode3Des error:{}", e3.getMessage());
            throw new Exception(-1, "数据加密异常");
        }
    }

    /**
     * 解密
     * @param desStr
     * @return
     */
    public static String decode3Des(String desStr) throws Exception{
        Base64 base64 = new Base64();
        byte[] bytes = toHexByte(key);
        byte[] desStrBytes = base64.decode(desStr);
        try {
            //生成密钥
            SecretKey secretKey = new SecretKeySpec(bytes, ALGORITHM);
            //解密
            Cipher c1 = Cipher.getInstance(ALGORITHM);
            c1.init(Cipher.DECRYPT_MODE, secretKey);
            return new String(c1.doFinal(desStrBytes));
        } catch (NoSuchAlgorithmException e1) {
            log.error("decode3Des NoSuchAlgorithmException error:{}", e1.getMessage());
            throw new Exception(-1, "解密时,没有找到指定的加密算法");
        } catch(Exception e3) {
            log.error("decode3Des error:{}", e3.getMessage());
            throw new Exception(-1, "数据解密异常");
        }
    }

    /**
     *
     * @param key
     * @return
     */
    public static byte[] toHexByte(String key) {
        String f = DigestUtils.md5Hex(key);
        byte[] keysByte = f.getBytes();
        byte[] result = new byte[24];
        for (int i=0; i < 24; i++){
            result[i] = keysByte[i];
        }

        return result;
    }

密钥生成方法,仅供参考,自定义key都行

public static void main(String[] args) {
        String key = UUID.randomUUID().toString();
        System.out.println(generateKey(key));
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值