package com.ks.tools; import java.util.ArrayList; import java.util.Arrays; import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.regex.Matcher; import java.util.regex.Pattern; /** * @author 李英夫 * @ClassName AntiCopy * @Version * @ModifiedBy * @Copyright @ 2010 H&L Technology Inc. * @date 2010-6-3 上午08:39:47 * @description */ public class AntiCopy { private static int A = 62; private static Map<String, String> STYLE = new HashMap<String, String>(); //初始化加载,有其它操作时在里面添加 static{ STYLE.put("WA", "window.alert"); STYLE.put("DW", "document.write"); } /** * function encode() { * var code = document.getElementById('code').value; * code = code.replace(/[\r\n]+/g, ''); * code = code.replace(/'/g, "\\'"); * var tmp = code.match(/\b(\w+)\b/g); * tmp.sort(); * var dict = []; * var i, t = ''; * for(var i=0; i<tmp.length; i++) { * if(tmp[i] != t) dict.push(t = tmp[i]); * } * var len = dict.length; * var ch; * for(i=0; i<len; i++) { * ch = num(i); * code = code.replace(new RegExp('\\b'+dict[i]+'\\b','g'), ch); * if(ch == dict[i]) dict[i] = ''; * } * document.getElementById('code').value = "eval(function(p,a,c,k,e,d){e=function(c){return(c<a?'':e(parseInt(c/a)))+((c=c%a)>35?String.fromCharCode(c+29):c.toString(36))};if(!''.replace(/^/,String)){while(c--)d[e(c)]=k[c]||e(c);k=[function(e){return d[e]}];e=function(){return'\\\\w+'};c=1};while(c--)if(k[c])p=p.replace(new RegExp('\\\\b'+e(c)+'\\\\b','g'),k[c]);return p}(" * + "'"+code+"',"+a+","+len+",'"+ dict.join('|')+"'.split('|'),0,{}))"; * } * * function num(c) { * return(c<a?'':num(parseInt(c/a)))+((c=c%a)>35?String.fromCharCode(c+29):c.toString(36)); * } */ /** * Javascript代码混淆器,参照Javascript代码所写. * @author 李英夫 (2010-6-7 下午01:32:27) * @param JSCode Javascript代码 * @return String 返回加密码后的代码 */ private static String JSCodeConfuser(String JSCode){ String code = JSCode, t = "", ch = ""; List<String> list = new ArrayList<String>(), dict = new ArrayList<String>(); /** * 1.Java中四个\代表一个在传化时还要多添一个\才不会错 * 2.去换行 * 3.不能将\"替换掉 * 4.将'变成\' */ code = code.replaceAll("\\\\", "\\\\\\\\").replaceAll("\r\n", "").replaceAll("[^\"]\\\\", "\").replaceAll("'", "\\\\'"); Pattern p = Pattern.compile("(?m)\\b(\\w+)\\b"); Matcher matcher = p.matcher(code); while (matcher.find()) list.add(matcher.group()); String[] rs = (String[]) list.toArray(new String[list.size()]); Arrays.sort(rs); for(int i = 0;i < rs.length; i++){ if(!rs[i].equals(t)){ t = rs[i]; dict.add(t); } } int len = dict.size(); for(int i = 0; i < len; i++){ ch = num(i); code = code.replaceAll("\\b"+dict.get(i)+"\\b", ch); if(ch.equals(dict.get(i))) { dict.remove(i); dict.add(i, ""); } } StringBuffer dictStr = new StringBuffer(); for(int i = 0; i < dict.size(); i++) dictStr.append(dict.get(i)).append("|"); String temp = dictStr.toString().substring(0, dictStr.toString().length()-1); return "eval(function(p,a,c,k,e,d){e=function(c){return(c<a?'':e(parseInt(c/a)))+((c=c%a)>35?String.fromCharCode(c+29):c.toString(36))};if(!''.replace(/^/,String)){while(c--)d[e(c)]=k[c]||e(c);k=[function(e){return d[e]}];e=function(){return'\\\\w+'};c=1};while(c--)if(k[c])p=p.replace(new RegExp('\\\\b'+e(c)+'\\\\b','g'),k[c]);return p}(" + "'"+code+"',"+A+","+len+",'"+ temp +"'.split('|'),0,{}))"; } /** * 参照Javascript代码 * @author 李英夫(2010-6-7 下午01:32:27) * @param c * @return String */ private static String num(int c) { return(c<A?"":num((int)(c/A)))+((c=c%A)>35?(char)(c+29):Integer.toString(c,36)); } /** * 字符串转Unicode(Javascript专用%u形式) * @author 李英夫(2010-6-7 下午01:53:05) * @param str * @return String */ private static String stringToUnicode(String str){ StringBuffer unStr = new StringBuffer(); for(char c : str.toCharArray()){ //只转汉字 String cStr = c+""; if(cStr.matches("[^\\x00-\\xff]")) unStr.append("%u" + Integer.toHexString(c)); else unStr.append(c); } return unStr.toString(); } /** * Unicode转字符串(Javascript专用%u形式) * @author 李英夫(2010-6-7 下午01:50:50) * @param str * @return String */ private static String unicodeToString(String str) { Pattern pattern = Pattern.compile("(%u(\\p{XDigit}{4}))"); Matcher matcher = pattern.matcher(str); char ch; while (matcher.find()) { ch = (char) Integer.parseInt(matcher.group(2), 16); str = str.replace(matcher.group(1), ch + ""); } return str; } /** * 加密(默认) * 默认是document.write,以后功能多了可以重载这个方法, * 可以加一个参数encrypted(String content, String style), * 如:window.alert * @author 李英夫(2010-6-7 下午01:53:47) * @param content 内容 * @return String 返回Javascript代码的加密形式 */ public static String encrypted(String content){ String s = STYLE.get("DW")+"(unescape('" + stringToUnicode(content) + "'));"; return JSCodeConfuser(s); } /** * 加密重载 * @author 李英夫(2010-6-7 下午02:00:53) * @param content 内容 * @param style 方式 * @return String 返回Javascript代码的加密形式, * @throws Exception String */ private static String encrypted(String content, String style)throws Exception{ if(null == STYLE.get(style)) throw new Exception("this style,"+style+",is not unavailable!"); String s = STYLE.get(style)+"(unescape('" + stringToUnicode(content) + "'));"; return JSCodeConfuser(s); } public static void main(String[] args)throws Exception{ // /** // * 问题代码 // */ //// String chinese = "<pre> public static String UnicodeToString(String str) {" //// +"Pattern pattern = Pattern.compile(\"\\\");" //// +"Matcher matcher = pattern.matcher(str);//这是一个注释,。!~·!#¥%……——*()" //// +"char ch;//这是一个注释ㄆㄊㄍㄐㄗぬねノハヒ】〈〕》〗)]{』」'"∶‖~々”“’‘`|'〃" //// +"while (matcher.find()) {//这是一个注释 " //// +"ch = (char) Integer.parseInt(matcher.group(2), 16);//这是一个注释" //// +"str = str.replace(matcher.//这是一个注释group(1), ch + \");//这是一个注释" //// +"}" //// +"return str;" //// +"}</pre>"; String chinese ="<li> A. 慢性肾小球肾炎肾病型</li><li> B. 慢性肾炎晚期</li><li> C. 慢性肾炎急性发作</li><li> D. 间质性肾炎</li><li> E. 肾肿瘤</li>"; System.out.println(encrypted(chinese)); } }