decode javascript.encode using java

decode javascript.encode using java

在前面一篇文章(http://www.hexiao.cn/blog/index.php?job=art&articleid=a_20060730_130422)中, 提到了 关于IE中的 js.edcode的加密问题. 由于要使用这些加密后的信息,就要解密,在网络上找了一个js版本的解密程序, 由于要在java中使用,就把他转化为java代码了, 解密js.encode的java代码如下:

 

//import java.util.regex.Matcher;
//import java.util.regex.Pattern;
/**
 * Decode encode
 * @author icess
 * @blog http://blog.matrix.org.cn/page/icess
 */
public class EncodeDecode {
 final int STATE_COPY_INPUT = 100;

 final int STATE_READLEN = 101;

 final int STATE_DECODE = 102;

 final int STATE_UNESCAPE = 103;

 int[] digits = new int[128];

 int[][] transformed = new int[3][128];

 int[] pick_encoding = { 1, 2, 0, 1, 2, 0, 2, 0, 0, 2, 0, 2, 1, 0, 2, 0, 1,
   0, 2, 0, 1, 1, 2, 0, 0, 2, 1, 0, 2, 0, 0, 2, 1, 1, 0, 2, 0, 2, 0,
   1, 0, 1, 1, 2, 0, 1, 0, 2, 1, 0, 2, 0, 1, 1, 2, 0, 0, 1, 1, 2, 0,
   1, 0, 2 };

 int[] rawData = { 0x64, 0x37, 0x69, 0x50, 0x7E, 0x2C, 0x22, 0x5A, 0x65,
   0x4A, 0x45, 0x72, 0x61, 0x3A, 0x5B, 0x5E, 0x79, 0x66, 0x5D, 0x59,
   0x75, 0x5B, 0x27, 0x4C, 0x42, 0x76, 0x45, 0x60, 0x63, 0x76, 0x23,
   0x62, 0x2A, 0x65, 0x4D, 0x43, 0x5F, 0x51, 0x33, 0x7E, 0x53, 0x42,
   0x4F, 0x52, 0x20, 0x52, 0x20, 0x63, 0x7A, 0x26, 0x4A, 0x21, 0x54,
   0x5A, 0x46, 0x71, 0x38, 0x20, 0x2B, 0x79, 0x26, 0x66, 0x32, 0x63,
   0x2A, 0x57, 0x2A, 0x58, 0x6C, 0x76, 0x7F, 0x2B, 0x47, 0x7B, 0x46,
   0x25, 0x30, 0x52, 0x2C, 0x31, 0x4F, 0x29, 0x6C, 0x3D, 0x69, 0x49,
   0x70, 0x3F, 0x3F, 0x3F, 0x27, 0x78, 0x7B, 0x3F, 0x3F, 0x3F, 0x67,
   0x5F, 0x51, 0x3F, 0x3F, 0x3F, 0x62, 0x29, 0x7A, 0x41, 0x24, 0x7E,
   0x5A, 0x2F, 0x3B, 0x66, 0x39, 0x47, 0x32, 0x33, 0x41, 0x73, 0x6F,
   0x77, 0x4D, 0x21, 0x56, 0x43, 0x75, 0x5F, 0x71, 0x28, 0x26, 0x39,
   0x42, 0x78, 0x7C, 0x46, 0x6E, 0x53, 0x4A, 0x64, 0x48, 0x5C, 0x74,
   0x31, 0x48, 0x67, 0x72, 0x36, 0x7D, 0x6E, 0x4B, 0x68, 0x70, 0x7D,
   0x35, 0x49, 0x5D, 0x22, 0x3F, 0x6A, 0x55, 0x4B, 0x50, 0x3A, 0x6A,
   0x69, 0x60, 0x2E, 0x23, 0x6A, 0x7F, 0x09, 0x71, 0x28, 0x70, 0x6F,
   0x35, 0x65, 0x49, 0x7D, 0x74, 0x5C, 0x24, 0x2C, 0x5D, 0x2D, 0x77,
   0x27, 0x54, 0x44, 0x59, 0x37, 0x3F, 0x25, 0x7B, 0x6D, 0x7C, 0x3D,
   0x7C, 0x23, 0x6C, 0x43, 0x6D, 0x34, 0x38, 0x28, 0x6D, 0x5E, 0x31,
   0x4E, 0x5B, 0x39, 0x2B, 0x6E, 0x7F, 0x30, 0x57, 0x36, 0x6F, 0x4C,
   0x54, 0x74, 0x34, 0x34, 0x6B, 0x72, 0x62, 0x4C, 0x25, 0x4E, 0x33,
   0x56, 0x30, 0x56, 0x73, 0x5E, 0x3A, 0x68, 0x73, 0x78, 0x55, 0x09,
   0x57, 0x47, 0x4B, 0x77, 0x32, 0x61, 0x3B, 0x35, 0x24, 0x44, 0x2E,
   0x4D, 0x2F, 0x64, 0x6B, 0x59, 0x4F, 0x44, 0x45, 0x3B, 0x21, 0x5C,
   0x2D, 0x37, 0x68, 0x41, 0x53, 0x36, 0x61, 0x58, 0x58, 0x7A, 0x48,
   0x79, 0x22, 0x2E, 0x09, 0x60, 0x50, 0x75, 0x6B, 0x2D, 0x38, 0x4E,
   0x29, 0x55, 0x3D, 0x3F };

 public EncodeDecode() {
//  System.out.println(rawData[0]);
  init();
 }

 public String decode(String encodingString) {
//  System.out.println("encodingString  :  " + encodingString);
  String marker = "#@~^";
  String at;
  String encodingLable = null;
  String unEncodingString = "";
  int stringIndex = 0;
  int scriptIndex = -1;
  int unEncodingIndex = 0;
  int unEncodinglength = 0;
  int state = STATE_COPY_INPUT;
  // int statePos = 0;

  while (state != 0) {
   // System.out.println("in the Swith statePos is :" + statePos +
   // "State:" +state);
   switch (state) {
   case (STATE_COPY_INPUT):
    scriptIndex = encodingString.indexOf(marker, stringIndex);
    // System.out.println("scriptIndex1"+scriptIndex);
    if (scriptIndex != -1) {
     unEncodingString += encodingString.substring(stringIndex,
       stringIndex + scriptIndex);
     scriptIndex += marker.length();
     state = STATE_READLEN;
    } else {
     stringIndex = stringIndex == 0 ? 0 : stringIndex;
     unEncodingString += encodingString.substring(stringIndex,
       encodingString.length());
     state = 0;
    }
    break;

   case (STATE_READLEN):
    // System.out.println("scriptIndex"+scriptIndex);
    encodingLable = encodingString.substring(scriptIndex,
      scriptIndex + 6);
    unEncodinglength = decodeBase64(encodingLable);
    scriptIndex += (6 + "==".length());
    state = STATE_DECODE;
    break;

   case (STATE_DECODE):
    if (unEncodinglength == 0) {
     stringIndex = scriptIndex + "DQgAAA==^#~@".length();
     unEncodingIndex = 0;
     state = STATE_COPY_INPUT;
     break;
    }
    at = encodingString.substring(scriptIndex, scriptIndex + 1);
    if (at == "@")
     state = STATE_UNESCAPE;
    else {
     if (at.codePointAt(0) < 0xFF) {
      unEncodingString += String
        .valueOf((char) transformed[pick_encoding[unEncodingIndex % 64]][at
          .codePointAt(0)]);
      unEncodingIndex++;
     } else {
      unEncodingString += at;
     }
     scriptIndex++;
     unEncodinglength--;
     break;
    }

   case STATE_UNESCAPE:
    unEncodingString += unescape(encodingString.substring(
      ++scriptIndex, scriptIndex + 1));
    scriptIndex++;
    unEncodinglength -= 2;
    unEncodingIndex++;
    state = STATE_DECODE;
    break;
   }
  }

  // System.out.println("unEncodingString : " + unEncodingString);
  // Pattern pattern = Pattern.compile("JScript.Encode");
  // Matcher matcher = pattern.matcher(unEncodingString);
  // if(matcher.find()) {
  // System.out.println("unEncodingString 1: " + unEncodingString);
  // unEncodingString = unEncodingString.substring(0, matcher.start()) +
  // matcher.group() + unEncodingString.substring(matcher.end());
  // }

//  System.out.println("EncodeDecode.decode : unEncodingString  :  " + unEncodingString);
  return unEncodingString;
 }

 public int decodeBase64(String string) {
  int val = 0;
  val += (digits[string.codePointAt(0)] << 2);
  val += (digits[string.codePointAt(1)] >> 4);
  val += (digits[string.codePointAt(1)] & 0xf) << 12;
  val += ((digits[string.codePointAt(2)] >> 2) << 8);
  val += ((digits[string.codePointAt(2)] & 0x3) << 22);
  val += (digits[string.codePointAt(3)] << 16);
  return val;
 }

 public String unescape(String ch) { // no used, and no test
  // System.out.println("dddd");
  String escapes = "#&!*$";
  String escaped = "\r\n<>@";

  if (ch.codePointAt(0) > 126)
   return ch;
  int index = escapes.indexOf(ch);
  if (index != -1)
   return escaped.substring(index, index + 1);
  return "?";
 }

 public void init() {
  for (int i = 31; i <= 126; i++)
   for (int j = 0; j < 3; j++)
    transformed[j][rawData[(i - 31) * 3 + j]] = (i == 31) ? 9 : i;

  for (int i = 0; i < 26; i++) {
   digits['A' + i] = i;
   digits['a' + i] = i + 26;
  }
  for (int i = 0; i < 10; i++)
   digits['0' + i] = i + 52;
  digits[0x2b] = 62;
  digits[0x2f] = 63;
 }

 /**
  * @param args
  */
 public static void main(String[] args) {
  // new EncodeDecode().decode("<script
  // language='JScript.Encode'>#@~^SwAAAA==-mD~DK'B8vZfF~!0FyF/TFFFZXFR!F8)TvZc82qsq98,TfZ+!wF{T;F&q*Z2T!Z1ZAqZFl!bEiyRAAAA==^#~@</script>");
  String plantext = new EncodeDecode()
    .decode("#@~^SwAAAA==-mD~DK'B8vZfF~!0FyF/TFFFZXFR!F8)TvZc82qsq98,TfZ+!wF{T;F&q*Z2T!Z1ZAqZFl!bEiyRAAAA==^#~@");
  System.out
    .println(plantext
      .equals("var ero='16031B08121C07110518011A06041E1F1D190D020F170C13140E00090B10150A';"));
  System.out.println("0".codePointAt(0) + " " + (int) 'a' + " "
    + (int) 'A');
  
  System.out.println("ididgigggi".indexOf("i", 1));
 }

}

现在可以得到需要的信息了. 呵呵

你可能感兴趣的:(decode javascript.encode using java)