ورود

View Full Version : حرفه ای: رمزنگاری در J2ME



davar111
پنج شنبه 14 مهر 1390, 15:41 عصر
دوستان این کد مربوط به رمزنگاری و رمزگشایی متن در جاوا موبایله !
ولی من تو اجراش با Netbeans مشکل دارم .
کد و error رو میذارم ببینین میتونین بفهمین مشکل از کجاس ؟
در ضمن کد ایراد نداره اصلا .
میتونین به عنوان منبع ازش استفاده کنین . مشکل توی IDE netbeans .
من تو فرومای خارجی سرچ کردم گفتن باید JDK 32 bit نصب کنم . این کارو کردم ولی باز error میده . :ناراحت:






import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
import javax.microedition.rms.*;

import org.bouncycastle.crypto.*;

import java.math.BigInteger;

public class CryptoTest extends MIDlet {

private Display display;
private Command exitCommand = new Command( "Exit", Command.EXIT, 1 );
private Command okCommand = new Command( "OK", Command.OK, 1 );

private Encryptor encryptor;
private RecordStore rs;

/** Creates a new instance of CryptoTest */
public CryptoTest() {
}

/**
* This method initializes UI of the application.
*/
private void initialize() {
}

public void startApp() {
initialize();
if( display == null ){ // first time called...
initMIDlet();
}
}

public void pauseApp() {
}

public void destroyApp(boolean unconditional) {
exitMIDlet();
}

private void initMIDlet(){
display = Display.getDisplay( this );

// Open a record store here
try {
// This need to be changed to start with a clean reocrd
rs = RecordStore.openRecordStore( "test3",
true );
} catch( RecordStoreException e ){
// put in error handling here
}

display.setCurrent( new AskForKey() );
}

public void exitMIDlet(){
try {
if( rs != null ){
rs.closeRecordStore();
}
} catch( RecordStoreException e ){
}

notifyDestroyed();
}

private void displayException( Exception e ){
Alert a = new Alert( "Exception" );
a.setString( e.toString() );
a.setTimeout( Alert.FOREVER );
display.setCurrent( a, new AskForKey() );
}

class AskForKey extends TextBox
implements CommandListener {
public AskForKey(){
super( "Enter a secret key:", "", 8, 0 );
setCommandListener( this );
addCommand( okCommand );
addCommand( exitCommand );
}

public void commandAction( Command c,
Displayable d ){
if( c == exitCommand ){
exitMIDlet();
}

String key = getString();
if( key.length() < 8 ){
Alert a = new Alert( "Key too short" );
a.setString( "The key must be " +
"8 characters long" );
setString( "" );
display.setCurrent( a, this );
return;
}

encryptor = new Encryptor( key );

try {
if( rs.getNextRecordID() == 1 ){
display.setCurrent(
new EnterMessage() );
} else {
byte[] data = rs.getRecord( 1 );
String str =
encryptor.decryptString( data );

Alert a =
new Alert( "Decryption" );
a.setTimeout( Alert.FOREVER );
a.setString(
"The decrypted string is '" +
str + "'" );
display.setCurrent( a, this );
}
} catch( RecordStoreException e ){
displayException( e );
} catch( CryptoException e ){
displayException( e );
}
}
}

class EnterMessage extends TextBox
implements CommandListener {
public EnterMessage(){


super( "Enter a message to encrypt:", "",
100, 0 );

BigInteger bigInt = new BigInteger("199999");

setCommandListener( this );
addCommand( okCommand );
}

public void commandAction( Command c,
Displayable d ){
String msg = getString();

try {
byte[] data =
encryptor.encryptString( msg );
rs.addRecord( data, 0, data.length );
} catch( RecordStoreException e ){
displayException( e );
} catch( CryptoException e ){
displayException( e );
}

display.setCurrent( new AskForKey() );
}
}

}



اینم کلاس Encryptor

//import com.sun.kvem.midp.pim.formats.Base64Encoding;

import org.bouncycastle.crypto.*;
import org.bouncycastle.crypto.engines.*;
import org.bouncycastle.crypto.modes.*;
import org.bouncycastle.crypto.params.*;

import org.bouncycastle.util.encoders.Base64;
import org.bouncycastle.util.encoders.Base64Encoder;


// A simple example that uses the Bouncy Castle
// lightweight cryptography API to perform DES
// encryption of arbitrary data.

public class Encryptor {

private BufferedBlockCipher cipher;
private KeyParameter key;

// Initialize the cryptographic engine.
// The key array should be at least 8 bytes long.

public Encryptor(String keyString ){
/*
cipher = new PaddedBlockCipher(
new CBCBlockCipher(
new DESEngine() ) );
*/

cipher = new PaddedBlockCipher(
new CBCBlockCipher(
new BlowfishEngine() ) );

this.key = new KeyParameter(keyString.getBytes());
}

// Initialize the cryptographic engine.
// The string should be at least 8 chars long.

/*
public Encryptor( String key ){
this( this.keyBytes.getBytes() );
}
***/

// Private routine that does the gritty work.

private byte[] callCipher( byte[] data )
throws CryptoException {
int size =
cipher.getOutputSize( data.length );
byte[] result = new byte[ size ];
int olen = cipher.processBytes( data, 0,
data.length, result, 0 );
olen += cipher.doFinal( result, olen );

if( olen < size ){
byte[] tmp = new byte[ olen ];
System.arraycopy(
result, 0, tmp, 0, olen );
result = tmp;
}

return result;
}

// Encrypt arbitrary byte array, returning the
// encrypted data in a different byte array.

public synchronized byte[] encrypt( byte[] data )
throws CryptoException {
if( data == null || data.length == 0 ){
return new byte[0];
}

cipher.init( true, key );
return callCipher( data );
}

// Encrypts a string.

public byte[] encryptString( String data )
throws CryptoException {
if( data == null || data.length() == 0 ){
return new byte[0];
}

return encrypt( data.getBytes());
}

// Decrypts arbitrary data.

public synchronized byte[] decrypt( byte[] data )
throws CryptoException {
if( data == null || data.length == 0 ){
return new byte[0];
}

cipher.init( false, key );
return callCipher( data );
}

// Decrypts a string that was previously encoded
// using encryptString.

public String decryptString( byte[] data )
throws CryptoException {
if( data == null || data.length == 0 ){
return "";
}

//return new String( decrypt( data ) );

return new String (Base64.encode(data));
}
}





ارورش C:\Users\davar\Documents\NetBeansProjects\MobileAp plication5\nbproject\build-impl.xml:915: Execution failed with error code 1.
BUILD FAILED (total time: 14 seconds)



<target name="cldc-run" if="cldc-platform.trigger">
<nb-run jadfile="${dist.dir}/${dist.jad}" jarfile="${dist.dir}/${dist.jar}" jadurl="${dist.jad.url}" device="${platform.device}" platformhome="${platform.home}" platformtype="${platform.type}" execmethod="${run.method}" securitydomain="${evaluated.run.security.domain}" commandline="${platform.runcommandline}" classpath="${platform.bootclasspath}:${dist.dir}/${dist.jar}" cmdoptions="${run.cmd.options}"/>
</target>

ghasem.fattahpour
پنج شنبه 14 مهر 1390, 19:28 عصر
با احترام
بنده هم قبلا دنبال رمز نگاری های استاندارد در j2me بودم. اما بسیاری از کتابخانه های رمز نپاری در j2me پیاده سازی نشده اند. بهتر است خودتون اون رو پیاده سازی کنید.

handinux
شنبه 16 مهر 1390, 14:01 عصر
می تونید از الگوریتم TEA استفاده کنید که احتیاج به استفاده از bouncy castle سنگین ندارید.برای j2me هم سورس کد داره اگر سرچ کنید