Data encryption in java is quite simple and Java Cryptographic Extension framework enables you to encrypt data in different widely accepted algorithms.

As an example for this tutorial we will use Blowfish algorithm to encrypt data

import java.io.UnsupportedEncodingException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;

import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.NoSuchPaddingException;
import javax.crypto.spec.SecretKeySpec;

import org.apache.commons.codec.binary.Hex;

public class EncryptionTest {

	
	public static void main(String args[]){
		String dataToEncrypt="Data to encrypt";
		String encryptionKey="the key";
		SecretKeySpec key = new SecretKeySpec(encryptionKey.getBytes(), "Blowfish");
		String encryptedData="";
		byte[] encryptedBytes;
		Cipher cipher;
		try {
		    //Get cipher instance for Blowfish algorithm
			cipher = Cipher.getInstance("Blowfish");
			//initialize cipher in encryption mode with the specified key
			cipher.init(Cipher.ENCRYPT_MODE, key);
			//encrypted bytes of data string
			encryptedBytes = cipher.doFinal(dataToEncrypt.getBytes("UTF-8"));
			//encode encrypted data in Hex String
			encryptedData=Hex.encodeHexString(encryptedBytes);
			System.out.println("DATA:"+encryptedData);
		} catch (NoSuchAlgorithmException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (NoSuchPaddingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (InvalidKeyException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IllegalBlockSizeException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (BadPaddingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (UnsupportedEncodingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}
}

As you can see in the above example we instantiated Cipher object using only the name of the algorithm we wanted to use. Cipher could also be instantiated using different feedback modes and padding schemes.In other words Cipher object could be instantiated using the formula Algorithm/Mode/Padding where Algorithm is the name of the algorithm,Mode is the feedback mode-in other words how data blocks will interact with each other during encoding and decoding- and Padding is the padding scheme used by Cipher

Here you can find all the algorithms,feedback modes and padding schemes supported by Cipher