DES加密

package com.spf.common.arithmetic;

import com.alibaba.fastjson.JSON;
import com.google.common.collect.Maps;
import javax.crypto.Cipher;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;
import java.util.Map;

/**
 * Created by SPF
 */
public class DES {
    private static byte[] iv = { 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h' };

    /**
     * 加密
     * @param encryptString
     * @param encryptKey
     * @return
     * @throws Exception
     */
    public static String encryptDESwithBase64(String encryptString, String encryptKey) throws Exception {
        return new String(org.apache.commons.codec.binary.Base64.encodeBase64(encryptDES(encryptString, encryptKey)), "UTF-8");
    }

    public static byte[] encryptDES(String encryptString, String encryptKey)throws Exception {
        IvParameterSpec zeroIv = new IvParameterSpec(iv);

        SecretKeySpec key = new SecretKeySpec(encryptKey.getBytes(), "DES");

        Cipher cipher = Cipher.getInstance("DES/CBC/PKCS5Padding");

        cipher.init(Cipher.ENCRYPT_MODE, key, zeroIv);

        byte[] encryptedData = cipher.doFinal(encryptString.getBytes("UTF-8"));

        return encryptedData;

    }

    /**
     * 解密
     * @param encryptedString
     * @param decryptKey
     * @return
     */
    public static String decryptDESwithBase64(String encryptedString, String decryptKey) {
        String result = null;
        try {
            byte[] encryptedData = org.apache.commons.codec.binary.Base64.encodeBase64(encryptedString.getBytes());
            result = decryptDES(encryptedData, decryptKey);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;
    }

    public static String decryptDES(byte[] encryptedData, String decryptKey) {
        String decryptedString = null;
        try {
            IvParameterSpec zeroIv = new IvParameterSpec(iv);
            SecretKeySpec key = new SecretKeySpec(decryptKey.getBytes("UTF-8"), "DES");

            Cipher cipher = Cipher.getInstance("DES/CBC/PKCS5Padding");

            cipher.init(Cipher.DECRYPT_MODE, key, zeroIv);

            byte decryptedData[] = cipher.doFinal(encryptedData);

            decryptedString = new String(decryptedData, "UTF-8");
        } catch (Exception e) {
            e.printStackTrace();
        }

        return decryptedString;

    }

    /**
     * 测试方法
     * @param args
     */
    public static void main(String[] args) throws Exception {
        Map<String,String> requestMap = Maps.newHashMap();
        requestMap.put("order_no","556561561655");
        requestMap.put("order_status","2000");
        requestMap.put("member_id","12345");
        requestMap.put("trade_no","12359654782");
        requestMap.put("total_fee","12155");
        String str = DES.encryptDESwithBase64(JSON.toJSONString(requestMap),"12345678");
        System.out.println(str);
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值