Q: 什么是混淆器? 


  A: 由于Java程序运行时是动态连接的,因此编译成的目标文件中包含有符号表, 

  使得Java程序很容易被反编译,混淆器可以打乱class文件中的符号信息,使反向 

  工程变得非常困难。 


  Q: 现有的混淆器有什么问题? 


  A: 现有的混淆器都是对编译好的class文件进行混淆,这样就需要编译和混淆两个 


  步骤。并不是所有的符号都需要混淆,如果你开发的是一个类库,或者某些类需要 


  动态装载,那些公共API就必须保留符号不变,这样别人才能使用你的类库。现有 


  的混淆器提供了GUI或脚本的方式来对那些需要保留的符号名称进行配置,如果程 


  序较大时配置工作变得很复杂,而程序一旦修改配置工作又要重新进行。某些混淆 


  器能够调整字节码的顺序,使反编译更加困难,但我经历过混淆之后的程序运行出 


  错的情况。 


  Q: Java混淆编译器是如何工作的? 


  A: Java混淆编译器是在Sun JDK中提供的Java编译器(javac)的基础上完成的,修 


  改了代码生成过程,对编译器生成的中间代码进行混淆,最后再生成class文件, 


  这样编译和混淆只需要一个步骤就可以完成。另外可以在源程序中插入符号保留指 


  令来控制哪些符号需要保留,不需要单独的配置。 


  Q: 如何安装和运行JOC? 


  A: 下载joc.jar (http://www.apusic.com/product/cpsy.htm),运行java -jar 


  joc.jar就可以启动Java混淆编译器,joc的命令行参数和javac完全相同,但增加 


  了一个新的参数-Xobfuscate,它的用法如下: 


  -Xobfuscate: 


  其中指定混淆级别,可以是以下几种级别: 


  -Xobfuscate:none 


  不进行混淆 


  -Xobfuscate:private 


  对所有private访问级别的元素进行混淆 


  -Xobfuscate:package 


  对所有private或package private元素进行混 


  淆 


  -Xobfuscate:protected 


  对所有private, package private, 


  protected元素进行混淆 


  -Xobfuscate:public 


  对所有的元素都进行混淆 


  -Xobfuscate:all 


  相当于-Xobfuscate:public 


  如果使用-Xobfuscate不带级别参数,则相当于-Xobfuscate:package 


  Q: 如何使用符号保留指令? 


  A: 除了在命令行用-Xobfuscate参数控制符号混淆级别外,还可以在源代码中使用 


  符号保留指令来控制那些符号需要保留,符号保留指令是一个Java文档注释指令, 


  可以插入在类和类成员的文档注释中,例如: 


  /** 


  * This class should preserve. 


  * @preserve 


  */ 


  public class Foo { 


  /** 


  * You can specify which field should be preserved. 


  * @preserve 

  */ 

  private int x; 


  /** 


  * This field is not preserved. 


  */ 

  private int y; 

  /** 

  * You can also preserve methods. 

  * @preserve 

  */ 

  public void hello() {} 

  /** 

  * This method is not preserved. 

  */ 

  private void collect() {} 


  } 


  如果没有@preserve指令,则根据混淆级别及成员的访问级别来确定符号是否保留 


  对于类的符号保留指令可以附带一个保留级别参数,来控制类成员的符号保留,包 


  括: 


  @preserve 


  仅对类名进行保留,类成员的保留根据 


  -Xobfuscate命令行参数决定 


  @preserve public 


  保留所有public成员 


  @preserve protected 


  保留所有public和protected成员 


  @preserve package 


  保留所有public, protected, package private成 


  员 


  @preserve private 


  保留所有成员 


  @preserve all 


  相当于@preserve private 


  Q: JOC有哪些限制? 


  A: 不支持分别编译,必须对所有的源文件进行混淆编译。 


  最后给出一个JOC混淆的效果: 


  源文件: 

  import java.awt.event.*; 

  import javax.swing.*; 

  public class AboutBox extends JDialog 

  { 

  public AboutBox() 

  { 

  initForm(); 

  } 

  JPanel panel1 = new JPanel(); 

  JButton button1 = new JButton(); 

  JLabel jLabel2 = new JLabel(); 

  JTextArea jTextArea1 = new JTextArea(); 

  /** 

  * NOTE: The following code is required by the form designer. 

  * It can be modified using the form editor. 

  Do not 

  * modify it using the code editor. 

  */ 

  private void initForm() 

  { 

  this.setDefaultCloseOperation( WindowConstants.DISPOSE_ON_CLOSE 

  ); 

  this.getContentPane().setLayout( new java.awt.CardLayout()); 

  this.setModal( true ); 

  this.setResizable( false ); 

  this.setTitle( "About..." ); 

  panel1.setLayout( null ); 

  button1.setText( "OK" ); 

  button1.setBounds( 272, 168, 88, 24 ); 

  panel1.add( button1 ); 

  jLabel2.setText( "File System Viewer for Swing 1.1.1" ); 

  jLabel2.setVerticalAlignment( SwingConstants.TOP ); 

  jLabel2.setBounds( 64, 32, 240, 56 ); 

  panel1.add( jLabel2 ); 

  jTextArea1.setFont( new java.awt.Font( "Dialog", 0, 10 )); 

  jTextArea1.setLineWrap( true ); 

  jTextArea1.setOpaque( false ); 

  jTextArea1.setText( "This computer program is protected by 

  copyright law." ); 

  jTextArea1.setWrapStyleWord( true ); 

  jTextArea1.setBounds( 8, 112, 256, 80 ); 

  panel1.add( jTextArea1 ); 

  this.getContentPane().add( panel1, "Card1" ); 

  this.setSize( 376, 228 ); 

  button1.addActionListener( new java.awt.event.ActionListener(){ 

  public void actionPerformed( java.awt.event.ActionEvent 

  ev ){ 

  button1_actionPerformed( ev ); 

  }}); 

  } 

  private void button1_actionPerformed(ActionEvent ev) 

  { 

  this.dispose(); 

  } 


  } 

  经Javac编译后用JAD反编译的结果: 

  import java.awt.*; 

  import java.awt.event.ActionEvent; 

  import java.awt.event.ActionListener; 

  import javax.swing.*; 

  import javax.swing.text.JTextComponent; 

  public class AboutBox extends JDialog 

  { 

  JPanel panel1; 

  JButton button1; 

  JLabel jLabel2; 

  JTextArea jTextArea1; 

  public AboutBox() 

  { 

  panel1 = new JPanel(); 

  button1 = new JButton(); 

  jLabel2 = new JLabel(); 

  jTextArea1 = new JTextArea(); 

  initForm(); 

  } 

  private void initForm() 

  { 

  setDefaultCloseOperation(2); 

  getContentPane().setLayout(new CardLayout()); 

  setModal(true); 

  setResizable(false); 

  setTitle("About..."); 

  panel1.setLayout(null); 

  button1.setText("OK"); 

  button1.setBounds(272, 168, 88, 24); 

  panel1.add(button1); 

  jLabel2.setText("File System Viewer for Swing 1.1.1"); 

  jLabel2.setVerticalAlignment(1); 

  jLabel2.setBounds(64, 32, 240, 56); 

  panel1.add(jLabel2); 

  jTextArea1.setFont(new Font("Dialog", 0, 10)); 

  jTextArea1.setLineWrap(true); 

  jTextArea1.setOpaque(false); 

  jTextArea1.setText("This computer program is protected by 

  copyright law."); 

  jTextArea1.setWrapStyleWord(true); 

  jTextArea1.setBounds(8, 112, 256, 80); 

  panel1.add(jTextArea1); 

  getContentPane().add(panel1, "Card1"); 

  setSize(376, 228); 

  button1.addActionListener(new ActionListener() { 

  public void actionPerformed(ActionEvent actionevent) 

  { 

  button1_actionPerformed(actionevent); 

  } 

  }); 

  } 

  private void button1_actionPerformed(ActionEvent actionevent) 

  { 

  dispose(); 

  } 

  } 

  经JOC混淆编译后用JAD反编译的结果: 

  import java.awt.*; 

  import java.awt.event.ActionEvent; 

  import java.awt.event.ActionListener; 

  import javax.swing.*; 

  import javax.swing.text.JTextComponent; 

  public class AboutBox extends JDialog 

  { 

  JPanel _