场景

记一次加解密算法优化过程。

实现

1.开始

后台查询出所有数据,并循环对所有的字段进行加密,

在前段循环对所有字段进行循环解密。

2.一次优化

后台只对关键字段进行加密,前段只对关键字段进行解密。

3.二次优化

后台对每条记录,即每个对象的关键字段进行循环取出,拼接成字符串,

然后再对所有对象进行加密。

4.三次优化

后台先循环取出所有的对象的所有字段,拼接成字符串,字段之间以及

对象之间用分隔符分隔,然后拼接成一个字符串,也就是在循环外调用加密算法。

然后前段实现一次解密,然后遍历赋值。

部分代码

单个关键字段进行加解密

后台:

data2 = this.service.queryAll(map);
String key =rsaService.getPublicKey();
String totalWithOutLine="";
for(orderComfirm oc:data2){
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
DateFormat format1 = new SimpleDateFormat("yyyy-MM-dd");
Date date = new java.sql.Date(format1.parse(sdf.format(oc.getDeliverDate())).getTime());
oc.setDeliverDate(date);
if(oc.getIsOnline()==1) {
oc.setTotalWithOutLine(oc.getTotalOnline());
oc.setTotalOnline(null);
}
if(oc.getIsOnline()==0) {
oc.setTotalWithOutLine(oc.getTotalNoOnline());
oc.setTotalNoOnline(null);
}


if(oc.getTotalWithOutLine()!=null) {
String totalWithOutLineStr=String.valueOf(oc.getTotalWithOutLine());
if(!"".equals(totalWithOutLine)) {
totalWithOutLine=totalWithOutLine+","+totalWithOutLineStr;
}else {
totalWithOutLine=totalWithOutLineStr;
}
}
oc.setTotalWithOutLine(null);
oc.setPrice(null);
oc.setState(oc.getStateEn());
oc.setTotal(null);
String week = DateConvert.getWeek(oc.getDeliverDate()).getChineseName();
week = "周"+week.substring(2);
oc.setWeek(week);
}

String totalWithOutLineEn=rsaService.encrypt(key,totalWithOutLine);

data2.get(0).setTotalEn(totalWithOutLineEn);

jsonResult.setData(data2);
jsonResult.setStatus(true);
jsonResult.setMessage("接口访问成功!");
LogService.getInstance(this).debug("获取列表成功!");
}catch(Exception ex) {
jsonResult.setStatus(false);
jsonResult.setMessage("接口访问失败");
LogService.getInstance(this).error("获取数据失败:" + ex.getMessage(), ex);
}
return jsonResult;
}

前端:

var list = data.data;

var totalWithOutLineStr = list[0].totalEn;
totalWithOutLineStr = RSADecrypt(totalWithOutLineStr);

var totalPriceArr=totalWithOutLineStr.split(",");
if(list.length>0){
$("#ok_order").css("display","block");
}
for(var i=0;i<list.length;i++){
list[i].totalWithOutLine=totalPriceArr[i];
if(list[i].deliverDate){
list[i].deliverDate=Covert(list[i].deliverDate);
}
//list[i].deliverDate = list[i].deliverDate.replace(/-/g,".");
if(list[i].total!=null){
list[i].total = commafy(list[i].total);
}

}

多个关键字段进行加解密

typeList = this.tbComfirmListService.getCates(map);
for(CateList cate : typeList){
Map<String,Object> map1 = new HashMap<>();
map1.put("time", orderId);
map1.put("caName", cate.getCaName());
if(code!=null&&!code.equals("")){
map1.put("oid", code);
}else{
map1.put("oid", code1);
}
if(online!=null&&!online.equals("")){
map1.put("online", "0");
}
List<ComfirmList> cst = tbComfirmListService.queryOrder(map1);

cate.setList(cst);
}
}
BigDecimal totalPrice=new BigDecimal("0.00");
for(CateList cl:typeList) {
totalPrice=totalPrice.add(cl.getPayment());
}

String cateEncry="";
String encryAfter ="";
for(CateList cl:typeList) {
cl.setTotalPrice(totalPrice);
if(!"".equals(cateEncry)) {
cateEncry=cateEncry+"="+totalPrice.toString();
}else {
cateEncry=totalPrice.toString();
}
cateEncry=cateEncry+"-"+cl.getPayment().toString();
cl.setPayment(null);
cl.setTotalPrice(null);
for(ComfirmList c:cl.getList()) {
//对num和totalFee进行拼接加密,以"."分割数量和价格,","分割每一个comfirmList对象
if(!"".equals(encryAfter)) {
encryAfter=encryAfter+"="+c.getNum().toString();
}else {
encryAfter=c.getNum().toString();
}
encryAfter=encryAfter+"-"+c.getPrice().toString();
c.setNum(null);
c.setPrice(null);
}
}
String result=cateEncry+";"+encryAfter;
cateEncry=rsaService.encrypt(key,result);
if(typeList.get(0)!=null) {
typeList.get(0).setTotalPriceEn(cateEncry);
}
jsonResult.setData(typeList);
jsonResult.setStatus(true);
jsonResult.setMessage("接口访问成功!");
LogService.getInstance(this).debug("获取列表成功!");

前端:

var list = data.data;
for(var i=0;i<list.length;i++){
if(list[i].payment!=null){
list[i].payment = commafy(list[i].payment);
}
list[i].totalPrice = commafy(list[i].totalPrice);
for(var j=0;j<list[i].list.length;j++){
list[i].list[j].price = commafy(list[i].list[j].price);
}
}