JAVA实现DES加密

DES加密介绍

DES是一种对称加密算法,所谓对称加密算法即:加密和解密使用相同密钥的算法。DES加密算法出自IBM的研究,后来被美国政府正式采用,之后开始广泛流传,但是近些年使用越来越少,因为DES使用56位密钥,以现代计算能力,24小时内即可被破解。虽然如此,在某些简单应用中,我们还是可以使用DES加密算法,本文简单讲解DES的JAVA实现。

JAVA实现

package org.zpli.java8.safeapi;

/**
 * @Description: DES 加解密
 * @author: zpli
 * @Date: 2020/4/2 14:34
 */
import java.security.SecureRandom;

import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;
import javax.crypto.SecretKey;
import javax.crypto.spec.SecretKeySpec;

public class EncryptDES {
     

    private static final String KEY = "Hello Ketty";

    /**
     * DES 加密
     * @param content
     * @return
     */
    public static byte[] desEncrypt(String content) {
     
        try {
     
            KeyGenerator kGen = KeyGenerator.getInstance("DES");
            kGen.init(56, new SecureRandom(KEY.getBytes()));
            SecretKey secretKey = kGen.generateKey();
            byte[] enCodeFormat = secretKey.getEncoded();
            SecretKeySpec key = new SecretKeySpec(enCodeFormat, "DES");
            Cipher cipher = Cipher.getInstance("DES");// 创建密码器
            byte[] byteContent = content.getBytes("utf-8");
            cipher.init(Cipher.ENCRYPT_MODE, key);// 初始化
            byte[] result = cipher.doFinal(byteContent);
            return result; // 加密
        } catch (Throwable e) {
     
            e.printStackTrace();
        }
        return null;
    }

    /**
     * DES 解密
     * @param content
     * @return
     */
    public static byte[] desDecrypt(byte[] content) throws Exception {
     
        KeyGenerator kGen = KeyGenerator.getInstance("DES");
        kGen.init(56, new SecureRandom(KEY.getBytes()));
        SecretKey secretKey = kGen.generateKey();
        byte[] enCodeFormat = secretKey.getEncoded();
        SecretKeySpec key = new SecretKeySpec(enCodeFormat, "DES");
        Cipher cipher = Cipher.getInstance("DES"); // 创建密码器
        cipher.init(Cipher.DECRYPT_MODE, key); // 初始化
        byte[] result = cipher.doFinal(content); // 解密
        return result;
    }


    /**
     * 将二进制转换成16进制
     *
     * @param buf
     * @return
     */
    public static String parseByte2HexStr(byte buf[]) {
     
        StringBuffer sb = new StringBuffer();
        for (int i = 0; i < buf.length; i++) {
     
            String hex = Integer.toHexString(buf[i] & 0xFF);
            if (hex.length() == 1) {
     
                hex = '0' + hex;
            }
            sb.append(hex.toUpperCase());
        }
        return sb.toString();
    }

    /**
     * 将16进制转换为二进制
     *
     * @param hexStr
     * @return
     */
    public static byte[] parseHexStr2Byte(String hexStr) {
     
        if (hexStr.length() < 1)
            return null;
        byte[] result = new byte[hexStr.length() / 2];
        for (int i = 0; i < hexStr.length() / 2; i++) {
     
            int high = Integer.parseInt(hexStr.substring(i * 2, i * 2 + 1), 16);
            int low = Integer.parseInt(hexStr.substring(i * 2 + 1, i * 2 + 2), 16);
            result[i] = (byte) (high * 16 + low);
        }
        return result;
    }

    /**
     * 主函数
     */
    public static void main(String[] args) throws Exception {
     

        String content = "Hello World!";
        //加密
        System.out.println("加密前:" + content);
        byte[] encryptResult = desEncrypt(content);
        String result = parseByte2HexStr(encryptResult);
        System.out.println("加密后:" + result);
        //解密
        byte[] bytes = parseHexStr2Byte(result);
        byte[] decryptResult = desDecrypt(bytes);
        System.out.println("解密后:" + new String(decryptResult));
    }
}

输出结果

加密前:Hello World!
加密后:2480B5D4C5B18785F4433EAB7927162C
解密后:Hello World!

你可能感兴趣的:(Java,加密解密,密码学)