使用Proguard混淆J2ME應用程序

 如果我們的開發的J2ME應用程序直接打包成JAR文件發佈,那麼存在被其他人反編譯的可能。因爲反編譯class文件並不是一件困難的事情。爲了保護我們的程序代碼不被破解,我們可以使用混淆器Proguard。非常幸運的是eclipse已經把Proguard集成在一起了。

    Proguard是開源的軟件,他是基於java語言寫成的,因此他的運行需要java2運行環境。我們可以從http://Proguard.sourceforge.net 免費下載到。目前的最新版本爲proguard3.0。把他解壓縮安裝在C:/proguard3.0.1。運行eclipse,選擇菜單windows-preferences-j2me-obfuscation,在這裏我們應該指定正確的Proguard的根目錄,由於我們混淆的時候要保留擴展了MIDlet的類,不然程序將無法執行。所以在Proguard keep expressions中應該寫public class * extends javax.microedition.midlet.MIDlet。請參考下圖
2004923161556773.gif

 

 

 

 

 

 

 







 

 

 

    下面我們按照開發J2ME的應用程序一樣編寫代碼、編譯、預驗證。爲了節省時間,在這裏略去這些步驟。以下面的代碼爲例演示如何使用proguard混淆J2ME應用程序:

import javax.microedition.midlet.MIDlet;
import javax.microedition.midlet.MIDletStateChangeException;
import javax.microedition.lcdui.*;
import java.io.*;
import javax.microedition.io.*;

public class ImageGetter extends MIDlet implements CommandListener
{

    private Display display;
    public static final Command connCommand = new Command("Connect",
            Command.ITEM, 1);
    public static final Command exitCommand = new Command("Exit", Command.EXIT,
            1);
    private Form mainForm;
    private GetterThread gt;

    protected void startApp() throws MIDletStateChangeException
    {

        display = Display.getDisplay(this);
        mainForm = new Form("Image Getter");
        mainForm.append("Click Connect to get Image");
        mainForm.addCommand(connCommand);
        mainForm.addCommand(exitCommand);
        mainForm.setCommandListener(this);
        display.setCurrent(mainForm);
        gt = new GetterThread(this);
        gt.start();

    }

    public void setImage(Image image)
    {

        mainForm.append(image);
        display.setCurrent(mainForm);
    }

    protected void pauseApp()
    {

    }

    protected void destroyApp(boolean arg0) throws MIDletStateChangeException
    {

    }

    public void commandAction(Command cmd, Displayable disp)
    {
        if (cmd == connCommand)
        {
            synchronized (this)
            {
                notify();
            }
        } else if (cmd == exitCommand)
        {
            exitMIDlet();
        }
    }

    private void exitMIDlet()
    {
        try
        {
            destroyApp(false);
            notifyDestroyed();
        } catch (MIDletStateChangeException e)
        {
            e.printStackTrace();
        }
    }

    class GetterThread extends Thread
    {
        private ImageGetter midlet;
        public static final String URL = "http://localhost/j2medev.png";
        private HttpConnection httpConn = null;
        private InputStream is = null;

        public GetterThread(ImageGetter midlet)
        {
            this.midlet = midlet;
        }

        public void run()
        {
            synchronized (midlet)
            {
                try
                {
                    midlet.wait();
                } catch (InterruptedException e)
                {
                    e.printStackTrace();
                }
            }
            System.out.println("connect to server...");
            try
            {
                httpConn = (HttpConnection) Connector.open(URL);
                is = httpConn.openInputStream();
                ByteArrayOutputStream baos = new ByteArrayOutputStream();
                int ch = 0;
                while ((ch = is.read()) != -1)
                {
                    baos.write(ch);
                }
                byte[] imageData = baos.toByteArray();
                Image image = Image.createImage(imageData, 0, imageData.length);
                midlet.setImage(image);
                baos.close();
                is.close();
                httpConn.close();

            } catch (IOException e)
            {
                e.printStackTrace();
            }

        }
    }

}

右鍵選擇項目GetImage-J2ME-create obfuscated package,這樣proguard就會把除MIDlet之外的class文件混淆。如下圖所示:
2004923162546804.gif

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 







 




在deploy文件夾內我們可以看到GetImage.jar、GetImage.jad和一些proguard產生的其他文件,你可以用jar命令解開GetImage.jar,發現他的另一個class文件已經被混淆成a.class了,MIDlet類則沒有任何改變,Proguard除了混淆的功能之外同時會把我們的jar文件減小,提高一些運行效率。

 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章