sample code for DES encryption

Hi, Does anyone have the sample code for DES encryption.
[63 byte] By [polasaa] at [2007-11-26 23:39:58]
# 1

Here is the program, but it givs only one eror, If u find that error please post it

program:

--

import javax.microedition.midlet.*;

import javax.microedition.lcdui.*;

import java.security.*;

import java.security.spec.*;

import javax.crypto.*;

import javax.crypto.spec.*;

public class SymmetricEncryption extends MIDlet implements 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);

private static String algorithm = "DES";

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

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

private static String secretKeyAlgorithm = "DES";

private static byte[] iv = "DES".getBytes();

private static byte[] plainText = null;

private Key key = null;

private static Cipher cipher = null;

private static int ciphertextLength = 1024;

private static byte[] cipherText = new byte[ciphertextLength];

public void startApp()

{

form.append(tf);

form.addCommand(okCmd);

form.addCommand(exitCmd);

form.setCommandListener(this);

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

}

public void pauseApp()

{

}

public void destroyApp(boolean unconditional)

{

notifyDestroyed();

}

public void 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();

}

}

public void 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();

}

}

public void decrypt()

{

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

try

{

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

}

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

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

}

catch (Exception ex)

{

ex.printStackTrace();

}

}

}

NelsonJosepha at 2007-7-11 15:06:10 > top of Java-index,Security,Cryptography...
# 2

If that's supposed to help the OP, it doesn't.

If you're looking for help yourself you need (i) your own thread; (ii) to tell us what error or exception you encountered; and (iii) to use the code tags or the Code button above to format your source code: nobody's going to read it like that.

ejpa at 2007-7-11 15:06:10 > top of Java-index,Security,Cryptography...
# 3
Here's an example: http://www.exampledepot.com/egs/javax.crypto/DesString.html
Darkman_dda at 2007-7-11 15:06:10 > top of Java-index,Security,Cryptography...