Thursday, December 11, 2008

Simple key encryption/decryption java example

In my internship period, i was assign to a Software Security Module development project. And it has to be developed in java. So in the middle of the development i had to face lot's of programming problems. But finally, i've managed to complete my project. So i thought to share my java programming experiences on using java.Crypto class with people who are in all around the world.

Key encryption/decryption class in java.

The unique feature of this class is the doEncrypt method of this class returns a byte array which has the same length of encrypted key.And the doDecrypt method will decrypt the resulted values of doEncrypt method into it's clear values.Try this, it's very simple.  


import javax.crypto.*;
import javax.crypto.spec.*;

/**
 *
 * @author Sashika Nimantha Perera
 */
public class Crypto {
    
    public static byte[] doEncrypt(byte[] baKey,byte[] baFormattedDataBlock){
        byte encrypted[] = new byte[1];
SecretKey key = null;
Cipher c = null;
        
        // for 8 byte long key
        if(baKey.length == 8){
            // return that Warning in host command manual "02 : Warning PVK not single length"
            try{
                SecretKeyFactory skf = SecretKeyFactory.getInstance("DES");
                // as parameters we can give only algorithm and the provider
                c = Cipher.getInstance("DES/ECB/NoPadding");
                DESKeySpec ks = new DESKeySpec(baKey);

                key = skf.generateSecret(ks);

                c.init(Cipher.ENCRYPT_MODE, key);

                encrypted = c.doFinal(baFormattedDataBlock);
                                      
                }catch(Exception e){
                System.out.println(e.toString());
                }
         
        // for 16 byte long key    
        }else if(baKey.length==16){
            byte[] bArrayForDouble = new byte[24];
            System.arraycopy(baKey,0,bArrayForDouble,0,16);
            System.arraycopy(baKey,0,bArrayForDouble,16,8);
            try{
                SecretKeyFactory skf = SecretKeyFactory.getInstance("DESede");
                // as parameters we can give only algorithm and the provider
                c = Cipher.getInstance("DESede/ECB/NoPadding");
                DESedeKeySpec ks = new DESedeKeySpec(bArrayForDouble);

                key = skf.generateSecret(ks);

                c.init(Cipher.ENCRYPT_MODE, key);

                encrypted = c.doFinal(baFormattedDataBlock);
                
                }catch(Exception e){
                    System.out.println(e.toString());
                }
         
        //for 24 Byte long key    
        }else if (baKey.length==24){
            try{
                SecretKeyFactory skf = SecretKeyFactory.getInstance("DESede");
                // as parameters we can give only algorithm and the provider
                c = Cipher.getInstance("DESede/ECB/NoPadding");
                DESedeKeySpec ks = new DESedeKeySpec(baKey);

                key = skf.generateSecret(ks);

                c.init(Cipher.ENCRYPT_MODE, key);

                encrypted = c.doFinal(baFormattedDataBlock);
                
            }catch(Exception e){
                System.out.println(e.toString());
            }
        }
        return encrypted;
    }
    
    
    public static byte[] doDecrypt(byte[] baKey, byte[] baFormattedDataBlock){
        byte decrypted[] = new byte[1];
SecretKey key = null;
Cipher c = null;
        
        // for 8 byte long key
        if(baKey.length == 8){
            // return that Warning in host command manual "02 : Warning PVK not single length"
            try{
                SecretKeyFactory skf = SecretKeyFactory.getInstance("DES");
                // as parameters we can give only algorithm and the provider
                c = Cipher.getInstance("DES/ECB/NoPadding");
                DESKeySpec ks = new DESKeySpec(baKey);

                key = skf.generateSecret(ks);
                c.init(Cipher.DECRYPT_MODE, key);
                        
                decrypted = c.doFinal(baFormattedDataBlock);
                
                }catch(Exception e){
                    System.out.println("Exception in De8 " + e.toString());
                }
            
        // for 16 byte long key    
        }else if(baKey.length==16){
            byte[] bArrayForDouble = new byte[24];
            System.arraycopy(baKey,0,bArrayForDouble,0,16);
            System.arraycopy(baKey,0,bArrayForDouble,16,8);
            
            try{
                SecretKeyFactory skf = SecretKeyFactory.getInstance("DESede");
                // as parameters we can give only algorithm and the provider
                c = Cipher.getInstance("DESede/ECB/NoPadding");
                DESedeKeySpec ks = new DESedeKeySpec(bArrayForDouble);

                key = skf.generateSecret(ks);

                c.init(Cipher.DECRYPT_MODE, key);

                decrypted = c.doFinal(baFormattedDataBlock);
                        
                }catch(Exception e){
                    System.out.println( e.toString());
                }
        
        //for 24 Byte long key    
        }else if (baKey.length==24){
            try{
                SecretKeyFactory skf = SecretKeyFactory.getInstance("DESede");
                // as parameters we can give only algorithm and the provider
                c = Cipher.getInstance("DESede/ECB/NoPadding");
                DESedeKeySpec ks = new DESedeKeySpec(baKey);

                key = skf.generateSecret(ks);

                c.init(Cipher.DECRYPT_MODE, key);

                decrypted = c.doFinal(baFormattedDataBlock);
                
                }catch(Exception e){
                    System.out.println( e.toString());
                }
        }
        
        return decrypted;
    }
}

7 comments:

  1. Hey..welcome to the blogsphere. Congratulations and good luck in your tech career. Try to keep up in blogging you'll miss it out wit your busy life.

    www.lasithasilva.wordpress.com

    ReplyDelete
  2. love to find blogs on code...waiting for the next pot...

    ReplyDelete
  3. Its nice to see someone such as yourself helping others with your knowledge.

    keep up the good work.

    ReplyDelete
  4. This comment has been removed by the author.

    ReplyDelete
  5. Amazing. I am thankful to you for sharing this code. I copied and executed the program and it worked well. You do have a great knowledge about this scheme.
    esign

    ReplyDelete