Write a program that allows users to encrypt files. We will implement two simple
ID: 3865090 • Letter: W
Question
Write a program that allows users to encrypt files. We will implement two simple encryption methods. Have the following GUI components: Have a button that when pressed opens a Fil Have the user choose the file with the FileChooser. Encrypt the contents of the file and put the results in a TextArea. First Encryption Technique The first file encryption technique we will use for this program is the highly secure "one letter off system. So an"a" will be represented by a "b", and a "b" will be represented by a "c" in the encrypted file and so on.Explanation / Answer
/*
* COMMENTS: I HAVE USED SWINGS FOR UI ,AND I PRESENTED A ROUGH WORKING SKETCH FOR YOUR PROBLEM
* FOR THE CASE PROVIDED YOU NEED NOT HAVE COMPLE LOGIC ,FOLLOW THE CODE IN ENCRYPT BUTTON
* I HOPE U WILL SURELY GET THE LOGIC,ITS JUST CONVERSION OF STRING TO CHAR ARRAY AND THEN
* THE RESPECTION ELEMENT IS INCREMENTED BY ONE SO AS TO MEET YOUR CRITERIA
*
* SO NOW YOU CAN REPLACE THE LOGIC IN YOUR SCENEBUILDER AND DO RESPECTIVE MODIFICATIONS
* TO ACHIEVE YOUR GOAL
*
* ALL THE BEST
*/
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
public class SwingControlDemo {
private JFrame mainFrame;
private JPanel controlPanel;
private JTextArea textArea;
private String s=null;
public SwingControlDemo(){
prepareGUI();
}
public static void main(String[] args){
SwingControlDemo swingControlDemo = new SwingControlDemo();
swingControlDemo.showFileChooserDemo();
}
private void prepareGUI(){
mainFrame = new JFrame("encrypt text file");
mainFrame.setSize(400,400);
mainFrame.setLayout(new GridLayout(1, 4));
mainFrame.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent windowEvent){
System.exit(0);
}
});
controlPanel = new JPanel();
controlPanel.setLayout(new FlowLayout());
mainFrame.add(controlPanel);
mainFrame.setVisible(true);
}
private void showFileChooserDemo(){
final JFileChooser fileDialog = new JFileChooser();
JButton showFileDialogButton = new JButton("Open File");
showFileDialogButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
int returnVal = fileDialog.showOpenDialog(mainFrame);
if (returnVal == JFileChooser.APPROVE_OPTION) {
java.io.File file = fileDialog.getSelectedFile();
BufferedReader br =null;
try {
br = new BufferedReader(new FileReader(file) );
StringBuilder sb = new StringBuilder();
String line = br.readLine();
while (line != null) {
sb.append(line);
// sb.append(System.lineSeparator());
line = br.readLine();
}
String everything = sb.toString();
System.out.println(everything);
s=everything;
}
catch(Exception ee)
{
ee.printStackTrace();
}finally {
try {
br.close();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
System.out.println("File Selected :" + file.getName());
} else {
System.out.println("Open command cancelled by user." );
}
}
});
JButton encryptButton = new JButton("encrypt");
encryptButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
String encrypted =null;
char chars[] = s.toCharArray();
char encrypt[] = new char[chars.length];
encrypt =chars;
for (int i=0;i<chars.length;i++)
{
if(chars[i]!=' '&&!String.valueOf(chars[i]).equalsIgnoreCase(" ")){
int j =(int)chars[i];
j=j+1;
System.out.print((char)(j));
encrypt[i]=(char)(j);
}
else if(chars[i]==' ')
{
System.out.print(" ");
}
else{
encrypt[i]=chars[i];
System.out.println();
}
}
String text =new String(encrypt);
textArea.setText(text);
}
});
textArea = new JTextArea(5, 20);
JScrollPane scrollPane = new JScrollPane(textArea);
textArea.setEditable(false);
controlPanel.add(showFileDialogButton);
controlPanel.add(encryptButton);
controlPanel.add(textArea);
mainFrame.setVisible(true);
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.