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 这样才能访问本地文件