Baran Topal

Baran Topal


April 2024
M T W T F S S
« Feb    
1234567
891011121314
15161718192021
22232425262728
2930  

Categories


CryptoServices in Java: FileOperatorPro

baranbaran

This is the Java Realm implementation. Note there are 4 other parts to fulfill this cryptoservices completely.

The drift in this project is simple: File Operations. I must admit this project is not generic as I intended. It can be improved.

I used FilenameUtils, IOUtils from org.apache.commons.io. Check more, http://commons.apache.org/

I love the drift to use System.getProperty(…) function to fulfill the need of path assigning. System.getProperty(“user.dir”) will return the string representation of the path that the user is executing the program.

There is also a uniquename generator for the outputfile. I used SecureRandom class for this need. You can also generate via UID but for a change, I followed this:



/*
 * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
 * Company: -								 *            
 * Assignment :Cyclic Redundancy Check + DES + Hibernate + JAX-WSRPC	 *
 * Programmer: Baran Topal                    				 *
 * WorkspaceName: Crypto					 	 *
 * Project Name: FileOperatorPro      		 			 *
 * Package name: com.baran.file				 		 *
 * File name: FileOperator.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.file;

import java.io.DataOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.math.BigInteger;
import java.security.SecureRandom;
import org.apache.commons.io.FilenameUtils;
import org.apache.commons.io.IOUtils;

public class FileOperator {

	// store crypted content in a file generated
	public static String storeBinaryContent(byte[] crypted, String inputFile)
	{			
		String completePath = uniqueNameGenerator(inputFile);
		try {		
			DataOutputStream dos = new DataOutputStream(new FileOutputStream(completePath));
			dos.write(crypted);		 
			dos.close();
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return completePath;
	}

	// unique name generator
	public static String uniqueNameGenerator(String inputFile)
	{
		// randomization
		SecureRandom random = new SecureRandom();
		BigInteger bi = new BigInteger(130, random);
		String uniqueEncFile = String.valueOf((BigInteger)bi);		

		// commons-io-2.4.jar
		String fileNameWithOutExt = FilenameUtils.removeExtension(inputFile);		

		String randomFileName = System.getProperty("user.dir") + "\\" + fileNameWithOutExt + "_" + uniqueEncFile + ".enc";

		return randomFileName;		
	}

	// read file content to a binary array
	public static byte[] readFileToBinaryArray(File file)
	{
		byte[] bin = null;
		try {			
			InputStream is = new FileInputStream(file);  

			// commons-io-2.4.jar
			bin = IOUtils.toByteArray(is);
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return bin;
	}	
}

Comments 0
There are currently no comments.