使用3DES-ECB 加解密算法对JSON数据进行加密

测试代码如下, 相关jar包在链接中

public class RandCipherTest {

    public static void main(String[] args) throws Exception {
        //Step1: 组装请求报文
        JSONObject json = new JSONObject(); 
        json.put("fromCompany", "阿拉警察");
        json.put("userName", "宋学利");
        json.put("idCardNo", "342101198010307646");
        json.put("requestTime", RandCipherUtils.getTimestamp());
        
        String source = "阿拉警察";        // 渠道平台名称,如:e乡北仑、阿拉警察、浙江省居住证等
        String data = json.toString();            // 要加密的数据,一般为请求报文中的body部分
        
        //Step2: 请求报文加密
        RandCipherData response = RandCipherUtils.encrypt(source, data);
        String requestStr = getRequestStr(response);
        System.out.println("加密字符串 " + requestStr);
        
        
        //字符串转化为json对象
        JSONObject returnJson = JSONObject.parseObject(requestStr);
        
        //Step4: 解密
        // 得到索引和密文
        String header = returnJson.get("header").toString();
        JSONObject headerObj = JSONObject.parseObject(header);
        String keyIndex = headerObj.get("keyIndex").toString();
        int key = Integer.parseInt(keyIndex);
        String bodyStr = returnJson.get("body").toString();
        
        // 解密
        RandCipherData response2 = RandCipherUtils.decrypt(source, key, bodyStr);
        //System.out.println("response: " + response);
        System.out.println("解密字符串 " + response2.getData());
    }
    
    
    /**
     * 获取请求字符串
     * @param response
     * @return
     */
    private static String getRequestStr(RandCipherData response) {
        String requestStr = "{\"header\":{\"keyIndex\":\"" + response.getIndex() + "\"}, \"body\":\"" + response.getDataEnc() + "\"}";
        return requestStr;
    }

}
 

 

 

文件jar包下载地址      https://download.csdn.net/download/u013019878/10545861

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值