TripeDES In Java?
Here is the class I am using for encryption:
/*
* DES.java
*
* Created on April 14, 2007, 11:51 AM
*
* To change this template, choose Tools | Template Manager
* and open the template in the editor.
*/
package codexcheck;
import com.sun.org.apache.bcel.internal.util.ByteSequence;
import java.util.Random;
import java.util.*;
import java.net.PasswordAuthentication;
import java.security.*;
import java.security.spec.*;
import javax.crypto.*;
import javax.crypto.Cipher;
import javax.crypto.Mac;
import javax.crypto.SecretKey;
import javax.crypto.spec.DESedeKeySpec;
import javax.crypto.spec.SecretKeySpec;
/**
*
* @author Josh
*/
publicclass DES{
/** Creates a new instance of DES */
public DES(){
}
publicstatic SecretKey generateSecretKey(byte[] a)throws InvalidKeyException, NoSuchAlgorithmException, InvalidKeySpecException
{
DESedeKeySpec dks =new DESedeKeySpec(a);
SecretKeyFactory factory = SecretKeyFactory.getInstance("DESede");
return factory.generateSecret(dks);
}
publicstaticbyte[] tripleDESEncrypt(byte[] keyMaterial,byte[] source)
throws InvalidKeyException, NoSuchAlgorithmException,
InvalidKeySpecException, BadPaddingException,
NoSuchPaddingException, IllegalBlockSizeException
{
SecretKey key = generateSecretKey(keyMaterial);
SecureRandom random = SecureRandom.getInstance("SHA1PRNG");
Cipher cipher = Cipher.getInstance("DESede");
cipher.init(Cipher.ENCRYPT_MODE, key, random);
return cipher.doFinal(source);
}
publicstaticbyte[] tripleDESDecrypt(byte[] keyMaterial,byte[] source)
throws InvalidKeyException, NoSuchAlgorithmException,
InvalidKeySpecException, BadPaddingException,
NoSuchPaddingException, IllegalBlockSizeException
{
SecretKey key = generateSecretKey(keyMaterial);
SecureRandom random = SecureRandom.getInstance("SHA1PRNG");
Cipher cipher = Cipher.getInstance("DESede");
cipher.init(Cipher.DECRYPT_MODE, key, random);
return cipher.doFinal(source);
}
}
Here is the sample I am using (Ignore the comments):
/*
* Main.java
*
* Created on April 13, 2007, 8:21 PM
*
* To change this template, choose Tools | Template Manager
* and open the template in the editor.
*/
package codexcheck;
import java.security.*;
import java.util.*;
/**
*
* @author Josh
*/
publicclass Main{
/** Creates a new instance of Main */
public Main(){
}
/**
* @param args the command line arguments
*/
publicstaticvoid main(String[] args){
try{
//MessageDigest md = MessageDigest.getInstance("SHA");
//md.update(getRandom(25).getBytes());
//String tmpPass = md.digest().toString();
//byte[] pass = (tmpPass + getRandom(15)).getBytes();
byte[] pass ="jdjfhyriquw738495js63js2".getBytes();
//SecretKey key = new SecretKeySpec(pass, "DESede");
byte[] source ="My test input string".getBytes();
byte[] result = DES.tripleDESEncrypt(pass, source);
System.out.println(result);
byte[] result1 = DES.tripleDESDecrypt(pass, result);
System.out.println(result1);
}catch (Exception e){
e.printStackTrace();
}
}
publicstatic String getRandom(int length){
UUID uuid = UUID.randomUUID();
String myRandom = uuid.toString();
return myRandom.substring(0, length);
}
}
This is the output I get from running the example:
[B@1eec612
[B@10dd1f7
As you can see it's encrypting, but not decrypting, even though I used the exact same key. Can someone point out to me what I am doing wrong here? Thanks.

