程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> JAVA編程 >> J2ME >> J2ME手機文件加密

J2ME手機文件加密

編輯:J2ME

最近剛剛學習J2ME  真可惜 J2ME的類太少 功能太弱了!哎~..... 而且什麼東西都受安全限制,不過也有很多因素是硬件方面的!而且當 CLASS訪問本地文件的時候,需要在文件訪問權限那裡開啟

package cn.isto;

import Javax.microedition.midlet.*;
import Javax.microedition.lcdui.*;
import Javax.microedition.io.*;
import Java.io.*;
/**
 *
 * @author Administrator
 */
public class EncryptTool extends MIDlet implements CommandListener {
   
    /** Creates a new instance of HelloMidlet */
    public EncryptTool() {
    }
   
    private Form Encrypt;                    
    private Command exitCommand;
    private TextFIEld filepath;
    private TextFIEld enterpass;
    private TextFIEld againpass;
    private ChoiceGroup choiceTypeGroup;
    private Command okCommand;
    private TextFIEld outputPath;
    private Spacer spacer1;
    private Gauge gauge1;
    private Ticker tickerMsg;                  

                    

    /** This method initializes UI of the application.                       
     */
    private void initialize() {                     
 // Insert pre-init code here
        getDisplay().setCurrent(get_Encrypt());                     
 // Insert post-init code here
    }                    
   
    /** Called by the system to indicate that a command has been invoked on a particular displayable.                     
     * @param command the Command that ws invoked
     * @param displayable the Displayable on which the command was invoked
     */
    public void commandAction(Command command, Displayable displayable) {                   
        // Insert global pre-action code here
        if (displayable == Encrypt) {                    
            if (command == exitCommand) {                  
                // Insert pre-action code here
                exitMIDlet();                      
                // Insert post-action code here
            } else if (command == okCommand) {                    
                tickerMsg.setString("kj021320簡單加密程序");
                String OperateFile=filepath.getString();
                String outputFile=outputPath.getString();
                InputStream fileR=null;
                OutputStream fileW=null;
                if((operateFile!=null&&!"".equals(OperateFile))&&(outputPath!=null&&!"".equals(outputPath))){
                    if(enterpass.getString().equals(againpass.getString())){
                        int pas=getPass(enterpass.getString());
                        try {
                            fileR=Connector.openInputStream(OperateFile);
                            fileW=Connector.openOutputStream(outputFile);
                            int b=fileR.read();
                            int xorB=0;
                            while(b!=-1){
                                xorB= b ^ pas;
                                fileW.write(xorB);
                                b=fileR.read();
                            }
                            tickerMsg.setString("加密成功 你的密碼為 "+enterpass.getString()+" 請記住");
                        } catch (Exception ex) {
                            tickerMsg.setString("加密失敗~ 有錯誤"+ex.toString());
                            ex.printStackTrace();
                        } finally{
                            try{fileR.close();}catch(Exception e){}
                            try{fileW.close();}catch(Exception e){}
                        }
                    }else{
                        tickerMsg.setString("兩次輸入的密碼不一樣");
                    }
                }else{
                    tickerMsg.setString("文件路徑不能為空");
                }
               
                // Do nothing                       
                // Insert post-action code here
            }                      
        }                    
        // Insert global post-action code here
}                  
    public int getPass(String pass){
        byte[] passes=pass.getBytes();
        int result=0;
        for(int len=0;len<passes.length;len++){
            result+=passes[len];
        }
        return result;
    }
    /**
     * This method should return an instance of the display.
     */
    public Display getDisplay () {                        
        return Display.getDisplay(this);
    }                       
   
    /**
     * This method should exit the midlet.
     */
    public void exitMIDlet () {                        
        getDisplay().setCurrent(null);
        destroyApp(true);
        notifyDestroyed();
    }                       
   
    /** This method returns instance for Encrypt component and should be called instead of Accessing Encrypt fIEld directly.                       
     * @return Instance for Encrypt component
     */
    public Form get_Encrypt() {
        if (Encrypt == null) {                     
            // Insert pre-init code here
            Encrypt = new Form(null, new Item[] {                      
                get_filepath(),
                get_outputPath(),
                get_enterpass(),
                get_againpass(),
                get_choiceTypeGroup()
            });
            Encrypt.addCommand(get_exitCommand());
            Encrypt.addCommand(get_okCommand());
            Encrypt.setCommandListener(this);
            Encrypt.setTicker(get_tickerMsg());                    
            // Insert post-init code here
        }                     
        return Encrypt;
    }                   
   
   
    /** This method returns instance for exitCommand component and should be called instead of Accessing exitCommand fIEld directly.                       
     * @return Instance for exitCommand component
     */
    public Command get_exitCommand() {
        if (exitCommand == null) {                     
            // Insert pre-init code here
            exitCommand = new Command("Exit", Command.EXIT, 1);                     
            // Insert post-init code here
        }                     
        return exitCommand;
    }                   
 
    /** This method returns instance for filepath component and should be called instead of Accessing filepath fIEld directly.                        
     * @return Instance for filepath component
     */
    public TextFIEld get_filepath() {
        if (filepath == null) {                      
            // Insert pre-init code here
            filepath = new TextField("filePath:", null, 120, TextFIEld.ANY);                      
            // Insert post-init code here
        }                      
        return filepath;
    }                    

    /** This method returns instance for enterpass component and should be called instead of Accessing enterpass fIEld directly.                        
     * @return Instance for enterpass component
     */
    public TextFIEld get_enterpass() {
        if (enterpass == null) {                      
            // Insert pre-init code here
            enterpass = new TextField("enterpass", null, 120, TextField.ANY | TextFIEld.PASSWord);                      
            // Insert post-init code here
        }                      
        return enterpass;
    }                    

    /** This method returns instance for againpass component and should be called instead of Accessing againpass fIEld directly.                        
     * @return Instance for againpass component
     */
    public TextFIEld get_againpass() {
        if (againpass == null) {                      
            // Insert pre-init code here
            againpass = new TextField("againpass", null, 120, TextField.ANY | TextFIEld.PASSWord);                      
            // Insert post-init code here
        }                      
        return againpass;
    }                    
 

    /** This method returns instance for choiceTypeGroup component and should be called instead of Accessing choiceTypeGroup fIEld directly.                        
     * @return Instance for choiceTypeGroup component
     */
    public ChoiceGroup get_choiceTypeGroup() {
        if (choiceTypeGroup == null) {                      
            // Insert pre-init code here
            choiceTypeGroup = new ChoiceGroup("chooseType", Choice.POPUP, new String[0], new Image[0]);                       
            choiceTypeGroup.setSelectedFlags(new boolean[0]);                     
            // Insert post-init code here
            choiceTypeGroup.insert(0,"encrypt",null);
            choiceTypeGroup.insert(0,"decrypt",null);
        }                      
        return choiceTypeGroup;
    }                    

    /** This method returns instance for okCommand component and should be called instead of Accessing okCommand fIEld directly.                        
     * @return Instance for okCommand component
     */
    public Command get_okCommand() {
        if (okCommand == null) {                      
            // Insert pre-init code here
            okCommand = new Command("OK", Command.OK, 1);                      
            // Insert post-init code here
        }                      
        return okCommand;
    }                    

    /** This method returns instance for outputPath component and should be called instead of Accessing outputPath fIEld directly.                        
     * @return Instance for outputPath component
     */
    public TextFIEld get_outputPath() {
        if (outputPath == null) {                      
            // Insert pre-init code here
            outputPath = new TextField("outputPath:", null, 120, TextFIEld.ANY);                      
            // Insert post-init code here
        }                      
        return outputPath;
    }                    

    /** This method returns instance for spacer1 component and should be called instead of Accessing spacer1 fIEld directly.                        
     * @return Instance for spacer1 component
     */
    public Spacer get_spacer1() {
        if (spacer1 == null) {                      
            // Insert pre-init code here
            spacer1 = new Spacer(1000, 1);                      
            // Insert post-init code here
        }                      
        return spacer1;
    }                    

    /** This method returns instance for gauge1 component and should be called instead of Accessing gauge1 fIEld directly.                        
     * @return Instance for gauge1 component
     */
    public Gauge get_gauge1() {
        if (gauge1 == null) {                      
            // Insert pre-init code here
            gauge1 = new Gauge("gauge1", false, 100, 50);                      
            // Insert post-init code here
        }                      
        return gauge1;
    }                    

    /** This method returns instance for tickerMsg component and should be called instead of Accessing tickerMsg fIEld directly.                        
     * @return Instance for tickerMsg component
     */
    public Ticker get_tickerMsg() {
        if (tickerMsg == null) {                      
            // Insert pre-init code here
            tickerMsg = new Ticker("kj021320\u7B80\u5355\u52A0\u5BC6\u7A0B\u5E8F");                      
            // Insert post-init code here
        }                      
        return tickerMsg;
    }                    
    
    public void startApp() {
        initialize();
    }
   
    public void pauseApp() {
    }
   
    public void destroyApp(boolean unconditional) {
    }
}

程序采用 異或形式加密~ 所以加密就是解密...choosetype那個按扭是多余的!呵呵.忘記說一點~ Connector不支持創建文件! 所以輸入和輸出文件必須要存在,-_-郁悶了吧! 本地文件協議如下例如

file:///c:\13.jpg 這樣才能訪問本地文件

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved