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;
}
}
|