Oct 5, 2012

Data Encryption in Java Part 2

DES Cryptography
This is another Data Encryption example in Java, but it featured DES encryption algorithm which is academically popular and has been in use for quite some time. DES uses 56-bits key size and also uses a key to customize the transformation, so that decryption can only be performed by those who know the particular key used for the encryption. Every 8th bit of the selected key is discarded, that is, positions 8, 16, 24, 32, 40, 48, 56, 64 are removed from the 64 bit key leaving behind only the 56 bit key.

 In this example I used the UTF-8 variable-width encoding for the supplied data, which was encoded at its raw bytes state before its encoded in Base64 encoding scheme to make sure our text remained intact during the transport.
package codecypherprojects;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import javax.crypto.*;
import javax.swing.JOptionPane;

/**
 *
 * @author Khamis
 */

public class DESDataEncryption {
    
    private static String DES_key = "DES";
    private static Cipher enc_cipher;
    private static Cipher dec_cipher;
    private static KeyGenerator k_generator;
    private static SecretKey secret_key;
    
    
       public static void main(String args[]) throws NoSuchAlgorithmException{
        try {
            try {
                execute_data_encryption();
            } catch (IllegalBlockSizeException ex) {
                System.out.println("Error! " + ex.getMessage());
            } catch (BadPaddingException ex) {
                System.out.println("Error! " + ex.getMessage());
            }
        } catch (InvalidKeyException ex) {
            System.out.println("Error! " + ex.getMessage());
        }
    }

    private static String decrypt_data(String encrypted_text) 
            throws InvalidKeyException, IOException, 
              IllegalBlockSizeException, BadPaddingException {
        
        String dencrypted = null;  
       
        //Initialize cipher for decryption with same key
        dec_cipher.init(Cipher.DECRYPT_MODE, secret_key);
        
            
        //Do the decryption using Base64 decoder
        byte[] dec = new sun.misc.BASE64Decoder().decodeBuffer(encrypted_text);
        
        
        byte[] dec_bytes = dec_cipher.doFinal(dec);
        
        //wrap the decrypted data in UTF-8 
        dencrypted =  new String(dec_bytes, "UTF8");
           
        
         return dencrypted;
    }


    private static String encrypt_data(String plain_text) throws
                InvalidKeyException, UnsupportedEncodingException {
        
         String encrypted = null;        
         byte[] enc;
         byte[] enc_bytes;
         
        try {
            //initialize the cipher  for decription
            enc_cipher.init(Cipher.ENCRYPT_MODE, secret_key);
            
            // set the UTF-8  variable-width encoding
            enc = plain_text.getBytes("UTF8");
            
            //Do the encryption 
            enc_bytes = enc_cipher.doFinal(enc);
            
            //encode the bytes in Base64 encoder
            encrypted = new sun.misc.BASE64Encoder().encode(enc_bytes);
            
        } catch (IllegalBlockSizeException | BadPaddingException ex) {
            System.out.println("Error! " + ex.getMessage());
        }
        return encrypted;
    }

    private static void execute_data_encryption() 
            throws NoSuchAlgorithmException, InvalidKeyException,
                    IllegalBlockSizeException, BadPaddingException {
        
               try {
            //Generate encryption key based on the DES algorithm
            k_generator = KeyGenerator.getInstance(DES_key);
            
            //Create a secret key out of it
            secret_key = k_generator.generateKey();
            
            //Create DES encryption key  instance 
            enc_cipher = Cipher.getInstance(DES_key);
            
            //Create DES decryption key  instance 
            dec_cipher = Cipher.getInstance(DES_key);
            
            String plain_text = JOptionPane.showInputDialog(
                    "Type in the text to be encrypted");
                                
                      
            String encrypted_data = null;
            try {
                
                encrypted_data = encrypt_data(plain_text);
                
            } catch (UnsupportedEncodingException ex) {
                System.out.println("Error! " + ex.getMessage());
            }
            
            String decrypted_data = null;
            try {
                
                decrypted_data = decrypt_data(encrypted_data);
                
            } catch (IOException ex) {
                System.out.println("Error! " + ex.getMessage());
            }
            
            //Show dialog with the result summary 
            JOptionPane.showMessageDialog(JOptionPane.getRootFrame(), 
                    "The encrypted data is: "
                    + encrypted_data + "\n" + "Dectrypted data is : " 
                    + decrypted_data,
                    "Encryption/Decryption Summary",  JOptionPane.PLAIN_MESSAGE);
            
            System.exit(0);
           
           
        } catch (NoSuchPaddingException ex) {
            System.out.println("Error! " + ex.getMessage());
        }
    }
}

Related Posts 


Data Encryption in Java Part 1

2 comments:

  1. I followed both the articles to have a complete understanding of data encryption algorithm. I really appreciate you for providing the complete source program that implements data encryption.
    electronic signature

    ReplyDelete
  2. This particular papers fabulous, and My spouse and i enjoy each of the perform that you have placed into this. I’m sure that you will be making a really useful place. I has been additionally pleased. Good perform! privacycritic

    ReplyDelete