java实现DES的加密解密

本文介绍如何使用Java实现DES加密解密算法。通过创建CryptTest类,文章详细讲解了密钥生成、加密及解密的过程,并提供了完整的代码示例。此外,还介绍了必要的进制转换方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

java实现DES的加密解密

创建一个类 CryptTest,在这个类里面些方法。

首先需要导入这个jar包

这里写图片描述

这里写图片描述

需要这个方法

/**
     * 将两个ASCII字符合成一个字节; 如:"EF"--> 0xEF
     * 
     * @param src0
     *            byte
     * @param src1
     *            byte
     * @return byte
     */
    public static byte uniteBytes(byte src0, byte src1) {
        byte _b0 = Byte.decode("0x" + new String(new byte[] { src0 }))
                .byteValue();
        _b0 = (byte) (_b0 << 4);
        byte _b1 = Byte.decode("0x" + new String(new byte[] { src1 }))
                .byteValue();
        byte ret = (byte) (_b0 ^ _b1);
        return ret;
    }

进制转换的方法

/**
     * 将二进制转化为16进制字符串
     * 
     * @param b
     *            二进制字节数组
     * @return String
     */
    public String byte2hex(byte[] b) {
        String hs = "";
        String stmp = "";
        for (int n = 0; n < b.length; n++) {
            stmp = (java.lang.Integer.toHexString(b[n] & 0XFF));
            if (stmp.length() == 1) {
                hs = hs + "0" + stmp;
            } else {
                hs = hs + stmp;
            }
        }
        return hs.toUpperCase();
    }

    /**
     * 十六进制字符串转化为2进制
     * 
     * @param hex
     * @return
     */
    public byte[] hex2byte(String hex) {
        byte[] ret = new byte[8];
        byte[] tmp = hex.getBytes();
        for (int i = 0; i < 8; i++) {
            ret[i] = uniteBytes(tmp[i * 2], tmp[i * 2 + 1]);
        }
        return ret;
    }

创建密钥

/**
     * 创建密匙
     * 
     * @param algorithm
     *            加密算法,可用 DES,DESede,Blowfish
     * @return SecretKey 秘密(对称)密钥
     */
    public SecretKey createSecretKey(String algorithm) {
        // 声明KeyGenerator对象
        KeyGenerator keygen;
        // 声明 密钥对象
        SecretKey deskey = null;
        try {
            // 返回生成指定算法的秘密密钥的 KeyGenerator 对象
            keygen = KeyGenerator.getInstance(algorithm);
            // 生成一个密钥
            deskey = keygen.generateKey();
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        }
        // 返回密匙
        return deskey;
    }

DES加密

/**
     * 根据密匙进行DES加密
     * 
     * @param key
     *            密匙
     * @param info
     *            要加密的信息
     * @return String 加密后的信息
     */
    public String encryptToDES(SecretKey key, String info) {
        // 定义 加密算法,可用 DES,DESede,Blowfish
        String Algorithm = "DES";
        // 加密随机数生成器 (RNG),(可以不写)
        SecureRandom sr = new SecureRandom();
        // 定义要生成的密文
        byte[] cipherByte = null;
        try {
            // 得到加密/解密器
            Cipher c1 = Cipher.getInstance(Algorithm);
            // 用指定的密钥和模式初始化Cipher对象
            // 参数:(ENCRYPT_MODE, DECRYPT_MODE, WRAP_MODE,UNWRAP_MODE)
            c1.init(Cipher.ENCRYPT_MODE, key, sr);
            // 对要加密的内容进行编码处理,
            cipherByte = c1.doFinal(info.getBytes());
        } catch (Exception e) {
            e.printStackTrace();
        }
        // 返回密文的十六进制形式
        return byte2hex(cipherByte);
    }

DES解密

/**
     * 根据密匙进行DES解密
     * 
     * @param key
     *            密匙
     * @param sInfo
     *            要解密的密文
     * @return String 返回解密后信息
     */
    public String decryptByDES(SecretKey key, String sInfo) {
        // 定义 加密算法,
        String Algorithm = "DES";
        // 加密随机数生成器 (RNG)
        SecureRandom sr = new SecureRandom();
        byte[] cipherByte = null;
        try {
            // 得到加密/解密器
            Cipher c1 = Cipher.getInstance(Algorithm);
            // 用指定的密钥和模式初始化Cipher对象
            c1.init(Cipher.DECRYPT_MODE, key, sr);
            // 对要解密的内容进行编码处理
            cipherByte = c1.doFinal(hex2byte(sInfo));
        } catch (Exception e) {
            e.printStackTrace();
        }
        // return byte2hex(cipherByte);
        return new String(cipherByte);
    }

测试方法 mian

/**
     * 测试
     * 
     * @param args
     */
    public static void main(String[] args) {
        // 生成一个DES算法的密匙
        SecretKey key = jiami.createSecretKey("DES");
        // 用密匙加密信息"Hello world!"
        String str1 = jiami.encryptToDES(key, "Hello");
        System.out.println("使用des加密信息Hello为:" + str1);
        // 使用这个密匙解密
        String str2 = jiami.decryptByDES(key, str1);
        System.out.println("解密后为:" + str2);
    }

运行效果

这里写图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值