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();
}
}
}

