Java - Data Encryption Standard (DES) Algorithm

/*DESAlgorithm.java*/ 

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 dhanoopbhaskar
 */
public class DESAlgorithm {
   
    KeyGenerator keyGenerator = null;
    SecretKey secretKey = null;
    Cipher cipher = null;
   
    public DESAlgorithm() {
        try {
            /**
             * Create a DES key
             */
            keyGenerator = KeyGenerator.getInstance("DES");
            secretKey = keyGenerator.generateKey();
           
            /**
             * Create an instance of cipher providing the following info
             *
separated by slash.
             *
             *      - Algorithm name
             *      - Mode (optional)
             *      - Padding scheme (optional)
             *
             * NB:
             *      DES = Data Encryption Standard.
             *      ECB = Electronic Codebook mode.
             *      PKCS5Padding = PKCS #5-style padding.
             */
            cipher = Cipher.getInstance("DES/ECB/PKCS5Padding");
        } catch (NoSuchPaddingException ex) {
            System.out.println(ex);
        } catch (NoSuchAlgorithmException ex) {
            System.out.println(ex);
        }
    }
   
    /**
     *
     * @param plainText
     * @return cipherText
     */
    public byte[] encryptText(String plainText) {
        byte[] cipherText = null;
        try {
            /**
             * Initialize the cipher for encryption
             */
            cipher.init(Cipher.ENCRYPT_MODE, secretKey);
            /**
             * Convert the text string to byte format
             */
            byte[] plainBytes = plainText.getBytes();
            /**
             * Perform encryption with method doFinal()
             */
            cipherText = cipher.doFinal(plainBytes);           
        } catch (IllegalBlockSizeException ex) {
            System.out.println(ex);
        } catch (BadPaddingException ex) {
            System.out.println(ex);
        } catch (InvalidKeyException ex) {
            System.out.println(ex);
        }
       
        return cipherText;
    }
   
    /**
     *
     * @param cipherText
     * @return plainText
     */
    public String decryptText(byte[] cipherText) {
        String plainText = null;
        try {
            /**
             * Initialize the cipher for decryption
             */
            cipher.init(Cipher.DECRYPT_MODE, secretKey);           
            /**
             * Perform decryption with method doFinal()
             */
            byte[] plainBytes = cipher.doFinal(cipherText);
            /**
             * Convert encrypted text to string format
             */
            plainText = new String(plainBytes);
        } catch (IllegalBlockSizeException ex) {
            System.out.println(ex);
        } catch (BadPaddingException ex) {
            System.out.println(ex);
        } catch (InvalidKeyException ex) {
            System.out.println(ex);
        }
       
        return plainText;
    }
   
    public static void main(String[] args) {
        DESAlgorithm desAlgorithm = new DESAlgorithm();
        String textToEncrypt = "DES Algorithm";
        System.out.println("Text before Encryption: " + textToEncrypt);
        byte[] cipherText = desAlgorithm.encryptText(textToEncrypt);
        System.out.println("Cipher Text[bytes]: " + cipherText);       
        String textDecrypted = desAlgorithm.decryptText(cipherText);
        System.out.println("Text after Decryption: " + textDecrypted);
    }
}

Post a Comment

0 Comments