前端解密后台加密算法优化思想
生活随笔
收集整理的這篇文章主要介紹了
前端解密后台加密算法优化思想
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
場景
記一次加解密算法優化過程。
實現
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);}}?
總結
以上是生活随笔為你收集整理的前端解密后台加密算法优化思想的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: SSM+BJUI实现以Base64方式上
- 下一篇: Eclipse安装从dao层直接到map