⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 keepassmidlet.java

📁 KeePass for J2ME is a J2ME port of KeePass Password Safe, a free, open source, light-weight and easy
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
/*KeePass for J2MECopyright 2007 Naomaru Itoi <nao@phoneid.org>This program is free software; you can redistribute it and/or modifyit under the terms of the GNU General Public License as published bythe Free Software Foundation; version 2This program is distributed in the hope that it will be useful,but WITHOUT ANY WARRANTY; without even the implied warranty ofMERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See theGNU General Public License for more details.You should have received a copy of the GNU General Public Licensealong with this program; if not, write to the Free SoftwareFoundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA*/package org.phoneid.keepassj2me;// PhoneID utilsimport org.phoneid.*;// Javaimport javax.microedition.io.*;import javax.microedition.io.file.FileConnection;import javax.microedition.io.file.FileSystemRegistry;import javax.microedition.lcdui.*;import javax.microedition.midlet.*;import java.io.InputStream;import java.io.OutputStream;import java.io.IOException;import java.util.*;import java.io.UnsupportedEncodingException;import java.io.ByteArrayInputStream;/// record storeimport javax.microedition.rms.*;// Bouncy Castleimport org.bouncycastle1.util.encoders.Hex;public class KeePassMIDlet    extends MIDlet    implements CommandListener{    static KeePassMIDlet myself = null;    //private Form mMainForm;    private List mainList;    private boolean firstTime = true;    private Display mDisplay;    protected Command CMD_EXIT = new Command("Exit", Command.EXIT, 1);    PwManager mPwManager = null;    PwGroup mCurrentGroup;    Image mIcon[];    // timer    KeePassTimerTask mTimerTask = null;     Timer mTimer = new Timer();    private final long TIMER_DELAY = 60 * 1000 * 1000; // one hour    // background    Form mBackGroundForm = null;    /**     * Constructor     */    public KeePassMIDlet() {	myself = this;    }    /**     *     * return 0 for usual exit     * return 1 for reload KDB     */    public int openDatabaseAndDisplay()    {	// open database	try {	    Form form = new Form(Definition.TITLE);	        	    PasswordBox pwb = new PasswordBox (Definition.TITLE,					       "Enter KDB password",					       null, 64, this, true, TextField.PASSWORD,					       "Reload", "\r\nUse Reload button to reload KDB instead of using locally stored one");	    if (Definition.DEBUG) {		form.append("Reading Key Database ...\r\n");	    }	    	    if (pwb.getCommandType() == Command.ITEM) {		// Reload		System.out.println ("Reload KDB");		if (Definition.DEBUG) {		    form.append("Reload KDB\r\n");		}		// delete record store 		try {		    if (Definition.DEBUG) {			form.append("Delete record store\r\n");		    }		    RecordStore.deleteRecordStore(Definition.KDBRecordStoreName);		} catch (RecordStoreNotFoundException e) {		    // if it doesn't exist, it's OK		    if (Definition.DEBUG) {			form.append("Exception in deleting record store\r\n");		    }		}		return 1;	    }	    	    // read KDB from record store	    // open record store	    if (Definition.DEBUG) {		form.append("Will read record store\r\n");	    }	    RecordStore rs = RecordStore.openRecordStore( Definition.KDBRecordStoreName, false );	    byte[] kdbBytes = rs.getRecord(1);	    rs.closeRecordStore();	    System.out.println ("kdbBytes: " + kdbBytes.length);	    if (Definition.DEBUG) {		form.append("kdb length: " + kdbBytes.length + "\r\n");	    }	    System.out.println ("kdb: " + new String(Hex.encode(kdbBytes)));	    	    ByteArrayInputStream is = new ByteArrayInputStream(kdbBytes);	    	    // decrypt database	    form.append("Decrypting Key Database ...\r\n");	    form.append("Please Wait\r\n");	    mDisplay.setCurrent(form);	    mPwManager = new ImporterV3(Definition.DEBUG ? form : null).openDatabase(is, pwb.getResult());	    if (mPwManager != null)		System.out.println ("pwManager created");	} catch (Exception e) {	    System.out.println ("openDatabaseAndDisplay() received exception: " + e.toString());	    // doAlert(e.toString());	    MessageBox box = new MessageBox (Definition.TITLE,					     "openDatabaseAndDisplay() received exception: " + e.toString(),					     AlertType.ERROR,					     this,					     false,					     null);	    box.waitForDone();	    System.out.println ("alert done");	    return -1;	}		System.out.println ("call makeList");	// construct tree	mPwManager.constructTree(null);	// start from root position	mCurrentGroup = mPwManager.rootGroup; 	mainList = makeList(mCurrentGroup);	System.out.println ("makeList done");	mainList.addCommand(CMD_EXIT);	mainList.setCommandListener(this);	System.out.println ("setCurrent to mainList");	mDisplay.setCurrent(mainList);	mTimerTask = new KeePassTimerTask(this);	mTimer.schedule(mTimerTask, TIMER_DELAY);	System.out.println ("openDatabaseAndDisplay() return");	return 0;    }    // Ask user whether I should download KDB from web server,    // or use the one stored locally in .jar.    // Then get KDB    private void obtainKDB()	throws IOException, PhoneIDException, RecordStoreException    {	try {	    // if KDB is already in record store, don't do anything	    RecordStore rs = RecordStore.openRecordStore(Definition.KDBRecordStoreName, false);	    if (rs.getNumRecords() > 0) {		rs.closeRecordStore();		return;	    }	    rs.closeRecordStore();	} catch (RecordStoreNotFoundException e) {	    // record store doesn't exist yet - that's OK	}		// Ask user KDB download preference	KDBSelection kdbSelection = new KDBSelection(this);	kdbSelection.waitForDone();		if (kdbSelection.getResult() == 0) {	    // download from HTML	    System.out.println ("Download KDB from web server");	    String url = null, userCode = null, passCode = null, encCode = null;	    while (true) {		URLCodeBox box = new URLCodeBox(Definition.TITLE, this, true);		url = box.getURL();		userCode = box.getUserCode();		passCode = box.getPassCode();		encCode = box.getEncCode();		// check input length		if (userCode.length() != Definition.USER_CODE_LEN) {		    MessageBox msg = new MessageBox(Definition.TITLE,						    new String("User code length must be " + Definition.USER_CODE_LEN),						    AlertType.ERROR, this,						    false, null);		    msg.waitForDone();		} else if (passCode.length() != Definition.PASS_CODE_LEN) {		    MessageBox msg = new MessageBox(Definition.TITLE,						    new String("Pass code length must be " + Definition.PASS_CODE_LEN),						    AlertType.ERROR, this,						    false, null);		    msg.waitForDone();		} else if (encCode.length() != Definition.ENC_CODE_LEN) {		    MessageBox msg = new MessageBox(Definition.TITLE,						    new String("Enc code length must be " + Definition.ENC_CODE_LEN),						    AlertType.ERROR, this,						    false, null);		    msg.waitForDone();		} else {		    break;		}	    }	    // got secret code	    // now download kdb from web server	    Form waitForm = new Form(Definition.TITLE);	    waitForm.append("Downloading ...\n");	    mDisplay.setCurrent(waitForm);	    HTTPConnectionThread t =  new HTTPConnectionThread(url, userCode, passCode, encCode, this, waitForm); 	    t.start();	    	    try {		t.join();	    } catch (InterruptedException e) {		System.out.println (e.toString());	    }	    	} else if (kdbSelection.getResult() == 2) {		// we should use the FileConnection API to load from the file system		// search for the file		Alert findingAlert = new Alert("Searching...", "Searching for Database.kdb", null, AlertType.INFO);		findingAlert.setTimeout(Alert.FOREVER);		findingAlert.setIndicator(new Gauge(null, false, Gauge.INDEFINITE, Gauge.CONTINUOUS_RUNNING));		Displayable prevDisp = mDisplay.getCurrent();		mDisplay.setCurrent(findingAlert);		String dbUrl = findFile("Database.kdb");		mDisplay.setCurrent(prevDisp);		if (dbUrl == null) {			throw new PhoneIDException("Couldn't find Database.kdb");		}				// open the file		FileConnection conn = (FileConnection)Connector.open(dbUrl);		if (!conn.exists()) {			System.out.println("File doesn't exist");			throw new PhoneIDException("File does not exist: "+conn.getPath()+conn.getName());		}		InputStream is = conn.openInputStream();		int read = 0;		// TODO what if the file is too big for a single array?		byte buf[] = new byte[(int)conn.fileSize()];		// TODO this read is blocking and may not read the whole file		read = is.read(buf);		conn.close();		System.out.println("Storing "+read+" bytes into buf.");		storeKDBInRecordStore(buf, buf.length);	} else {	    // Use local KDB	    // read key database file	    InputStream is = getClass( ).getResourceAsStream("/Database.kdb");	    if (is == null) {

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -