Data Encryption Decryption using DES Algorithm in Java

Encryption is process of converting plan text to cypher text using encryption algorithm and encryption Key.
Decryption is reverse process of encryption which recover original data from encrypted data using decryption key.

Here, One should understood Cryptography concept before moving into encryption and description world. It's basically making communication private - Protect Sensitive Information. Refer to wiki for more details.

Types of Cryptography :
  1. Symmetric
  2. ASymmetric
Symmetric Cryptography:
  • Here, Encryption and decryption  parties uses same secret key as private key
  • Using this private Key, they will encrypt or decrypt data.
  • common symmetric algorithm are DES(Data Encryption Standard),3DES,AES(Advance Encryption Standard) 
  • DES accepts 64 bits.
  • 3DES - it works using cascading three instance of DES.
  • AES is advance one. it accepts 128,192,256 bits. - Recommended one

ASymmetric Cryptography:
  • It uses concept of public key and private key
  • We can distribute public key to anyone and using this public key they can encrypt data
  • Encrypted Data only can be decrypted by one who has associated private key
  • Here, Encryption Key and Decryption Key should be different.
Ex. RSA algorithm named after Rivest,Shamir and Adleman.

Please refer to Cipher API Documentation before using.

Java Program for Data Encryption Decryption using DES Algorithm :

package com.anuj.security.encryption;

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;

/**
 * 
 * @author Anuj
 * 
 */
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("Anuj Patel - Classified Information !");

   decryptCipher = Cipher.getInstance("DES/ECB/PKCS5Padding");
   decryptCipher.init(Cipher.DECRYPT_MODE, secretKey);
   decryptData(encryptedData);

  } catch (NoSuchAlgorithmException e) {
   e.printStackTrace();
  } catch (NoSuchPaddingException e) {
   e.printStackTrace();
  } catch (InvalidKeyException e) {
   e.printStackTrace();
  } catch (IllegalBlockSizeException e) {
   e.printStackTrace();
  } catch (BadPaddingException e) {
   e.printStackTrace();
  }

 }

 /**
  * Encrypt Data
  * @param data
  * @return
  * @throws IllegalBlockSizeException
  * @throws BadPaddingException
  */
 private static byte[] encryptData(String data)
   throws IllegalBlockSizeException, BadPaddingException {
  System.out.println("Data Before Encryption :" + data);
  byte[] dataToEncrypt = data.getBytes();
  byte[] encryptedData = encryptCipher.doFinal(dataToEncrypt);
  System.out.println("Encryted Data: " + encryptedData);

  return encryptedData;
 }

 /**
  * Decrypt Data
  * @param data
  * @throws IllegalBlockSizeException
  * @throws BadPaddingException
  */
 private static void decryptData(byte[] data)
   throws IllegalBlockSizeException, BadPaddingException {
  byte[] textDecrypted = decryptCipher.doFinal(data);
  System.out.println("Decryted Data: " + new String(textDecrypted));
 }
}


Here,
DES = Data Encryption Standard.
ECB = Electronic Codebook mode.
PKCS5Padding = PKCS #5-style padding

While initializing Cipher, we can pass Key,Certificate and AlgorithParameters as well.

Output :
Data Before Encryption :Classified Information!
Encryted Data: [B@bc6007
Decryted Data: Classified Information!

2 comments: