Baran Topal

Baran Topal


March 2024
M T W T F S S
« Feb    
 123
45678910
11121314151617
18192021222324
25262728293031

Categories


CryptoServices in Java: CryptoPro

baranbaran

This is the Java Realm implementation for crypto services in Java. Note there are 4 other parts to fulfill this cryptoservices completely. Yet, at the end of the page, there is a main java class for you to test the crypto functionality.

CryptoPro has the functionality of encrypting and decrypting with DES algorithm. Just google for DES details.

As I didn’t use bouncycastle libraries, I didn’t feel a significant different, yet, this project has the bottleneck for BOM. BOM is defined as byte order mark. The definition is in wikipedia but to make it short, when you see ï»¿ characters, well, you have a problem with the encoding of the file content. For this project I went for UTF-8 encoding.

In the functions, I return byte array for the sake of using the array content and make it less coupled with the service. It’s true that I can have a more decent approach, maybe storing the file in a relative path. Speaking of file operations, this project also uses apache commons io package.

I am not a great fan for apache commons packages, but it’s life savior and I sometimes really like it . Here is the link for you to read. http://commons.apache.org/

Also for the targe file, which is encoded in utf-8:

loremipsum_inp

Here is the implementation:


/* 
 * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * 
 * Company: -								 *            
 * Assignment: Cyclic Redundancy Check + DES + Hibernate + JAX-WSRPC	 *
 * Programmer: Baran Topal                   				 *
 * WorkspaceName: Crypto					 	 *
 * Project Name: CyrptoPro           		 			 *
 * Package name: com.baran.crypto			 		 *
 * File name: CryptoDES.java                 				 *
 *                                           				 *      
 * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
 *	                                                                                         *
 *  LICENSE: This source file is subject to have the protection of GNU General                   *
 *	Public License. You can distribute the code freely but storing this license information. *
 *	Contact Baran Topal if you have any questions. barantopal@barantopal.com                 *
 *	                                                                                         *
 * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
 */

package com.baran.crypto;

import java.io.File;
import java.security.MessageDigest;
import java.util.Arrays;
import javax.crypto.Cipher;
import javax.crypto.SecretKey;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;
import org.apache.commons.io.FileUtils;

// Important Note:
// The file in question must have UTF-8 encoding, careful about BOM character.

public class CryptoDES {

	// encrypt the file content via trivia password
	public byte[] encrypt(File file, String trivia) throws Exception
	{
		final MessageDigest md = MessageDigest.getInstance("md5");
		// digest the trivia password in UTF-8
		final byte[] digestTrivia = md.digest(trivia.getBytes("utf-8"));

		// truncating digest
		final byte[] keyBytes = Arrays.copyOf(digestTrivia, 24);
		for(int j = 0, k = 16; j < 8;)
		{
			keyBytes[k++] = keyBytes[j++];
		}
		// DESede setting
		final SecretKey key = new SecretKeySpec(keyBytes, "DESede");

		// CBC IV setting
		final IvParameterSpec iv = new IvParameterSpec(new byte[8]);

		final Cipher cipher = Cipher.getInstance("DESede/CBC/PKCS5Padding");
		cipher.init(Cipher.ENCRYPT_MODE, key, iv);

		String allInOne = FileUtils.readFileToString(file, "utf-8");

		final byte[] plainTextBytes = allInOne.getBytes("utf-8");
		final byte[] cipherText = cipher.doFinal(plainTextBytes);

		return cipherText;
	}

	// decrypt the message via trivia password
	public byte[] decrypt(byte[] message, String trivia) throws Exception
	{
		final MessageDigest md = MessageDigest.getInstance("md5");
		final byte[] digestTrivia = md.digest(trivia.getBytes("utf-8"));

		final byte[] keyBytes = Arrays.copyOf(digestTrivia, 24);

		for(int j = 0, k = 16; j < 8;)
		{
			keyBytes[k++] = keyBytes[j++];
		}

		final SecretKey key = new SecretKeySpec(keyBytes, "DESede");
		final IvParameterSpec iv = new IvParameterSpec(new byte[8]);
		final Cipher decipher = Cipher.getInstance("DESede/CBC/PKCS5Padding");
		decipher.init(Cipher.DECRYPT_MODE, key, iv);

		final byte[] plainText = decipher.doFinal(message);

		return plainText;		
	}
}



/* 
 * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * 
 * Company: -								 *            
 * Assignment: Cyclic Redundancy Check + DES + Hibernate + JAX-WSRPC	 *
 * Programmer: Baran Topal                  				 *
 * WorkspaceName: Crypto					 	 *
 * Project Name: CyrptoPro           					 *
 * Package name: com.baran.crypto					 *
 * File name: CryptoEx.java                  				 *
 *                                           				 *      
 * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
 *	                                                                                         *
 *  LICENSE: This source file is subject to have the protection of GNU General                   *
 *	Public License. You can distribute the code freely but storing this license information. *
 *	Contact Baran Topal if you have any questions. barantopal@barantopal.com                 *
 *	                                                                                         *
 * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
 */

package com.baran.crypto;
import java.io.File;
import java.io.FileOutputStream;
import java.io.PrintWriter;

//Important Note:
//The file in question must have UTF-8 encoding, careful about BOM character.

public class CryptoEx {

	/**
	 * @param args
	 */
	public static void main(String[] args) throws Exception{
		// TODO Auto-generated method stub
		CryptoDES cryptoDES = new CryptoDES();
		byte [] encBytes = cryptoDES.encrypt(new File("loremipsum_inp.txt"), "1234-4567-8910-2345");
		FileOutputStream fos = new FileOutputStream("loremipsum_enc.enc");
		fos.write(encBytes);
		fos.flush();
		fos.close();

		// passwords must match
		byte[] decBytes = cryptoDES.decrypt(encBytes, "1234-4567-8910-2345");

		String encoded = new String(decBytes, "utf-8");
		fos = new FileOutputStream("loremipsum_dec.dec");
		PrintWriter writer = new PrintWriter(fos);

		writer.write(encoded);
		writer.flush();
		writer.close();
		fos.flush();
		fos.close();

	}
}

Comments 0
There are currently no comments.