JAVA DES/ECB/PKCS7Padding 加解密算法

JAVA DES/ECB/PKCS7Padding 加解密算法

直接上代码

package com.api.hbBussiness;

/**
 * 加解密
 */

public class DesUtil  {
    
    private static Map<String, String> secretkeyMap = new HashMap();
  
    /**
     * 加密/解密算法-工作模式-填充模式
     */
    private static final String CIPHER_ALGORITHM = "DES/ECB/PKCS7Padding";
   
    
    static {
        Security.addProvider(new org.bouncycastle.jce.provider.BouncyCastleProvider());
  
    }
    
    
    /*
     * 生成key
     *
     * @param password
     * @return
     * @throws Exception
     */
    //获取加密或解密的Cipher对象:负责完成加密或解密工作
    private static Cipher GetCipher(int opmode, String key) {
        try {
            //根据传入的秘钥内容生成符合DES加密解密格式的秘钥内容
            DESKeySpec dks = new DESKeySpec(key.getBytes());
            //获取DES秘钥生成器对象
            SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");
            // 生成秘钥:key的长度不能够小于8位字节
            Key secretKey = keyFactory.generateSecret(dks);
            //获取DES/ECB/PKCS7Padding该种级别的加解密对象
            Cipher cipher = Cipher.getInstance(CIPHER_ALGORITHM);
            //初始化加解密对象【opmode:确定是加密还是解密模式;secretKey是加密解密所用秘钥】
            cipher.init(opmode, secretKey);
            return cipher;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }
    
    /**
     * DES算法,加密
     *
     * @param data 待加密字符串
     * @param key  加密私钥,长度不能够小于8位
     * @return 加密后的字节数组,一般结合Base64编码使用
     * @throws Exception
     */
    public static String encode(String data, String key) {
        if (data == null || data.isEmpty())
            return null;
        try {
            //获取加密对象【Cipher.ENCRYPT_MODE:指定加密模式为1】
            Cipher cipher = GetCipher(Cipher.ENCRYPT_MODE, key);
            if (cipher == null) {
                return null;
            } else {
                //设置加密的字符串为utf-8模式并且加密,返回加密后的byte数组。
                byte[] byteHex = cipher.doFinal(data.getBytes("UTF-8"));
                return byteToHexString(byteHex);//对加密后的数组进制转换
            }
        } catch (Exception e) {
            e.printStackTrace();
            return data;
        }
    }
    
    /**
     * DES算法,解密
     *
     * @param data 待解密字符串
     * @param key  解密私钥,长度不能够小于8位
     * @return 解密后的字节数组
     * @throws Exception
     */
    public static String decode(String data, String key) throws Exception {
        if (data == null || data.isEmpty()) {
            return null;
        }
        try {
            //先把待解密的字符串转成Char数组类型,然后进行进制转换。
            byte[] b = hex2byte(data);
            //获取解密对象【Cipher.DECRYPT_MODE:指定解密模式为2】
            Cipher cipher = GetCipher(Cipher.DECRYPT_MODE, key);
            if (cipher != null) {
                //进行解密返回utf-8类型的字符串
                return new String(cipher.doFinal(b), "UTF-8");
            } else {
                return null;
            }
        } catch (Exception e) {
            e.printStackTrace();
            return data;
        }
    }
    
    public static String byteToHexString(byte[] bytes) {
        StringBuffer sb = new StringBuffer(bytes.length);
        String sTemp;
        for (int i = 0; i < bytes.length; i++) {
            sTemp = Integer.toHexString(0xFF & bytes[i]);
            if (sTemp.length() < 2) {
                sb.append(0);
            }
            sb.append(sTemp.toUpperCase());
        }
        return sb.toString();
    }
    
    public static String encode(byte[] src) {
        String strHex = "";
        StringBuilder sb = new StringBuilder("");
        for (int n = 0; n < src.length; n++) {
            strHex = Integer.toHexString(src[n] & 0xFF);
            sb.append((strHex.length() == 1) ? "0" + strHex : strHex); // 每个字节由两个字符表示,位数不够,高位补0
        }
        return sb.toString().trim();
    }
    
    public static byte[] hex2byte(String hex) throws IllegalArgumentException {
        if (hex.length() % 2 != 0) {
            throw new IllegalArgumentException("invalid hex string");
        }
        char[] arr = hex.toCharArray();
        byte[] b = new byte[hex.length() / 2];
        for (int i = 0, j = 0, l = hex.length(); i < l; i++, j++) {
            String swap = "" + arr[i++] + arr[i];
            int byteint = Integer.parseInt(swap, 16) & 0xFF;
            b[j] = new Integer(byteint).byteValue();
        }
        return b;
    }
 }

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值