Free Online Articles Directory
18.11.2008 Sign In Register Hello Guest
Email:
Password:
Remember Me 
forgot your password?


Symmetric Cryptography in Java

Author: Debadatta Mishra Author Ranking Blue | Posted: 23-08-2008 | Comments: 0 | Views: 156 | Rating:  (126) Article Popularity - Blue (?) Got a Question? Ask.
Sign Up Now!

Symmetric cryptography in Java

                                               

Introduction

As you know data security is a significant aspect of any enterprise application. Starting from password encryption to any data exchange, cryptography has been in use for all kinds of purpose. However there are two kinds of cryptography, one is symmetric and another is asymmetric. In case of symmetric cryptography, a secret key is used for all kinds of encryption and decryption. This secret key is shared by all the members who want to participate in the encryption and decryption process. There are several algorithms for the symmetric cryptography. DES( Data Encryption Standard) is one of them. In case of normal login screen of web based application, implementation of DES algorithm is used for password encryption.

 

Technicalities

Java cryptography provides suitable and flexible framework for the use of symmetric cryptography. DES algorithm is commonly known as symmetric algorithm. In case of symmetric cryptography, the secret key is used in a plain text file and this file is shared between the members. This file is used to retrieve the secret key for all kinds of encryption and decryption. In this regards I can provide you an example that think of a situation where the two lovers receive their love letters in encrypted format so that no body can read it. They have a common key called the DES secret key. They use the secret key to decrypt the contents of the encrypted love letters at their end. It means that the secret key is generated once and shared between the members. For more details of DES algorithm please refer to the java docs and JCE framework provided by Sun. In this article I will provide an example of how to use the implementation of DES algorithm.

 

Complete example

 

The following is the utility class for the cryptography management. The class name is

“KeyUtil.java”.

 

package com.dds.core;

 

import java.io.FileInputStream;

import java.io.FileOutputStream;

import java.io.InputStream;

import java.io.OutputStream;

import javax.crypto.Cipher;

import javax.crypto.CipherInputStream;

import javax.crypto.CipherOutputStream;

import javax.crypto.KeyGenerator;

import javax.crypto.SecretKey;

import javax.crypto.spec.SecretKeySpec;

 

/**This is a utility class for all kinds

 * of useful methods related to symmetric

 * cryptography. This class only provides

 * the use of DES algorithm.You can use any

 * other symmetric algorithm similarly.

 * @author Debadatta Mishra( PIKU )

 *

 */

public class KeyUtil {

      /**

       * Name of the algorithm

       */

      private static String algorithm = "DES";

 

      /**This method is used to obtain the

       * Secret key as a String. It is useful

       * you can store the String in a file

       * for all kinds of encryption and

       * decryption.

       * @return the SecretKey as String

       */

      public static String generateSecretKey() {

            String secretKeyString = null;

            try {

                  KeyGenerator keyGen = KeyGenerator.getInstance(algorithm);

                  SecretKey secretKey = keyGen.generateKey();

                  byte[] secretKeyBytes = secretKey.getEncoded();

                  secretKeyString = new sun.misc.BASE64Encoder()

                  .encode(secretKeyBytes);

            } catch (Exception e) {

                  e.printStackTrace();

            }

            return secretKeyString;

      }

 

      /**This method is used to obtain the SecretKey

       * object by passing the secret key as string.

       * @param secretKeyString of type String

       * @return object of {@link SecretKey}

       */

      private static SecretKey getKeyInstance(String secretKeyString) {

            SecretKey secretKey = null;

            try {

                  byte[] b2 = new sun.misc.BASE64Decoder()

                  .decodeBuffer(secretKeyString);

                  secretKey = new SecretKeySpec(b2, algorithm);

            } catch (Exception e) {

                  e.printStackTrace();

            }

            return secretKey;

      }

 

      /**This method is used to encrypt the same file.

       * This method is useful when you are encrypting

       * the contents of a file. There is no need to

       * create another file with the encrypted contents.

       * @param filePath of type String indicating the path of the file

       * @param keyString of type String indicating the secret key

       */

      public static void encryptFile(String filePath, String keyString) {

            try {

                  SecretKey key = getKeyInstance(keyString);

                  Cipher ecipher = Cipher.getInstance(algorithm);

                  ecipher.init(Cipher.ENCRYPT_MODE, key);

                  InputStream in = new FileInputStream(filePath);

                  byte[] fileBytes = new byte[in.available()];

                  in.read(fileBytes);

                  in.close();

                  OutputStream out = new FileOutputStream(filePath);

                  out = new CipherOutputStream(out, ecipher);

                  out.write(fileBytes);

                  out.close();

            } catch (Exception e) {

                  e.printStackTrace();

            }

      }

 

      /**This method is used to decrypt the same file.

       * This method is useful when you are decrypting

       * the contents of a file. There is no need to

       * create another file with the decrypted contents.

       * @param filePath of type String indicating the path of the file

       * @param keyString of type String indicating the secret key

       */

      public static void decryptFile(String filePath, String keyString) {

            try {

                  SecretKey key = getKeyInstance(keyString);

                  Cipher ecipher = Cipher.getInstance(algorithm);

                  ecipher.init(Cipher.DECRYPT_MODE, key);

                  InputStream in = new FileInputStream(filePath);

                  byte[] fileBytes = new byte[in.available()];

                  in.read(fileBytes);

                  in.close();

                  OutputStream out = new FileOutputStream(filePath);

                  out = new CipherOutputStream(out, ecipher);

                  out.write(fileBytes);

                  out.close();

 

            } catch (Exception e) {

                  e.printStackTrace();

            }

      }

 

      /**This method is used to encrypt the contents

       * of a file and writing to another file. This

       * method is useful when you want to maintain the

       * original contents and encrypting the contents

       * and giving to another person.

       * @param in of type {@link InputStream}

       * @param out of type {@link OutputStream}

       * @param keyString of type String indicating the SecretKey

       */

      public static void encryptFile(InputStream in, OutputStream out,

                  String keyString) {

 

            byte[] buf = new byte[1024];

            try {

                  SecretKey key = getKeyInstance(keyString);

                  Cipher ecipher = Cipher.getInstance(algorithm);

                  ecipher.init(Cipher.ENCRYPT_MODE, key);

 

                  // Bytes written to out will be encrypted

                  out = new CipherOutputStream(out, ecipher);

                  // Read in the cleartext bytes and write to out to encrypt

                  int numRead = 0;

                  while ((numRead = in.read(buf)) >= 0) {

                        out.write(buf, 0, numRead);

                  }

                  out.close();

            } catch (Exception e) {

                  e.printStackTrace();

            }

      }

 

      /**This method is used to decrypt the contents

       * of a file and writing to another file. This

       * method is useful when you want to decrypt

       * the contents and writing to a another file.

       * @param in of type {@link InputStream}

       * @param out of type {@link OutputStream}

       * @param keyString of type String indicating the SecretKey

       */

      public static void decryptFile(InputStream in, OutputStream out,

                  String keyString) {

            byte[] buf = new byte[1024];

            try {

                  SecretKey key = getKeyInstance(keyString);

                  Cipher dcipher = Cipher.getInstance(algorithm);

                  dcipher.init(Cipher.DECRYPT_MODE, key);

                  in = new CipherInputStream(in, dcipher);

                  int numRead = 0;

                  while ((numRead = in.read(buf)) >= 0) {

                        out.write(buf, 0, numRead);

                  }

                  out.close();

            } catch (Exception e) {

                  e.printStackTrace();

            }

      }

 

      /**This method is used to encrypt the String.

       * @param contents of type String

       * @param keyString of type String indicating the Secret key as String

       * @return a String encrypted contents

       */

      public static String getEncryptedContents(String contents, String keyString) {

            String encryptedString = null;

            try {

                  byte[] contentBytes = contents.getBytes();

                  SecretKey key = getKeyInstance(keyString);

                  Cipher ecipher = Cipher.getInstance(algorithm);

                  ecipher.init(Cipher.ENCRYPT_MODE, key);

                  byte[] encryptedBytes = ecipher.doFinal(contentBytes);

                  encryptedString = new sun.misc.BASE64Encoder()

                  .encode(encryptedBytes);

            } catch (Exception e) {

                  e.printStackTrace();

            }

            return encryptedString;

      }

 

      /**This method is used to decrypt the String.

       * @param contents of type String

       * @param keyString of type String indicating the Secret key as String

       * @return a String encrypted contents

       */

      public static String getDecryptedContents(String contents, String keyString) {

            String decryptedString = null;

            try {

                  byte[] contentBytes = new sun.misc.BASE64Decoder()

                  .decodeBuffer(contents);

                  SecretKey key = getKeyInstance(keyString);

                  Cipher ecipher = Cipher.getInstance(algorithm);

                  ecipher.init(Cipher.DECRYPT_MODE, key);

                  byte[] encryptedBytes = ecipher.doFinal(contentBytes);

                  decryptedString = new String(encryptedBytes);

            } catch (Exception e) {

                  e.printStackTrace();

            }

            return decryptedString;

      }

}

 

The above class provides several useful methods for encryption and decryption. Please refer the java docs provided for each method. Generally in many applications, you may have to use either for a file or for a String. In the above class , I have provided two methods for file encryption and decryption. In certain situation, it is required that you have to encrypt the contents of the file. More specifically I can provide you an example, think about a file called “sample.txt”. You have to encrypt the contents of the file “Sample.txt” so that after encryption when you are opening the file, it becomes unreadable. It may also be required to transfer the file to network. Sometime a requirement comes that there is file called “Sample.txt”, you have to encrypt the contents of the file and to create a new file called “Sample_en.txt”. In this case you have two files, one is the original one as well as the encrypted file. Now refer to the following subordinate classes for the use.

 

Class name: KeyGenerator.java

 

package com.dds.core;

 

import java.io.File;

import java.io.FileOutputStream;

import java.io.OutputStream;

import java.util.Properties;

 

/**

 * This class is used to generate the secrete key and

 * stores the secret key in a file called secret.key.

 * @author Debadatta Mishra(PIKU)

 *

 */

public class KeyGenerator {

     /**This method is used to obtain the

      * path of the file secret.key.

      * @return path of Key

      */

     private static String getKeyFilePath() {

          String keyPath = null;

          try {

              String keyDirPath = System.getProperty("user.dir") + File.separator

              + "key";

              File keyDir = new File(keyDirPath);

              if (!keyDir.exists())

                   keyDir.mkdirs();

              keyPath = keyDirPath + File.separator + "secretkey.key";

          } catch (Exception e) {

              e.printStackTrace();

          }

          return keyPath;

     }

 

     /**

      * This method is used to store the

      * secret key in a file.

      */

     public static void storeKey() {

          try {

              Properties keyProp = new Properties();

              OutputStream out = new FileOutputStream(getKeyFilePath());

              keyProp.put("key", KeyUtil.generateSecretKey());

              keyProp

              .store(out,

                        "Secret key information, do not modify the key.");

          } catch (Exception e) {

              e.printStackTrace();

          }

 

     }

}

This class is used to generate the Secret key and to store in a file so that the secret key can be retrieved as and when required. Let us see another class where you can read the secret key file to get the secret key.

 

Class name: KeyReader.java

 

package com.dds.core;

 

import java.io.File;

import java.io.FileInputStream;

import java.io.InputStream;

import java.util.Properties;

 

/**This is a utility class to read the

 * contents of the secret.key file.

 * @author Debadatta Mishra(PIKU)

 *

 */

public class KeyReader {

     /**This method is used to obtain the

      * key String which is stored in the

      * file secret.key.

      * @return the key String

      */

     public static String getSecretKey() {

          String secretKeyString = null;

          try {

              String keyDirPath = System.getProperty("user.dir") + File.separator

              + "key";

              String keyPath = keyDirPath + File.separator + "secretkey.key";

              Properties keyProp = new Properties();

              InputStream in = new FileInputStream(keyPath);

              keyProp.load(in);

              secretKeyString = keyProp.getProperty("key");

          } catch (Exception e) {

              e.printStackTrace();

          }

          return secretKeyString;

     }

}

 

The above class is used to read the secret key.

 

I provide below all the test harness classes for the above classes. The first test harness class is “KeyGeneratorTest.java”.

 

import com.dds.core.KeyGenerator;

 

/**This is a test harness class to generate

 * the secret key and store in a file.

 * @author Debadatta Mishra(PIKU)

 *

 */

public class KeyGeneratorTest {

 

      public static void main(String[] args) {

            KeyGenerator.storeKey();

      }

 

}

 

This class is used to generate the secret key.

The next test harness class is “StringEncryptionTest.java”.

 

import com.dds.core.KeyReader;

import com.dds.core.KeyUtil;

 

/**This test harness class is used to

 * encrypt and decrypt the String.

 * @author Debadatta Mishra(PIKU)

 *

 */

public class StringEncryptionTest {

     public static void main(String[] args) {

          String originalString = "Hello World";

          String secretKeyString = KeyReader.getSecretKey();

          String encryptedStringContents = KeyUtil.getEncryptedContents(

                   originalString, secretKeyString);

          System.out.println("Original String------" + originalString);

          System.out.println("Encrypted String-----" + encryptedStringContents);

          /*

           * Now get back to your originalString by decryption

           */

          String decryptedString = KeyUtil.getDecryptedContents(

                   encryptedStringContents, secretKeyString);

          System.out.println("Decrypted String-----" + decryptedString);

     }

}

 

The above class is used to encrypt and decrypt a String. It may be useful for for your password and some other String of characters. Let us see the class for file encryption.

 

Class Name :  FileEncryptionTest.java

 

import java.io.FileInputStream;

import java.io.FileOutputStream;

import java.io.InputStream;

import java.io.OutputStream;

 

import com.dds.core.KeyReader;

import com.dds.core.KeyUtil;

 

/**This is the test harness class for the

 * encryption and decryption of the file.

 * @author Debadatta Mishra(PIKU)

 *

 */

public class FileEncryptionTest {

     

      public static void main(String[] args) throws Exception {

            String filePath = "C:/output.txt";

            String secretKeyString = KeyReader.getSecretKey();

            /*

             * Encrypt the fileContents and write to the same file

             */

            KeyUtil.encryptFile(filePath, secretKeyString);

            /*

             * Decrypt the file contents and write to the same file

             */

            KeyUtil.decryptFile(filePath, secretKeyString);

            /*

             * Encrypt the file contents and write to another

             * file which contains the encrypted contents.

             */

            String encryptedFilePath = "C:/en.txt";

            InputStream in = new FileInputStream(filePath);

            OutputStream out = new FileOutputStream(encryptedFilePath);

            KeyUtil.encryptFile(in, out, secretKeyString);

            /*

             * Decrypt the file contents and write to an another file.

             */

            in = new FileInputStream(encryptedFilePath);

            out = new FileOutputStream(filePath);

            KeyUtil.decryptFile(in, out, secretKeyString);

      }

}

 

The above class provides several ways you can encrypt and decrypt the file.

 

Please follow the following sequence to execute the above classes.

 




  1. Please create the appropriate package structure and copy the corresponding classes inside the package.





  2. First run the class “KeyGeneratorTest.java” . You will find a file called “secret.key” where the secret key is tored.





  3. Next run the class “StringEncryptionTest.java”.





  4. Create a text file with some contents and place in a directory. Then run the class “FileEncryptionTest.java” to encrypt or decrypt the file.




 

 

 

Conclusion

I hope that you will enjoy my article for the symmetric cryptography. For asymmetric cryptography please refer to the link http://www.articlesbase.com/...ation-technology-articles/asymmetric-cryptography-in-java-438155.html. If you find any problems or errors, please feel free to send me a mail in the address debadattamishra@aol.com . This article is only meant for those who are new to java development. This article does not bear any commercial significance. Please provide me the feedback about this article.

 

Rate this Article: Current: 1.6 / 5 stars - 5 vote(s).

Article Source: http://www.articlesbase.com/programming-articles/symmetric-cryptography-in-java-533875.html

Print this Article Print article   Email to a Friend Send to friend   Publish this Article on your Website Publish this Article   Send Author Feedback Author feedback  
Debadatta MishraAbout the Author:

Debadatta Mishra - An agressive java developer with a differenece.

Submitting articles has become one of the most popular means to drive traffic to your website and promote yourself and your business. Join us today - It's Free!

Article Comments

Comment on this article Comment on this article
Your Name
Your Email:
Comment Body
Enter Validation Code: Captcha


Related Articles

Asymmetric Cryptography in Java
By: Debadatta Mishra | 04/06/2008 | Information Technology
An example for asymmetric cryptography in java

Got a Question? Ask.

Ask the community a question about this article:

Q&A Powered by:
Powered by Yedda 

Latest Programming Articles

Tips for Buying Software Online
By: Daniel Jowssey | 17/11/2008
Buying software online not only helps save the planet, it also has other benefits, including: * Ease and Simplicity. You can purchase software in your underwear at 4am if you really want to. Shopping online doesn’t have to be done within regular business hours, nor do you need to look your best to do it. It’s also easy to shop around for the best prices and takes less time than driving to the shops.

Mvc Design Pattern
By: TuVinhSoft .,JSC | 14/11/2008
Model-view-controller (MVC) is an architectural pattern used in software engineering. In complex computer applications that present a large amount of data to the user, a developer often wishes to separate data (model) and user interface (View) concerns, so that changes to the user interface will not affect data handling, and that the data can be reorganized without changing the user interface.

Advantages of Low Cost Contract Programmers in Freelance Programming
By: Joanna Gadel | 12/11/2008
It observed that web industry is getting tougher thus the necessity of freelance contract programmer is required for developing more effective website with flexible features. This article states the fruitful advantages of freelance programmers in contract programming.

A Guide to Cnc Kits
By: Martin Applebaum | 09/11/2008
CNC kits are a way in which to construct your CNC machine. This article will provide some information on these machines.

A Guide to Cnc Tube Bending Machines
By: Martin Applebaum | 08/11/2008
Are you familiar with a CNC tube bending machine? This article will shed some light on the main function and components of this machine.

Ways to Hire Dedicated Php Programmers
By: Jucick | 08/11/2008
It’s not at all easy to hire dedicated PHP programmers unless you know where and how to find them. Whether you need to fix, update or enhance your website you naturally want the job done quick and right.

Top 4 Reasons Why Addressing Web Accessibility is Important
By: Matt Cave | 05/11/2008
There are very high chances that web accessibility is more important to the performance of your web site than you realize. Article takes a look at the top 4 reasons why it would be important to address the issue of web accessibility.

Xlphoto Printing, the Best Choice for Banner Printing and Poster Printing
By: Vikram Kumar | 01/11/2008
Suppose, you have visited a tourist spot or a hill station; over there, you have witnessed some of the mesmerizing locations of serene nature.

More from Debadatta Mishra

Encryption Using Rsa Algorithm in Java
By: Debadatta Mishra | 23/08/2008 | Programming
This article provides a solution for RSA encryption and decryption in java, which is limited to 117 bytes.

Eclipse Plugin Development - a Bird’s Eye View
By: Debadatta Mishra | 08/08/2008 | Programming
This article gives a brief introduction into the eclipse plugin development. You will find some of the terms used in the eclipse plugin development.

Asymmetric Cryptography in Java
By: Debadatta Mishra | 04/06/2008 | Information Technology
An example for asymmetric cryptography in java

Object Persistence in Java
By: Debadatta Mishra | 16/05/2008 | Information Technology
An article on the persistence of plain old java object in the file system. I provide an approach to the freshers to store the entire java object. I have highlighted on XML Persistence as well as serialization.

Article Categories





Give Feedback

Sign up for our email newsletter

Receive updates, enter your email below