Java DES加密解密数据 工具类

public class DesUtils {

    //DES加密和解密过程中,密钥长度都必须是8的倍数
    private static final String PASSWORD = "abcdefghijklmnopqrstuvwx";
    private static final String CHARSET = "UTF-8";
    
    /**
     * 
     * <pre>功能描述: 加密
     */
    private static byte[] encrypt(byte[] datasource) throws Exception {            
        SecureRandom random = new SecureRandom();
        DESKeySpec desKey = new DESKeySpec(PASSWORD.getBytes());
        //创建一个密匙工厂,然后用它把DESKeySpec转换成
        SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");
        SecretKey securekey = keyFactory.generateSecret(desKey);
        //Cipher对象实际完成加密操作
        Cipher cipher = Cipher.getInstance("DES");
        //用密匙初始化Cipher对象
        cipher.init(Cipher.ENCRYPT_MODE, securekey, random);
        //现在,获取数据并加密
        return cipher.doFinal(datasource);
    }
   
    /**
     * 
     * <pre>功能描述: 解密
     */
    private static byte[] decrypt(byte[] src) throws Exception {
        // DES算法要求有一个可信任的随机数源
        SecureRandom random = new SecureRandom();
        // 创建一个DESKeySpec对象
        DESKeySpec desKey = new DESKeySpec(PASSWORD.getBytes());
        // 创建一个密匙工厂
        SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");
        // 将DESKeySpec对象转换成SecretKey对象
        SecretKey securekey = keyFactory.generateSecret(desKey);
        // Cipher对象实际完成解密操作
        Cipher cipher = Cipher.getInstance("DES");
        // 用密匙初始化Cipher对象
        cipher.init(Cipher.DECRYPT_MODE, securekey, random);
        // 真正开始解密操作
        return cipher.doFinal(src);
    }
    
    /**
     * 
     * <pre>功能描述: 数据加密
     */
    public static String encrypt(String srcStr) throws Exception  {
        byte[] src = srcStr.getBytes(Charset.forName(CHARSET));
        byte[] buf = encrypt(src);
        return DesUtils.parseByte2HexStr(buf);
    }
    
    /**
     * 
     * <pre>功能描述: 数据解密
     */
    public static String decrypt(String hexStr) throws Exception {
        byte[] src = DesUtils.parseHexStr2Byte(hexStr);
        byte[] buf = decrypt(src);
        return new String(buf, Charset.forName(CHARSET));
    }
    
    
    /**
     * 
     * <pre>功能描述: 将二进制转换成16进制
     */
    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();
    }

   /**
    * 
    * <pre>功能描述: 将16进制转换为二进制
    */
    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;
    }
    
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
根据提供的引用内容,可以看出Java中提供了3DES解密工具类,同时支持ECB和CBC两种加密模式。如果需要使用3DESCBC解密工具类,可以按照以下步骤进行操作: 1. 导入相关的Java类库。 2. 创建3DESCBC解密工具类的实例。 3. 调用解密方法,传入密文和密钥等参数。 4. 获取解密后的明文数据。 下面是一个简单的示例代码: ``` import javax.crypto.Cipher; import javax.crypto.SecretKey; import javax.crypto.spec.IvParameterSpec; import javax.crypto.spec.SecretKeySpec; public class Java3DESCBCDecryptUtil { private static final String ALGORITHM = "DESede"; private static final String TRANSFORMATION = "DESede/CBC/PKCS5Padding"; private static final String CHARSET = "UTF-8"; public static String decrypt(String key, String iv, String encryptedData) throws Exception { byte[] keyBytes = key.getBytes(CHARSET); byte[] ivBytes = iv.getBytes(CHARSET); byte[] encryptedBytes = Base64.decodeBase64(encryptedData.getBytes(CHARSET)); SecretKey secretKey = new SecretKeySpec(keyBytes, ALGORITHM); IvParameterSpec ivParameterSpec = new IvParameterSpec(ivBytes); Cipher cipher = Cipher.getInstance(TRANSFORMATION); cipher.init(Cipher.DECRYPT_MODE, secretKey, ivParameterSpec); byte[] decryptedBytes = cipher.doFinal(encryptedBytes); return new String(decryptedBytes, CHARSET); } } ``` 在上面的代码中,我们使用了Java中的Cipher类来进行解密操作。同时,我们还需要提供密钥和初始化向量等参数。在实际使用中,我们可以根据具体的需求来修改代码中的参数。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值