4

I have a working code example that uses DES (see below), but I want to specify the key data to use. How can I edit the code sample to do this?

import java.security.InvalidKeyException;  
import java.security.NoSuchAlgorithmException;    
import javax.crypto.BadPaddingException;  
import javax.crypto.Cipher;  
import javax.crypto.IllegalBlockSizeException;  
import javax.crypto.KeyGenerator;  
import javax.crypto.NoSuchPaddingException;  
import javax.crypto.SecretKey;

public class DESEncryptionDecryption {

private static Cipher encryptCipher;  
private static Cipher decryptCipher; 
public static void main(String[] args) {  
try {
KeyGenerator keygenerator = KeyGenerator.getInstance("DES");  
SecretKey secretKey = keygenerator.generateKey();  

encryptCipher = Cipher.getInstance("DES/ECB/PKCS5Padding");  
encryptCipher.init(Cipher.ENCRYPT_MODE, secretKey);  
byte[] encryptedData = encryptData("Classified Information!");  

decryptCipher = Cipher.getInstance("DES/ECB/PKCS5Padding");  
decryptCipher.init(Cipher.DECRYPT_MODE, secretKey);  
decryptData(encryptedData);
}}}
Duncan Jones
  • 67,400
  • 29
  • 193
  • 254
nitinkt
  • 67
  • 1
  • 3
  • 7

1 Answers1

5

Do not use a KeyGenerator, use a SecretKeyFactory:

String desKey = "0123456789abcdef"; // value from user  
byte[] keyBytes = DatatypeConverter.parseHexBinary(desKey);

SecretKeyFactory factory = SecretKeyFactory.getInstance("DES");
SecretKey key = factory.generateSecret(new DESKeySpec(keyBytes));

Note that DES is not a secure algorithm. Consider using AES or Triple DES (DESede).


For DESede:

String desKey = "0123456789abcdef0123456789abcdef0123456789abcdef"; // user value (24 bytes)  
byte[] keyBytes = DatatypeConverter.parseHexBinary(desKey);

SecretKeyFactory factory = SecretKeyFactory.getInstance("DESede");
SecretKey key = factory.generateSecret(new DESedeKeySpec(keyBytes));

For AES, just do:

String aesKey = "0123456789abcdef0123456789abcdef"; // user value (16/24/32 bytes)
byte[] keyBytes = DatatypeConverter.parseHexBinary(aesKey);
SecretKey key = new SecretKeySpec(keyBytes, "AES"); 
Duncan Jones
  • 67,400
  • 29
  • 193
  • 254
  • for AES or Triple DES (DESede), is i just have to replace DES With AES/DESede in this line? no other alteration needed? `SecretKeyFactory factory = SecretKeyFactory.getInstance("DES");` – nitinkt Apr 07 '14 at 05:34
  • SecretKeyFactory working good as per your code. thanks for it. – nitinkt Apr 07 '14 at 07:08
  • @nitinkt To use DESede, change the call to `getInstance("DESede")` and also change the key spec (`DESedeKeySpec`). For AES, don't bother with a `SecretKeyFactory`, just do `SecretKey aesKey = new SecretKeySpec(byteValue, "AES");`. – Duncan Jones Apr 07 '14 at 07:18
  • What is the key length for DESede? as it is showing Exception `java.security.InvalidKeyException: Wrong key size`. And for AES, i am not able to understand what exactly to edit for AES. Can u elaborate more? Sorry to bother you, but i am working on a project where all three algorithms need to be implemented for user choice and i don't have any idea of these java classes. – nitinkt Apr 07 '14 at 21:10
  • @nitinkt See edits above. That should be enough to get you going. If you hit any errors, do some research and I'm sure you'll find many, many answers already. – Duncan Jones Apr 08 '14 at 07:18