Hello All,
I'm currently working on a project which requires me to encrypt multiple text files at one go.
I am able to retrieve the name of the files through listfiles() but how am i supposed to pass those files to File variable so that the encrypt method can encrypt those files.
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package EncryptDecryptPackage;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.security.InvalidKeyException;
import java.security.Key;
import java.security.NoSuchAlgorithmException;
import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.NoSuchPaddingException;
import javax.crypto.spec.SecretKeySpec;
/**
*
* @author SagarSe7en
* Utility which will be called during the Encryption/Decryption Process
*/
public class EncryptDecryptUtility
{
private static final String ALGORITHM = "AES";
private static final String TRANSFORMATION = "AES";
public static void encrypt(String key, File inputFile, File outputFile)
throws EncrypterDecrypterException
{
doCrypto(Cipher.ENCRYPT_MODE,key,inputFile,outputFile);
}
public static void decrypt(String key, File inputFile, File outputFile)
throws EncrypterDecrypterException
{
doCrypto(Cipher.DECRYPT_MODE,key,inputFile,outputFile);
}
private static void doCrypto(int cipherMode, String key, File inputFile, File outputFile)
throws EncrypterDecrypterException
{
try
{
Key fraternity_key = new SecretKeySpec(key.getBytes(), ALGORITHM);
Cipher cipher = Cipher.getInstance(TRANSFORMATION);
cipher.init(cipherMode, fraternity_key);
FileInputStream inputStream = new FileInputStream(inputFile);
byte[] inputBytes = new byte[(int) inputFile.length()];
inputStream.read(inputBytes);
byte[] outputBytes = cipher.doFinal(inputBytes);
FileOutputStream outputStream = new FileOutputStream(outputFile);
outputStream.write(outputBytes);
inputStream.close();
outputStream.close();
}
catch(NoSuchPaddingException | NoSuchAlgorithmException | InvalidKeyException | BadPaddingException | IllegalBlockSizeException | IOException ex)
{
throw new EncrypterDecrypterException("Error encrypting/decrypting file", ex);
}
}
}
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package EncryptDecryptPackage;
/**
*
* @author SagarSe7en
* Java Code to handle all kinds of Exception in case of any during Runtime
*/
public class EncrypterDecrypterException extends Exception
{
public EncrypterDecrypterException()
{
}
public EncrypterDecrypterException(String message, Throwable throwable)
{
super(message, throwable);
}
}
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package EncryptDecryptPackage;
import java.io.File;
/**
*
* @author SagarSe7en
*/
public class PBL_Finbranch_Encrypter
{
public static void main(String[]args)
{
String upl_directory;
String Secret_Key = "Prime_Fraternity";
File cts_unencrypted_file = new File(".");
//File cts_encrypted_file = new File(".");
upl_directory = cts_unencrypted_file.getAbsolutePath();
System.out.println("Upload Directory Is " +upl_directory);
File[] listofFiles = cts_unencrypted_file.listFiles();
for(int i = 0; i<listofFiles.length; i++)
{
if(listofFiles[i].isFile())
{
try
{
EncryptDecryptUtility.encrypt(Secret_Key, cts_unencrypted_file, cts_unencrypted_file);
}
catch(EncrypterDecrypterException ex)
{
System.out.println(ex.getMessage());
}
}
else
{
System.out.println("Unable to Encrypt Files");
}
}
}
}
What i am trying to achieve is that the jar file generated from this project should get the current directory which it is able to get, then list all the files in the directory and then encrypt them one by one.
Will appreciate all the help possible.