security

Java DES Encryption Decryption File Tutorial

In this tutorial we are going to see how can you Encrypt and Decrypt a file in Java, using the DES encryption algorithm. DES (Data Encryption Standard) is a block cipher algorithm. It’s one of the most basic symmetric encryption mechanisms, which means that both the encryptor and the decryptor has to know the secret key in order to perform their respective actions.

So the basic steps of this tutorial are :

  • Generate a secure, secret key using a KeyGnerator
  • Create one DES Chiper to encrypt and one to decrypt, using the same secret key as well as specifing an Initialization Vector (IV) for the block algorithm initialization.
  • Write and Read encrypted  or decrypted data  using CipherOutputStream and CipherInputStream

Let’s see the code:

package com.javacodegeeks.java.core;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.security.InvalidAlgorithmParameterException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.security.spec.AlgorithmParameterSpec;

import javax.crypto.Cipher;
import javax.crypto.CipherInputStream;
import javax.crypto.CipherOutputStream;
import javax.crypto.KeyGenerator;
import javax.crypto.NoSuchPaddingException;
import javax.crypto.SecretKey;
import javax.crypto.spec.IvParameterSpec;

public class JavaDESEncryption {

	private static Cipher encrypt;
	private static Cipher decrypt;

	private static final byte[] initialization_vector = { 22, 33, 11, 44, 55, 99, 66, 77 };

	public static void main(String[] args) {

		String clearFile = "C:/Users/nikos7/Desktop/input.txt";
		String encryptedFile = "C:/Users/nikos7/Desktop/encrypted.txt";
		String decryptedFile = "C:/Users/nikos7/Desktop/decrypted.txt";

		try {

			SecretKey secret_key = KeyGenerator.getInstance("DES")
					.generateKey();
			AlgorithmParameterSpec alogrithm_specs = new IvParameterSpec(
					initialization_vector);

			// set encryption mode ...
			encrypt = Cipher.getInstance("DES/CBC/PKCS5Padding");
			encrypt.init(Cipher.ENCRYPT_MODE, secret_key, alogrithm_specs);

			// set decryption mode
			decrypt = Cipher.getInstance("DES/CBC/PKCS5Padding");
			decrypt.init(Cipher.DECRYPT_MODE, secret_key, alogrithm_specs);

			// encrypt file
			encrypt(new FileInputStream(clearFile), new FileOutputStream(
					encryptedFile));

			// decrypt file
			decrypt(new FileInputStream(encryptedFile), new FileOutputStream(
					decryptedFile));
			System.out.println("End of Encryption/Decryption procedure!");

		} catch (NoSuchAlgorithmException | NoSuchPaddingException
				| InvalidKeyException | InvalidAlgorithmParameterException
				| IOException e) {
			e.printStackTrace();
		}

	}

	private static void encrypt(InputStream input, OutputStream output)
			throws IOException {

		output = new CipherOutputStream(output, encrypt);
		writeBytes(input, output);
	}

	private static void decrypt(InputStream input, OutputStream output)
			throws IOException {

		input = new CipherInputStream(input, decrypt);
		writeBytes(input, output);
	}

	private static void writeBytes(InputStream input, OutputStream output)
			throws IOException {
		byte[] writeBuffer = new byte[512];
		int readBytes = 0;

		while ((readBytes = input.read(writeBuffer)) >= 0) {
			output.write(writeBuffer, 0, readBytes);
		}

		output.close();
		input.close();
	}

}

input.txt

JavaCodeGeeks Rocks!

encrypted.txt

―w~Z5­ό&ΪεE=΄dΰ@’„+½έΎ

decrypted.txt

JavaCodeGeeks Rocks!

 
This was an example on how to work with DES Encryption/Decryption for a file in Java.

Byron Kiourtzoglou

Byron is a master software engineer working in the IT and Telecom domains. He is an applications developer in a wide variety of applications/services. He is currently acting as the team leader and technical architect for a proprietary service creation and integration platform for both the IT and Telecom industries in addition to a in-house big data real-time analytics solution. He is always fascinated by SOA, middleware services and mobile development. Byron is co-founder and Executive Editor at Java Code Geeks.
Subscribe
Notify of
guest

This site uses Akismet to reduce spam. Learn how your comment data is processed.

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
Back to top button