溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

Java混淆編譯器怎么實現(xiàn)

發(fā)布時間:2021-12-30 17:27:40 來源:億速云 閱讀:133 作者:iii 欄目:編程語言

本篇內(nèi)容主要講解“Java混淆編譯器怎么實現(xiàn)”,感興趣的朋友不妨來看看。本文介紹的方法操作簡單快捷,實用性強。下面就讓小編來帶大家學習“Java混淆編譯器怎么實現(xiàn)”吧!

Q: 什么是混淆器?
A: 由于Java程序運行時是動態(tài)連接的,因此編譯成的目標文件中包含有符號表,使得Java程序很容易被反編譯,混淆器可以打亂class文件中的符號信息,使反向工程變得非常困難。

Q: 現(xiàn)有的混淆器有什么問題?
A: 現(xiàn)有的混淆器都是對編譯好的class文件進行混淆,這樣就需要編譯和混淆兩個步驟。并不是所有的符號都需要混淆,如果你開發(fā)的是一個類庫,或者某些類需要動態(tài)裝載,那些公共API就必須保留符號不變,這樣別人才能使用你的類庫?,F(xiàn)有的混淆器提供了GUI或腳本的方式來對那些需要保留的符號名稱進行配置,如果程序較大時配置工作變得很復雜,而程序一旦修改配置工作又要重新進行。某些混淆器能夠調(diào)整字節(jié)碼的順序,使反編譯更加困難,但我經(jīng)歷過混淆之后的程序運行出錯的情況。

Q: Java混淆編譯器是如何工作的?
A: Java混淆編譯器是在Sun JDK中提供的Java編譯器(javac)的基礎上完成的,修改了代碼生成過程,對編譯器生成的中間代碼進行混淆,最后再生成class文件,這樣編譯和混淆只需要一個步驟就可以完成。另外可以在源程序中插入符號保留指令來控制哪些符號需要保留,不需要單獨的配置。

Q: 如何安裝和運行JOC?
A: 下載joc.jar ,運行java -jar joc.jar就可以啟動Java混淆編譯器,joc的命令行參數(shù)和javac完全相同,但增加了一個新的參數(shù)-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不帶級別參數(shù),則相當于-Xobfuscate:package

Q: 如何使用符號保留指令?
A: 除了在命令行用-Xobfuscate參數(shù)控制符號混淆級別外,還可以在源代碼中使用符號保留指令來控制那些符號需要保留,符號保留指令是一個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指令,則根據(jù)混淆級別及成員的訪問級別來確定符號是否保留。

對于類的符號保留指令可以附帶一個保留級別參數(shù),來控制類成員的符號保留,包括:
@preserve 僅對類名進行保留,類成員的保留根據(jù)-Xobfuscate命令行參數(shù)決定
@preserve public 保留所有public成員
@preserve protected 保留所有public和protected成員
@preserve package 保留所有public, protected, package private成員
@preserve private 保留所有成員
@preserve all 相當于@preserve private

Q: JOC有哪些限制?
A: 不支持分別編譯,必須對所有的源文件進行混淆編譯。

源文件:
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();
}
}

經(jīng)Javac編譯后用JAD反編譯的結(jié)果:

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

經(jīng)JOC混淆編譯后用JAD反編譯的結(jié)果:

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 _$1;
JButton _$2;
JLabel _$3;
JTextArea _$4;

public AboutBox()
{
_$1 = new JPanel();
_$2 = new JButton();
_$3 = new JLabel();
_$4 = new JTextArea();
_$1();
}

private void _$1()
{
2;
this;
JVM INSTR swap ;
setDefaultCloseOperation();
getContentPane().setLayout(new CardLayout());
true;
this;
JVM INSTR swap ;
setModal();
false;
this;
JVM INSTR swap ;
setResizable();
"About...";
this;
JVM INSTR swap ;
setTitle();
_$1.setLayout(null);
_$2.setText("OK");
_$2;
168;
272;
JVM INSTR swap ;
24;
88;
JVM INSTR swap ;
setBounds();
_$1.add(_$2);
_$3.setText("File System Viewer for Swing 1.1.1");
_$3.setVerticalAlignment(1);
_$3;
32;
64;
JVM INSTR swap ;
56;
240;
JVM INSTR swap ;
setBounds();
_$1.add(_$3);
_$4;
JVM INSTR new #13;
JVM INSTR dup ;
0;
"Dialog";
JVM INSTR swap ;
10;
Font();
setFont();
_$4.setLineWrap(true);
_$4.setOpaque(false);
_$4.setText("This computer program is protected by copyright law.");
_$4.setWrapStyleWord(true);
_$4;
112;
8;
JVM INSTR swap ;
80;
256;
JVM INSTR swap ;
setBounds();
_$1.add(_$4);
getContentPane().add(_$1, "Card1");
376;
this;
JVM INSTR swap ;
228;
setSize();
_$2.addActionListener(new IIlIlIIIIlllIIII(this));
return;
}

private void _$1(ActionEvent actionevent)
{
dispose();
}

/*
static void access$0(AboutBox aboutbox, ActionEvent actionevent)
{
actionevent;
aboutbox;
JVM INSTR swap ;
_$1();
return;
}

*/

// Unreferenced inner classes:

/* anonymous class */
final class IIlIlIIIIlllIIII
implements ActionListener
{
public void actionPerformed(ActionEvent actionevent)
{
AboutBox.access$0(AboutBox.this, actionevent);
}

{
AboutBox.this;
this;
JVM INSTR swap ;
this$0;
}
}
}

到此,相信大家對“Java混淆編譯器怎么實現(xiàn)”有了更深的了解,不妨來實際操作一番吧!這里是億速云網(wǎng)站,更多相關內(nèi)容可以進入相關頻道進行查詢,關注我們,繼續(xù)學習!

向AI問一下細節(jié)

免責聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI