Hel me in Encryption

Hi all I am creating a program in j2ME for encryption

It throws an error:

"javax.crypto.BadPaddingException" please help me

My program:

import javax.microedition.midlet.*;

import javax.microedition.lcdui.*;

import java.security.*;

import java.security.spec.*;

import javax.crypto.*;

import javax.crypto.spec.*;

publicclass SymmetricEncryptionextends MIDletimplements CommandListener, Runnable

{

Form form =new Form("MyForm");

TextField tf =new TextField("String","",15,TextField.ANY);

Command okCmd =new Command("Ok",Command.OK,1);

Command exitCmd =new Command("Exit",Command.EXIT,2);

privatestatic String algorithm ="DES/ECB/PKCS5Padding";

privatestaticbyte[] secretKey ={(byte) 0x2b, (byte) 0x7e, (byte) 0x15, (byte) 0x16,

(byte) 0x28, (byte) 0xae, (byte) 0xd2, (byte) 0xa6};

privatestatic String secretKeyAlgorithm ="DES";

privatestaticbyte[] iv ="DES".getBytes();

privatestaticbyte[] plainText =null;

private Key key =null;

privatestatic Cipher cipher =null;

privatestaticint ciphertextLength = 512;

privatestaticbyte[] cipherText =newbyte[ciphertextLength];

publicvoid startApp()

{

form.append(tf);

form.addCommand(okCmd);

form.addCommand(exitCmd);

form.setCommandListener(this);

Display.getDisplay(this).setCurrent(form);

}

publicvoid pauseApp()

{

}

publicvoid destroyApp(boolean unconditional)

{

notifyDestroyed();

}

publicvoid commandAction(Command c, Displayable d)

{

if (c == okCmd)

{

String input = tf.getString();

plainText = input.getBytes();

Thread t =new Thread(this);

t.start();

}

if (c == exitCmd)

{

notifyDestroyed();

}

}

publicvoid run()

{

try

{

key =new SecretKeySpec(secretKey,0,secretKey.length,secretKeyAlgorithm);

cipher = Cipher.getInstance(algorithm);

if (iv ==null)

{

System.out.println("Key: "+key);

cipher.init(Cipher.ENCRYPT_MODE, key);

}

else

{

System.out.println("Key: "+key);

IvParameterSpec ivps =new IvParameterSpec(iv, 0, iv.length);

System.out.println("IV:"+new String(ivps.getIV()));

cipher.init(Cipher.ENCRYPT_MODE, key,ivps);

}

cipher.doFinal(plainText, 0, plainText.length, cipherText, 0);

System.out.println("Plain Text :"+new String(plainText));

System.out.println("Cipher Text :"+cipherText);

decrypt();

}

catch (Exception e)

{

e.printStackTrace();

}

}

publicvoid decrypt()

{

byte[] decrypted =null;

System.out.println("Inside decrypt()");

try

{

cipher = Cipher.getInstance(algorithm);

if (iv ==null)

{

System.out.println("Key: "+key);

cipher.init(Cipher. DECRYPT_MODE, key);

}

else

{

System.out.println("Key: "+key);

IvParameterSpec ivps =new IvParameterSpec(iv, 0, iv.length);

System.out.println("IV:"+new String(ivps.getIV()));

cipher.init(Cipher.DECRYPT_MODE,key,ivps);

}

decrypted =newbyte[512];

System.out.println("Before doFinal()");

cipher.doFinal(cipherText,0,cipherText.length,decrypted,0);

System.out.println("Recovered Plain Text :"+new String(decrypted));

}

catch (Exception ex)

{

ex.printStackTrace();

}

}

}

[7944 byte] By [NelsonJosepha] at [2007-11-26 23:41:29]
# 1

You are ignoring the the return value from your encryption doFinal() method. This tells you how many bytes are in your encrypted output and therefore now many to decrypt.

You would do better using the public final byte[] doFinal(byte[] input)

method where all the lengths are calculated for you. This would remove about 60% of your code.

sabre150a at 2007-7-11 15:09:09 > top of Java-index,Security,Cryptography...
# 2

But that method is not available in J2ME SATSA

only one doFinal() method is available

public final int doFinal(byte[] input, int inputOffset, int inputLen, byte[] output,

int outputOffset)

throws IllegalStateException, ShortBufferException, IllegalBlockSizeExcepti

on, BadPaddingException

NelsonJosepha at 2007-7-11 15:09:09 > top of Java-index,Security,Cryptography...
# 3

> But that method is not available in J2ME SATSA

> only one doFinal() method is available

OK then make sure you use the return value!

P.S. If it exists in J2ME you might want to use the Cipher method

public final int getOutputSize(int inputLen)

so that you know how big to make the arrays needed for your encrypted data. Make sure you read the Javadoc for this method.

Message was edited by:

sabre150

sabre150a at 2007-7-11 15:09:09 > top of Java-index,Security,Cryptography...
# 4
Thank you for u r suggestion, But that method is not available in J2ME Only limitted amount of APIs are available. Please help me
NelsonJosepha at 2007-7-11 15:09:09 > top of Java-index,Security,Cryptography...
# 5

> Thank you for u r suggestion, But that method is not

> available in J2ME

:-(

> Only limitted amount of APIs are

> available.

Yep.

> Please help me

I thought I had done! You need to use the returned value from the doFinal() method so that you know how many encrypted bytes there are and how many you need to decrypt.

sabre150a at 2007-7-11 15:09:09 > top of Java-index,Security,Cryptography...