4

I have to encrypt/decrypt plain text in java using DES with a key. I have got a very good tutorial at IBM which can be found here . The problem with this example is that it is generating the key in the program itself. Now if I encrypt a string(eg password) and store in database then I would not be able to decrypt it because I would not know the key.

Below is the example at IBM

import java.security.*;
import javax.crypto.*;
//
// encrypt and decrypt using the DES private key algorithm
public class PrivateExample {

  public static void main (String[] args) throws Exception {
  //
  // check args and get plaintext
    if (args.length !=1) {
      System.err.println("Usage: java PrivateExample text");
      System.exit(1);
    }
  byte[] plainText = args[0].getBytes("UTF8");
  //
  // get a DES private key
  System.out.println( "\nStart generating DES key" );
  KeyGenerator keyGen = KeyGenerator.getInstance("DES");
  keyGen.init(56);
  Key key = keyGen.generateKey();
  System.out.println( "Finish generating DES key" );
//
// get a DES cipher object and print the provider
Cipher cipher = Cipher.getInstance("DES/ECB/PKCS5Padding");
System.out.println( "\n" + cipher.getProvider().getInfo() );
//
// encrypt using the key and the plaintext
System.out.println( "\nStart encryption" );
cipher.init(Cipher.ENCRYPT_MODE, key);
byte[] cipherText = cipher.doFinal(plainText);
System.out.println( "Finish encryption: " );
System.out.println( new String(cipherText, "UTF8") );

//
// decrypt the ciphertext using the same key
System.out.println( "\nStart decryption" );
cipher.init(Cipher.DECRYPT_MODE, key);
byte[] newPlainText = cipher.doFinal(cipherText);
System.out.println( "Finish decryption: " );

System.out.println( new String(newPlainText, "UTF8") );
}
}

Can anyone suggest how can I add my own key in this example?

antnewbee
  • 1,779
  • 4
  • 25
  • 38

2 Answers2

1

Make the key one of the args instead of generateKey if you plan to supply the key.

Edit: generateKey generates a random key. It might be simpler to save this key to use for decryption than to add code to parse a key arg. Have a look at KeyGenerator and SecretKey.

jacknad
  • 13,483
  • 40
  • 124
  • 194
  • thanks for the prompt answer but can you be a little more specific please. generateKey does not take any argument ! – antnewbee Dec 11 '12 at 20:05
  • generateKey generates a random key. It might be simpler for you to just save this key to use for decryption than to add code to parse a key arg. – jacknad Dec 11 '12 at 20:22
  • That doesn't seems fine as I will be using that class throughout the application and everytime I restart the server the key will change. – antnewbee Dec 17 '12 at 13:35
  • You will either have to save the generated key to some kind of NV memory in a way that makes sense or input the key as an arg as originally suggested. [Key management](http://en.wikipedia.org/wiki/Key_management) is beyond the scope of this question. – jacknad Dec 17 '12 at 15:08
  • Thanks but that was the question I asked in first comment...how to pass the key as an argument? – antnewbee Dec 19 '12 at 09:37
  • With `main (String[] args)` you would pass the key in as a String to your class. I don't know if there is a better way to explain it. You then of course need to create the DES key from the String. There is a good explanation of how to do that [here](http://stackoverflow.com/a/4985827/398460). – jacknad Dec 21 '12 at 14:31
1

Have a look at SecretKeyFactory and DESKeySpec. These can be used to construct a DES key from the key material (a byte array).

You can get the key material from a DES key using getEncoded().

martijno
  • 1,723
  • 1
  • 23
  • 53