Debadatta Mishra - An agressive java developer with a differenece.
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.
- Please create the appropriate package structure and copy the corresponding classes inside the package.
- First run the class “KeyGeneratorTest.java” . You will find a file called “secret.key” where the secret key is tored.
- Next run the class “StringEncryptionTest.java”.
- 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/information-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.
- Related Videos
- Related Articles
- Ask / Related Q&A




Packt Publishing's new 'Ext JS 3.0 Cookbook'
By: Amit Sharma | 13/11/2009Authored by Jorge Ramon, the Ext JS 3.0 Cookbook contains step-by-step recipes for building impressive rich internet applications using the Ext JS JavaScript library.
GROUP BY IN XQUERY
By: Anuj Tripathi | 13/11/2009This article will provide you to implement GROUP BY clause in XQUERY with the code.
Java Developers and the power of Java
By: jacklin | 13/11/2009Article explaining the security steps Java developers can take for JAR files while developing application with Java platform.
Sending SMS Alerts in SharePoint 2010 over Office Mobile Service Protocol (OMS)
By: Virtosoftware | 12/11/2009Finally it's possible to send SMS directly from SharePoint to mobile phones. This feature was only available in some add-on components before.
Forbidding the Clipboard for the specified process
By: Apriorit Inc. | 11/11/2009Though the Clipboard is one of the fundamental parts of the Windows operating system, there is little information about how it works, especially in the low level. In this article, I’m going to tell you something about the Clipboard internals by showing how you can forbid access to it.
How to save money from fixing computer?
By: janson | 11/11/2009Are you familiar with these problems in your computer below? Mysterious errors, crashes and restarts Sluggish internet and download speeds Trouble loading songs, movies and programs Do you tire of frequently bothering friends for help, or worse, paying a professional to fix your computer? Like a car, without regular maintenance computers begin to accumulate errors that cause slowdowns, crashes and even potential system failure.
Learn Java Easily With an IDE
By: Alberto Pareja-Lecaros | 11/11/2009Want to learn Java even more quickly than you currently are? IDE's are not just for professionals; learn how you can use an IDE to teach yourself Java even more easily, even if you're an absolute beginner! There are many advantages to using an IDE, why torture yourself by using a standard text editor when you can use the power of an IDE to learn Java even more easily?
Benefits of digital photography-why digital photography is booming?
By: hdrmiar | 11/11/2009This article shows you some benefits of digital photography and makes an outlook of future development of photography.
Encryption Using Rsa Algorithm in Java
By: Debadatta Mishra | 23/08/2008 | ProgrammingThis article provides a solution for RSA encryption and decryption in java, which is limited to 117 bytes.
Symmetric Cryptography in Java
By: Debadatta Mishra | 23/08/2008 | ProgrammingThis article gives an insight into the symmetric cryptography in java. It basically provides you the use of DES algorithm.
Eclipse Plugin Development - a Bird’s Eye View
By: Debadatta Mishra | 08/08/2008 | ProgrammingThis article gives a brief introduction into the eclipse plugin development. You will find some of the terms used in the eclipse plugin development.
Object Persistence in Java
By: Debadatta Mishra | 16/05/2008 | Information TechnologyAn 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.