TrippleDes Encryption Java

TrippleDes Encryption Java

In cryptography wrold the Triple DES OR 3DES is common name for Triple Data data encryption Algorithm which is TDEA or Triple DEA the symmetric-key block cipher. This can be applies to the Data Encryption Standard (DES) the cipher algorithm three times to each of data block.

Original DES cipher’s key size is 56 bits was generally sufficient when its algorithm was designed but availability of continuosly increasing computational power made it brute-force attacks feasible. Triple DES encryption provides relatively simple operation of increasing key size of DES that can protect against such attacks without need to design completely new block cipher algorithm. Below is sample TripleDes encryption java program to encrypt and decrypt data:

  • TripleDes.java:
import java.security.spec.KeySpec;

import javax.crypto.Cipher;
import javax.crypto.SecretKey;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.DESedeKeySpec;
import org.apache.commons.codec.binary.Base64;

public class TrippleDes {

	private static final String UNICODE_FORMAT = "UTF8";
	public static final String DESEDE_ENCRYPTION_SCHEME = "DESede";
	private KeySpec ks;
	private SecretKeyFactory skf;
	private Cipher cipher;
	byte[] arrayBytes;
	private String myEncryptionKey;
	private String myEncryptionScheme;
	SecretKey key;

	public TrippleDes() throws Exception {
		myEncryptionKey = "OTCProjectPasswordEncryp";
		myEncryptionScheme = DESEDE_ENCRYPTION_SCHEME;
		arrayBytes = myEncryptionKey.getBytes(UNICODE_FORMAT);
		ks = new DESedeKeySpec(arrayBytes);
		skf = SecretKeyFactory.getInstance(myEncryptionScheme);
		cipher = Cipher.getInstance(myEncryptionScheme);
		key = skf.generateSecret(ks);
	}

	public String encrypt(String unencryptedString) {
		String encryptedString = null;
		try {
			cipher.init(Cipher.ENCRYPT_MODE, key);
			byte[] plainText = unencryptedString.getBytes(UNICODE_FORMAT);
			byte[] encryptedText = cipher.doFinal(plainText);
			encryptedString = new String(Base64.encodeBase64(encryptedText));
		} catch (Exception e) {
			e.printStackTrace();
		}
		return encryptedString;
	}

	public String decrypt(String encryptedString) {
		String decryptedText = null;
		try {
			cipher.init(Cipher.DECRYPT_MODE, key);
			byte[] encryptedText = Base64.decodeBase64(encryptedString);
			byte[] plainText = cipher.doFinal(encryptedText);
			decryptedText = new String(plainText);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return decryptedText;
	}

	public static void main(String args[]) throws Exception {
		TrippleDes td = new TrippleDes();

		String target = "Java Honk";
		String encrypted = td.encrypt(target);
		String decrypted = td.decrypt(encrypted);

		System.out.println("String To Encrypt: " + target);
		System.out.println("Encrypted String:" + encrypted);
		System.out.println("Decrypted String:" + decrypted);

	}

}
  • Output:

TrippleDes Encryption Java

  • For more information on encryption please visit Oracle documentation here

Leave a Reply

Your email address will not be published. Required fields are marked *