JS实现前端密码加密,后台解密

JS实现前端密码加密,后台解密,第1张

JS加密方法
//登录按钮进行输入明文密码加密    
$("#login").on("click",function(){
    var passWord=$("#passWord").val();
    var eles = [];
    eles.push(passWord);
    var esz = encode("0x12",eles);    
    $("#passWord").val(esz);
})
 
//具体加密逻辑
function encode(key, eles) {
    var keyVal = parseInt(key)
      var arr = []
      for(var i = 0; i < eles.length; ++i) {
          var ele = eles[i]
          arr.push(ele.length ^ keyVal)
          for(var j = 0; j < ele.length; ++j) {
              arr.push(ele[j].charCodeAt(0) ^ keyVal)
          }
      }
      arr.push(keyVal)
      var context = "CV16" + arr.join("%")
      return context;
}

java解密工具类
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Arrays;

public class PwdUtils {

    public static String apply(String code) {
        try {
            Map<Integer, String> context = new HashMap<>();
            List<String> partList = new ArrayList<>(Arrays.asList(code.substring(4).split("%")));
            for (int index = 0, key = Integer.parseInt(partList.remove(partList.size() - 1)); read(index, key, partList, context); ++index)
                ;
            return context.get(0);
        } catch (Exception e) {
            e.printStackTrace();
            return "";
        }
    }

    private static boolean read(int index, int key, List<String> partList, Map<Integer, String> context) {
        if (partList.isEmpty() ) return false;
        StringBuilder sb = new StringBuilder();
        for (int i = 0, length = nextElement(key, partList); i < length; ++i) {
            sb.append(Character.toChars(nextElement(key, partList)));
        }
        context.put(index, sb.toString());
        return true;
    }

    private static int nextElement(int key, List<String> partList) {
        return Integer.parseInt(partList.remove(0)) ^ key;
    }
}


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/langs/790960.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-05
下一篇 2022-05-05

发表评论

登录后才能评论

评论列表(0条)

保存