Different Data Encryption Techniques in Java

In today's cyber world, data security is a major concern and we more often hear about data breaches news. Hence, it becomes very important to keep our critical data secured. To make our data secured, we can use different encryption techniques in order to encrypt our confidential data.

There are many encryption technologies available. Broadly speaking, there are mainly 2 different types of encryption techniques — Symmetric and Asymmetric Encryption. Never get confused with Hashing and Encryption. A hash is a string or number generated from a string of text which is not meant for reversing back to the original string. For example, Bcrypt is a one-way hashing algorithm. Whereas encryption technique is used to make any plain-text unreadable but the encrypted text is always reversible.

In this article, we will discuss the different algorithms that we can use to perform these encryptions and provide some code samples for these algorithm implementations in Java.

Symmetric Encryption

Symmetric encryption is an encryption technique where a single key, commonly known as the encryption key, is used to encrypt the plain-text and the same key is required in order to decrypt the encrypted text.


Symmetric encryption is faster then Asymmetric encryption and it is mostly used in encrypting database passwords, images, etc. If you again want to classify the symmetric encryption, then there are basically two types of symmetric encryption algorithms — Block Algorithms and Stream Algorithms.

Some symmetric encryption techniques are AES, DES, IDEA, Blowfish, etc. Encryption technologies such as AES, DES uses a block cipher algorithm whereas RC4 (Rivest Cipher 4) uses stream cipher.

DES (Data Encryption Standard)

The real strength of symmetric encryption lies in the encryption key length and the DES uses 56 bits long key for encryption. With the increase in computation power of computers, using such a small key length is not secured. Hence, DES is not a secured encryption technique. A DES key can be broken in 22 hours and 15 minutes.

The algorithm is believed to be practically secure in the form of Triple-DES. Triple DES breaks the user-provided key into three subkeys as k1, k2, and k3 and then performs the encryption. You can actually use this fine tool for 3DES encryption online.

Below is the sample Java code that performs 3DES encryption.

public String encrypt(String message) throws Exception {
        DESedeKeySpec spec = new DESedeKeySpec("secret-key".getBytes());
        SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DESede");
        Key desKey = keyFactory.generateSecret(spec);
        Cipher cipher = Cipher.getInstance("desede/CBC/PKCS5Padding");

        IvParameterSpec iv = new IvParameterSpec(new byte[8]);
        cipher.init(Cipher.ENCRYPT_MODE, desKey, iv);

        byte[] encrypted = cipher.doFinal(message.getBytes());

        String encryptedResult = Base64.encodeBase64String(encrypted);
        return encryptedResult;

AES (Advanced Encryption Standard)

AES encryption is the industry standard as of now as it allows 128 bit, 192 bit and 256 bit encryption and relatively stronger then 3DES.

Below is the sample Java code for AES encryption.

private static final String key = "aesEncryptionKey";
private static final String initVector = "encryptionIntVec";

public static String encrypt(String value) {
	try {
		IvParameterSpec iv = new IvParameterSpec(initVector.getBytes("UTF-8"));
		SecretKeySpec skeySpec = new SecretKeySpec(key.getBytes("UTF-8"), "AES");

		Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5PADDING");
		cipher.init(Cipher.ENCRYPT_MODE, skeySpec, iv);

		byte[] encrypted = cipher.doFinal(value.getBytes());
		return Base64.encodeBase64String(encrypted);
	} catch (Exception ex) {
	return null;

Asymmetric Encryption

Asymmetrical encryption is powerful then symmetric encryption but slower than symmetric encryption. We should not really compare these encryption techniques with slowness as both are made for different purposes.

Asymmetric encryption uses the concept of public and private key encryption and it is mostly used when there are 2 different endpoints are involved such as VPN client. You can use this tool to generate public and private keys. A very common example of Asymmetric encryption is RSA encryption.

With RSA, we encrypt sensitive information with a public key and a matching private key is used to decrypt the same. Below is the sample code to perform RSA encryption in Java.

public static byte[] encrypt(String data, String publicKey) throws BadPaddingException, IllegalBlockSizeException, InvalidKeyException, NoSuchPaddingException, NoSuchAlgorithmException {
	Cipher cipher = Cipher.getInstance("RSA/ECB/PKCS1Padding");
	cipher.init(Cipher.ENCRYPT_MODE, getPublicKey(publicKey));
	return cipher.doFinal(data.getBytes());
java, information security, rsa, aes, des, encryption

You can't comment this post because its author is not yet a full member of the community. You will be able to contact the author only after he or she has been invited by someone in the community. Until then, author's username will be hidden by an alias.