I have created simple application to encrypt the string using Cipher class like below:
This code snippet I got from the forums.nokia;
In this The string "THIS IS A SECRET MESSAGE" is encrypted and decrypted using the key "SECRET!!" .
Code:import java.security.InvalidKeyException; import java.security.NoSuchAlgorithmException; import javax.microedition.lcdui.Command; import javax.microedition.lcdui.CommandListener; import javax.microedition.lcdui.Displayable; import javax.microedition.lcdui.Form; import javax.microedition.lcdui.Item; import javax.microedition.lcdui.TextBox; import javax.microedition.lcdui.TextField; import javax.crypto.BadPaddingException; import javax.crypto.Cipher; import javax.crypto.IllegalBlockSizeException; import javax.crypto.NoSuchPaddingException; import javax.crypto.ShortBufferException; import javax.crypto.spec.SecretKeySpec; public class FirstForm extends Form implements CommandListener { TextField txtData=new TextField("Enter the Data","",10,TextField.ANY); Command cmdEncrypt=new Command("Encrypt",Command.SCREEN,1); public FirstForm(String arg0) { super(arg0); this.append(txtData); this.addCommand(cmdEncrypt); this.setCommandListener(this); } public void commandAction(Command cmd, Displayable frm) { if(cmd==this.cmdEncrypt) { System.out.println("Command Is Pressed......."); byte[] msg = "THIS IS A SECRET MESSAGE".getBytes(); byte[] enMsg = new byte[10000]; byte[] deMsg = new byte[10000]; //create new cipher using DES algorithm try { Cipher c = Cipher.getInstance("DES"); //our raw byte[] key - please note that since we use DES algorithm, //the key must be 8 bytes long byte[] b = "SECRET!!".getBytes(); //init the cipher to encrypt the data c.init(Cipher.ENCRYPT_MODE, new SecretKeySpec(b,0,b.length,"DES")); int numBytes = c.doFinal(msg, 0, msg.length, enMsg, 0); System.out.println("Encrypted Message is"+enMsg); //init the cipher to decrypt the data c.init(Cipher.DECRYPT_MODE, new SecretKeySpec(b,0,b.length,"DES")); numBytes = c.doFinal(enMsg, 0, numBytes, deMsg, 0); String s = new String(deMsg,0,numBytes); System.out.println("Decrypted Message is"+s); } catch (InvalidKeyException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (NoSuchAlgorithmException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (NoSuchPaddingException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IllegalStateException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (ShortBufferException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IllegalBlockSizeException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (BadPaddingException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } }
after clicking on command button the Encrypted and decrypted sting is shown on the console
I have tested this code with wtk2.5.2.
This code works fine with sun'sWTK 2.5.2
But when; I have created project with the eclippse and wtk22
1) I added the satsa-crypto.jar in projrct->Properties->java-Builder-Path in "Library tab"
2)I Had also checked the checkbox before satsa-crypto.jar "Order-Of-Export Tab"
I am using the Ant Built
After Building I got Error saying::
What may be the reason of this??Code:[j2mepolish] preverify:Error preverifying class u [j2mepolish] preverify: VERIFIER ERROR u.a(ILm;Lw;)V: [j2mepolish] preverify:Cannot find class com/sun/midp/ssl/Key [j2mepolish] Preverify call failed: 1 [j2mepolish] C:\WTK22\bin\preverify.exe -classpath D:\Java\J2ME-Polish\import\midp-2.0.jar;D:\Java\J2ME-Polish\import\cldc-1.0.jar; -d D:\nitins_workspace\Encrypt\build\real\Generic\midp2\de_DE\obfuscated -nofp -nofinalize -nonative D:\nitins_workspace\Encrypt\build\real\Generic\midp2\de_DE\obfuscated BUILD FAILED

Reply With Quote




