Wednesday, September 21, 2011

DES algorithm Code in Java

Introduction of DES
The Data Encryption Standard (DES) is a symmetric-key algorithm that uses a 56-bit key.Symmetric-key encryption means it uses same key to cipher and decipher the message. DES was developed in IBM under the name of LUCIFER. DES is now considered to be insecure for many applications(especially Govt., defense). For applications demanding more security of the content, Triple-DES can be used.

Coding DES in Java
Code listing below contains the Cryptography class which has both encryption and decryption method.
import java.io.IOException;
import java.security.InvalidKeyException;

import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.SecretKey;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.DESKeySpec;

import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;

/**
 * @author dharmvir.singh 
 * The class demonstrates the DES algorithm by using java
 * crypto API
 */
public class Cryptography {
 private static final String CRYPTOGRAPHY_ALGO_DES = "DES";
 private static Cipher cipher = null;
 private static DESKeySpec keySpec = null;
 private static SecretKeyFactory keyFactory = null;
 
 public static String encrypt(String inputString, String commonKey)
   throws InvalidKeyException, IllegalBlockSizeException,
   BadPaddingException {
  String encryptedValue = null;
  SecretKey key = getSecretKey(commonKey);
  cipher.init(Cipher.ENCRYPT_MODE, key);
  byte[] inputBytes = inputString.getBytes();
  byte[] outputBytes = cipher.doFinal(inputBytes);
  encryptedValue = new BASE64Encoder().encode(outputBytes);
  return encryptedValue;
 }
 public static String decrypt(String encryptedString, String commonKey)
   throws InvalidKeyException, IllegalBlockSizeException,
   BadPaddingException, IOException {
  String decryptedValue = "";
// When Base64Encoded strings are passed in URLs, '+' character gets converted to space and so we need to reconvert the space to '+' and since encoded string cannot have space in it so we are completely safe.
  encryptedString = encryptedString.replace(' ', '+');
  SecretKey key = getSecretKey(commonKey);
  cipher.init(Cipher.DECRYPT_MODE, key);
  byte[] recoveredBytes = cipher.doFinal(new BASE64Decoder()
    .decodeBuffer(encryptedString));
  decryptedValue = new String(recoveredBytes);
  return decryptedValue;
 }
 private static SecretKey getSecretKey(String secretPassword) {
  SecretKey key = null;
  try {
   cipher = Cipher.getInstance(CRYPTOGRAPHY_ALGO_DES);
   keySpec = new DESKeySpec(secretPassword.getBytes("UTF8"));
   keyFactory = SecretKeyFactory.getInstance(CRYPTOGRAPHY_ALGO_DES);
   key = keyFactory.generateSecret(keySpec);
  } catch (Exception e) {
   e.printStackTrace();
   System.out.println("Error in generating the secret Key");
  }
  return key;
 }
}

Download
Code can be downloaded from here
Download contains:
  1. Cryptography.java: Contains the encrption and decryption method
  2. TestCrypto.java

Related Articles
Implementing MD5 in java

Relevant References
DES explained
Wiki Links:
DES, Symmetric-key algorithm, Triple-DES

13 comments:

  1. How to implement OFB mode in java?Should this algorithm is used to implement?

    ReplyDelete
  2. Thanks a Lot for your Program...

    ReplyDelete
  3. Thanks for providing the implementation code in java. I tried to execute the code to testify it and it worked well. I will use it my application.
    electronic signature

    ReplyDelete
  4. Thank You Very Much,, It helped me in my project Enhanced Privacy and Authentication for Cloud

    ReplyDelete
  5. The article you wrote was very helpful for me. I am looking forward to reading more articles of yours in the future. The visa for Turkey is completely open now and you can take benefits of the Turkish visa facility and Explore the Turkey like a local .

    ReplyDelete