patternjavaMinor
Application for encrypting and decrypting files in Java
Viewed 0 times
applicationjavaencryptingfilesfordecryptingand
Problem
(See the next iteration.)
I have that application for en-/decrypting files. Here, I will post the actual GUI and command line code. In order to run the program refer to this GitHub repository.
Some images:
My code is as follows:
App.java:
```
package net.coderodde.ciphertool;
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.Toolkit;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.SwingUtilities;
import net.coderodde.encryption.CipherTools;
import net.coderodde.file.FileTools;
/**
* This class implements the command line app for encrypting and decrypting
* files with a key.
*
* @author Rodion "rodde" Efremov
* @version 1.6 (Feb 29, 2016)
*/
public class App {
private static final String HELP_MESSAGE =
"Usage: java -jar File.jar [-e | -d FILE1 [FILE2 [...]]]\n" +
"Where -e encrypt the files.\n" +
" -d decrypt the files.\n" +
" the key in decimal; " +
"use prefix \"0x\" for hexadecimal.\n" +
"If you omit all arguments a GUI is started instead.";
private boolean graphicalInterfaceRequested;
private boolean printHelpMessage;
private Mode mode;
private int key;
private String[] args;
public void processArguments(String[] args) {
this.args = args.clone();
if (args.length == 0) {
graphicalInterfaceRequested = true;
return;
}
if (args.length = 2 &&
(args[1].startsWith("0x") || args[1].startsWith("0X"))) {
String keyString = args[1].substring(2).trim().toLowerCase();
try {
key = Integer.parseInt(keyString, 16);
} catch (NumberFormatException ex) {
printHelpMessage = true;
return;
}
} else {
try {
key =
I have that application for en-/decrypting files. Here, I will post the actual GUI and command line code. In order to run the program refer to this GitHub repository.
Some images:
My code is as follows:
App.java:
```
package net.coderodde.ciphertool;
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.Toolkit;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.SwingUtilities;
import net.coderodde.encryption.CipherTools;
import net.coderodde.file.FileTools;
/**
* This class implements the command line app for encrypting and decrypting
* files with a key.
*
* @author Rodion "rodde" Efremov
* @version 1.6 (Feb 29, 2016)
*/
public class App {
private static final String HELP_MESSAGE =
"Usage: java -jar File.jar [-e | -d FILE1 [FILE2 [...]]]\n" +
"Where -e encrypt the files.\n" +
" -d decrypt the files.\n" +
" the key in decimal; " +
"use prefix \"0x\" for hexadecimal.\n" +
"If you omit all arguments a GUI is started instead.";
private boolean graphicalInterfaceRequested;
private boolean printHelpMessage;
private Mode mode;
private int key;
private String[] args;
public void processArguments(String[] args) {
this.args = args.clone();
if (args.length == 0) {
graphicalInterfaceRequested = true;
return;
}
if (args.length = 2 &&
(args[1].startsWith("0x") || args[1].startsWith("0X"))) {
String keyString = args[1].substring(2).trim().toLowerCase();
try {
key = Integer.parseInt(keyString, 16);
} catch (NumberFormatException ex) {
printHelpMessage = true;
return;
}
} else {
try {
key =
Solution
Specifying a key
From a UX perspective, I think it will help if you can also indicate that the key should either be prefixed with
Code duplication
And speaking about parsing the input keys, there's some code duplication in
Getting a file list
I think it'll be better if a processed
In fact, this will make refactoring easier when
From a UX perspective, I think it will help if you can also indicate that the key should either be prefixed with
0x, so that you will treat it as a hexadecimal value, or it should be an integer.Code duplication
And speaking about parsing the input keys, there's some code duplication in
App.processArguments(String[]) and MyActionListener.actionPerformed(ActionEvent). You should consider refactoring the integer parsing part out so that it can be shared in both places.Getting a file list
private List getFileList(String[] args) {
// We subtract 2 in order to omit the fist two arguments in 'args' that
// are the switch and the key.
List fileList = new ArrayList<>(args.length - 2);
for (int i = 2; i < args.length; ++i) {
fileList.add(new File(args[i]));
}
return fileList;
}I think it'll be better if a processed
args array without the initial command-line arguments is passed in here. Anyways, assuming if you prefer to keep to the current approach, you can use the familiar stream processing for this too:private List getFileList(String[] args) {
return Arrays.stream(args)
.skip(2)
.map(File::new)
.collect(Collectors.toList());
}In fact, this will make refactoring easier when
args no longer have the additional arguments: you can just remove the line .skip(2).Code Snippets
private List<File> getFileList(String[] args) {
// We subtract 2 in order to omit the fist two arguments in 'args' that
// are the switch and the key.
List<File> fileList = new ArrayList<>(args.length - 2);
for (int i = 2; i < args.length; ++i) {
fileList.add(new File(args[i]));
}
return fileList;
}private List<File> getFileList(String[] args) {
return Arrays.stream(args)
.skip(2)
.map(File::new)
.collect(Collectors.toList());
}Context
StackExchange Code Review Q#121657, answer score: 4
Revisions (0)
No revisions yet.