java 3des 24位密钥 加解密

3 篇文章 0 订阅
2 篇文章 0 订阅
这是一个Java实现的3DES加密和解密的示例代码。通过`SecretKeySpec`和`Cipher`类进行加密和解密操作,使用了ECB模式和PKCS5Padding填充方式。代码中定义了3DES的加密和解密方法,并提供了测试用例。
摘要由CSDN通过智能技术生成

package com.test;

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

public class des3_24 {

    /**
     * @throws Exception 
     * @功能:测试用例
     * @参数: 参数
     */
    public static void main(String[] args) throws Exception {
        String key = "123456789000000000000000";
        String str = "{}";
        System.out.println("随机key-----------> " + key);
        String encrypt3DES = encrypt3DES(str,key);
        System.out.println("加密-----------> " + encrypt3DES);
        String decrypt3DES = decrypt3DES(encrypt3DES,key);
        System.out.println("解密-----------> " + decrypt3DES);
 
        
    }
    /**
     * 3DES加密
     *
     * @param data
     * @return
     * @throws Exception
     */
    public static String encrypt3DES(String data,String key) throws Exception {
        //加密
        byte key_byte[] = key.getBytes();
        SecretKey secretKey = new SecretKeySpec(key_byte, "DESede");
        Cipher cipher = Cipher.getInstance("DESede/ECB/PKCS5Padding");
        cipher.init(Cipher.ENCRYPT_MODE, secretKey);
        byte[] bytes = cipher.doFinal(data.getBytes("utf-8"));
        String str =bytesToHexString(bytes);
        return str;
    }
   
 
    /**
     * 3DES解密
     *
     * @param data
     * @return
     * @throws Exception
     */
    public static String decrypt3DES(String data,String key) throws Exception {
        //解密
        byte key_byte[] = key.getBytes();
        SecretKey secretKey = new SecretKeySpec(key_byte, "DESede");
        Cipher cipher = Cipher.getInstance("DESede/ECB/PKCS5Padding");
        cipher.init(Cipher.DECRYPT_MODE, secretKey);
        byte[] bytes = cipher.doFinal(hexStringToBytes(data));
        return new String(bytes, "UTF-8");
    }
    public static String bytesToHexString(byte[] bytes) {
        StringBuilder stringBuilder = new StringBuilder();
        if (bytes != null && bytes.length > 0) {
            byte[] arg1 = bytes;
            int arg2 = bytes.length;

            for (int arg3 = 0; arg3 < arg2; ++arg3) {
                byte b = arg1[arg3];
                int v = b & 255;
                String hv = Integer.toHexString(v);
                if (hv.length() < 2) {
                    stringBuilder.append(0);
                }

                stringBuilder.append(hv);
            }

            return stringBuilder.toString().toUpperCase();
        } else {
            return "";
        }
    }
    public static byte[] hexStringToBytes(String hexString) {
        if (hexString != null && !"".equals(hexString)) {
            hexString = hexString.toUpperCase();
            int length = hexString.length() / 2;
            char[] hexChars = hexString.toCharArray();
            byte[] d = new byte[length];

            for (int i = 0; i < length; ++i) {
                int pos = i * 2;
                d[i] = (byte) (charToByte(hexChars[pos]) << 4 | charToByte(hexChars[pos + 1]));
            }

            return d;
        } else {
            return null;
        }
    }

    private static byte charToByte(char c) {
        return (byte) "0123456789ABCDEF".indexOf(c);
    }
     
}
 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值