欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

DES 加密解密算法 java 版

程序员文章站 2024-03-14 15:08:10
...

前言:

工作需求:所有参数(数据库连接,服务器连接,都不能使用明文传参),所以需要一个加密解密算法,本来是想写 AES 的,但是 python 环境下各种改东西调试,好烦,所以退而求其次用 DES;
DES 算法写了 java 版和 python 版,这个是 java 版,python 可以在我的博客里找一下。python版

1.直接上代码
import javax.crypto.Cipher;
import javax.crypto.SecretKey;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.DESKeySpec;
import javax.crypto.spec.IvParameterSpec;


public class DesEnAndDe {

    public static String DES_CBC_Encrypt(String secretKey, String str) {
        try {
            byte[] keyBytes = secretKey.getBytes();
            byte[] content = str.getBytes();
            DESKeySpec keySpec = new DESKeySpec(keyBytes);
            SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");
            SecretKey key = keyFactory.generateSecret(keySpec);

            Cipher cipher = Cipher.getInstance("DES/CBC/PKCS5Padding");
            cipher.init(Cipher.ENCRYPT_MODE, key, new IvParameterSpec(keySpec.getKey()));
            byte[] result = cipher.doFinal(content);
            return byteToHexString(result);
        } catch (Exception e) {
            System.out.println("exception:" + e.toString());
        }
        return null;
    }

    public static String DES_CBC_Decrypt(String secretKey, String str) {
        try {
            byte[] keyBytes = secretKey.getBytes();
            byte[] content = hexToByteArray(str);
            DESKeySpec keySpec = new DESKeySpec(keyBytes);
            SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");
            SecretKey key = keyFactory.generateSecret(keySpec);

            Cipher cipher = Cipher.getInstance("DES/CBC/PKCS5Padding");
            cipher.init(Cipher.DECRYPT_MODE, key, new IvParameterSpec(keyBytes));
            byte[] result = cipher.doFinal(content);
            return new String(result);
        } catch (Exception e) {
            System.out.println("exception:" + e.toString());
        }
        return null;
    }

    private static String byteToHexString(byte[] bytes) {
        StringBuilder sb = new StringBuilder(bytes.length);
        String sTemp;
        for (byte aByte : bytes) {
            sTemp = Integer.toHexString(0xFF & aByte);
            if (sTemp.length() < 2)
                sb.append(0);
            sb.append(sTemp.toUpperCase());
        }
        return sb.toString();
    }

    private static byte[] hexToByteArray(String inHex) {
        int hexLen = inHex.length();
        byte[] result;
        if (hexLen % 2 == 1) {
            hexLen++;
            result = new byte[(hexLen / 2)];
            inHex = "0" + inHex;
        } else {
            result = new byte[(hexLen / 2)];
        }
        int j = 0;
        for (int i = 0; i < hexLen; i += 2) {
            result[j] = (byte) Integer.parseInt(inHex.substring(i, i + 2), 16);
            j++;
        }
        return result;
    }

    public static void main(String[] o) {
        String secretKey = "wo/**bhd"; // **
        String str = "xgsqrfyw1hr38*/-ff?"; // 加密内容
        String secret_pwd = DesEnAndDe.DES_CBC_Encrypt(secretKey, str);
        System.out.println("密文:" + secret_pwd);
        String clear_pwd = DesEnAndDe.DES_CBC_Decrypt(secretKey, secret_pwd);
        System.out.println("明文:" + clear_pwd);

    }

}

2.运行主函数结果
密文:E2EEA3378DF26770E7D0BED76FAB4B2AF9F9886134706D56
明文:xgsqrfyw1hr38*/-ff?
3.tips

DES 算法要求**的位数必须是 8 的整数倍,如果不是 8 的整数倍会报错。