Encrypt and Decrypt a file in java

Source code of the main class

The main class also demonstrates the argument passing mechanism to the jar file to carry out the encryption.The arguments for encryption which are passed are the file name, encrypt as string, and the password to be used for the encryption.


/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package encdecexample;

import java.io.File;

/**
*
* @author Me
*/
public class Encdec {

/**
* @param args the command line arguments
*/
public static void main(String[] args) {
if (args.length > 0) {
if (new File(args[0]).isFile()){

File encdecFile = new File(args[0]);

if(args[1].equals("encrypt") || args[1].equals("decrypt")){

String passWord ="";

if(!(args[2].length()>0)){

System.out.println("Not a valid third Argument.Password is empty !\n\nSyntax java -jar encdec.jar <filepath> <encrypt|decrypt> <password>");

}else{

passWord = args[2];

if(args[1].equals("encrypt")){

Encrypter.Encrypt(encdecFile, passWord.toCharArray());

}else{
//decrypt
Decrypter.Decrypt(encdecFile, passWord.toCharArray());
}
}

}
else{
System.out.println("Not a valid second Argument\n\nSyntax java -jar encdec.jar <filepath> <encrypt|decrypt> <password>");

}
}else{
System.out.println("First Argument is not a valid file\n\nSyntax java -jar encdec.jar <filepath> <encrypt|decrypt> <password>");
}

}else{
System.out.println("No valid Arguments found.\n\nSyntax java -jar encdec.jar <filepath> <encrypt|decrypt> <password>");

}
}
}

In the next page you will read about the decryption process and the decrypter class and know how the encrypted file is decrypted.


Continue reading the remaining 1 pages of the article through page links below ....

Leave a Reply

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

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