moneymama
asked on
DES Algorithm Example needed
Hello All,
I need to encrypt an Object using DES. Please let me know is this possible if it is possible with java jdk tool kit
thanks
moneymama
I need to encrypt an Object using DES. Please let me know is this possible if it is possible with java jdk tool kit
thanks
moneymama
use javax.cryto package
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
To use the crypto package you must have JCE.
This is contained automatically with jdk1.4 (and greater).
This is downloadable from www.java.sun.com for previous jdk (see java Sun and JCE package)
If you want to build your own the implementation of DES algorithm you must study it before all and you can even not use the crypto package of JCE.
Bye, Giant.
This is contained automatically with jdk1.4 (and greater).
This is downloadable from www.java.sun.com for previous jdk (see java Sun and JCE package)
If you want to build your own the implementation of DES algorithm you must study it before all and you can even not use the crypto package of JCE.
Bye, Giant.
here's an example, i hope it helps...i had to implement it from scratch for an assigment...but using the javax package is so much more simpler, so use what is available to you.
//EXAMPLE
import javax.crypto.KeyGenerator;
import javax.crypto.SecretKey;
import javax.crypto.spec.IvParame terSpec;
import javax.crypto.Cipher;
public class SymmetricCipherTest {
private static byte[] iv =
{ 0x0a, 0x01, 0x02, 0x03, 0x04, 0x0b, 0x0c, 0x0d };
private static byte[] encrypt(byte[] inpBytes,
SecretKey key, String xform) throws Exception {
Cipher cipher = Cipher.getInstance(xform);
IvParameterSpec ips = new IvParameterSpec(iv);
cipher.init(Cipher.ENCRYPT _MODE, key, ips);
return cipher.doFinal(inpBytes);
}
private static byte[] decrypt(byte[] inpBytes,
SecretKey key, String xform) throws Exception {
Cipher cipher = Cipher.getInstance(xform);
IvParameterSpec ips = new IvParameterSpec(iv);
cipher.init(Cipher.DECRYPT _MODE, key, ips);
return cipher.doFinal(inpBytes);
}
public static void main(String[] unused) throws Exception {
String xform = "DES/ECB/PKCS5Padding";
// Generate a secret key
KeyGenerator kg = KeyGenerator.getInstance(" DES");
kg.init(56); // 56 is the keysize. Fixed for DES
SecretKey key = kg.generateKey();
byte[] dataBytes =
"J2EE Security for Servlets, EJBs and Web Services".getBytes();
byte[] encBytes = encrypt(dataBytes, key, xform);
byte[] decBytes = decrypt(encBytes, key, xform);
boolean expected = java.util.Arrays.equals(da taBytes, decBytes);
System.out.println("Test " + (expected ? "SUCCEEDED!" : "FAILED!"));
}
}
//EXAMPLE
import javax.crypto.KeyGenerator;
import javax.crypto.SecretKey;
import javax.crypto.spec.IvParame
import javax.crypto.Cipher;
public class SymmetricCipherTest {
private static byte[] iv =
{ 0x0a, 0x01, 0x02, 0x03, 0x04, 0x0b, 0x0c, 0x0d };
private static byte[] encrypt(byte[] inpBytes,
SecretKey key, String xform) throws Exception {
Cipher cipher = Cipher.getInstance(xform);
IvParameterSpec ips = new IvParameterSpec(iv);
cipher.init(Cipher.ENCRYPT
return cipher.doFinal(inpBytes);
}
private static byte[] decrypt(byte[] inpBytes,
SecretKey key, String xform) throws Exception {
Cipher cipher = Cipher.getInstance(xform);
IvParameterSpec ips = new IvParameterSpec(iv);
cipher.init(Cipher.DECRYPT
return cipher.doFinal(inpBytes);
}
public static void main(String[] unused) throws Exception {
String xform = "DES/ECB/PKCS5Padding";
// Generate a secret key
KeyGenerator kg = KeyGenerator.getInstance("
kg.init(56); // 56 is the keysize. Fixed for DES
SecretKey key = kg.generateKey();
byte[] dataBytes =
"J2EE Security for Servlets, EJBs and Web Services".getBytes();
byte[] encBytes = encrypt(dataBytes, key, xform);
byte[] decBytes = decrypt(encBytes, key, xform);
boolean expected = java.util.Arrays.equals(da
System.out.println("Test " + (expected ? "SUCCEEDED!" : "FAILED!"));
}
}
ASKER
thanks for ur inputs.
Basically I need to encrypt and decrypt the objects.
thanks
Basically I need to encrypt and decrypt the objects.
thanks