本文主要是介绍使用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
这篇关于使用3DES-ECB 加解密算法对JSON数据进行加密的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!