Question

Java Programming Part 1 File encryption is the science of writing the contents of a file...

Java Programming

Part 1

File encryption is the science of writing the contents of a file in a secret code. Your encryption program should work like a filter, reading the contents of one file, modifying the data into a code, and then writing the coded contents out to a second file. The second file will be a version of the first file, but written in a secret code. Although there are complex encryption techniques, you should come up with a simple one of your own. For example, you could read the first file one character at a time, and add 10 to the character code of each character before it is written to the second file. I would like you to modify the program slightly. You should ask the user what the number is that is going to be used for the Encryption. Therefore the program will prompt the user for a number and apply it to the data in the file to create the encrypted file.

Part 2

File Decryption Filter
Write a program that decrypts the file produced by the program in Programming Challenge 7. The decryption program should read the contents of the coded file, restore the data to its original state, and write it to another file.

Note: Need this code simplified for a beginner class

0 0
Add a comment Improve this question Transcribed image text
Answer #1

2.


// ---------- Imported Packages ------------------
import java.text.*;
import java.util.*;
import javax.swing.*;
import java.awt.*;
import java.io.*;
import java.awt.event.*;
// -----------------------------------------------



class Problem12_8 extends PaintPanel
{
        JPanel topPanel = new JPanel();
        JPanel inputPanel = new JPanel(new GridLayout(2,1));
        JPanel outputPanel = new JPanel();
        JLabel inputLabel = new JLabel("File Name (*.dat):");
        JTextArea outputLabel = new JTextArea(25,25);
        JTextArea fileList = new JTextArea(25,25);
        final JTextField input = new JTextField(25);
        
        
        public Problem12_8()
        {
                buildPanel();
                
                add(KDUtil.fileHandleTest(topPanel));
                
                setVisible(true);
        }
        
        @Override
        public String getDescription()
        {
                return "8. File Decryption Filter"
+"\n\nWrite a program that decrypts the file produced by the program in Programming Challenge 7. The decryption program should read the contents of the coded file, "
+"\nrestore the data to its original state, and write it to another file.";
        }
        
        
        public void buildPanel()
        {
                /*input.addActionListener(new ActionListener() 
                {
                        public void actionPerformed(ActionEvent arg0) 
                        {
                                                if(input.getText().isEmpty()==false)
                                                {
                                                        outputLabel.setText("");
                                                        outputLabel.append(getFileWords(input.getText()));
                                                        outputLabel.setEditable(false);
                                                }                                                               
                        }
                });     */
                                        
                        
                JPanel mainPanel = new JPanel();
                mainPanel.setLayout(new BorderLayout());
                
                JLabel header = new JLabel("Decryption Program");
                JPanel headerFrame = new JPanel();
                headerFrame.add(header);
                mainPanel.add(headerFrame, BorderLayout.NORTH);
                

                
                JPanel wrapper = new JPanel();
                wrapper.add(inputLabel);
                wrapper.add(input);
                wrapper.add(new JLabel("Decryption Key"));
                final JTextField inputKey = new JTextField(10);
                wrapper.add(inputKey);
        
                inputPanel.add(wrapper);
                
                JButton create = new JButton("Read New File");
                create.addActionListener(new ActionListener() 
                {
                        public void actionPerformed(ActionEvent arg0) 
                        {
                                outputLabel.setText(" ");
                                populateList();
                                
                                String temp = "";
                                String key = inputKey.getText();

                                try
                                {

                                        RandomAccessFile output = new RandomAccessFile(input.getText(),"rw");
                                        boolean endOfFile = false;
                                        while(!endOfFile)
                                        {
                                                try
                                                {
                                                        temp = temp + output.readUTF();
                                                }
                                                catch(EOFException e)
                                                {
                                                        endOfFile = true;
                                                }
                                        }
                                        
                                }
                                catch(IOException e)
                                {
                                        outputLabel.append(e.getMessage());
                                }
                                
                                outputLabel.append(Decryption.decode(temp, key));
        
                        }
                });
                
                                
                
                wrapper = new JPanel();
                wrapper.add(create);
                
                inputPanel.add(wrapper);
                
                
                
                mainPanel.add(inputPanel, BorderLayout.CENTER);
                
        
                outputPanel.setLayout(new GridLayout(0,2));
                
                JPanel left = new JPanel(new BorderLayout());
                JPanel right = new JPanel(new BorderLayout());
                
                left.add(new JLabel("File List"), BorderLayout.NORTH);
                right.add(new JLabel("Decryption"), BorderLayout.NORTH);
                
                JScrollPane scrollFileList = new JScrollPane(fileList);
                left.add(scrollFileList, BorderLayout.CENTER);
                JScrollPane scroll = new JScrollPane(outputLabel);
                right.add(scroll, BorderLayout.CENTER);
                
                outputPanel.add(left);
                outputPanel.add(right);
                
                mainPanel.add(outputPanel, BorderLayout.SOUTH);

                populateList();
                topPanel.add(mainPanel);
                
        }
        
        private void populateList()
        {
                        try{
                                        String path = "."; 
                                        fileList.setText(" ");
 
                                        String files;
                                        File folder = new File(path);
                                        File[] listOfFiles = folder.listFiles(); 
 
                                        for (int i = 0; i < listOfFiles.length; i++) 
                                        {
 
                                           if (listOfFiles[i].isFile()) 
                                           {
                                                   files = listOfFiles[i].getName();
                                                        if(files.endsWith(".dat"))
                                                        fileList.append(files + "\n");
                                           }
                                   }
                        }
                        catch(java.security.AccessControlException k){}

        }

        
        
}                       

class Decryption
{
        public static String decode(String s, String key)
        {
                String temp = "";
                int coder = 0;
                
                for(int i = 0; i<key.length(); i++)
                {
                        coder = coder + (int)key.charAt(i);
                }
                
                for(int i = 0; i<s.length(); i++)
                {
                        int value = (int)s.charAt(i);
                        char k = (char)(value - coder);
                        
                        temp = temp + k;
                }
                
                return temp;
        }
}

1.



// ---------- Imported Packages ------------------
import java.text.*;
import java.util.*;
import javax.swing.*;
import java.awt.*;
import java.io.*;
import java.awt.event.*;
// -----------------------------------------------



class Problem12_7 extends PaintPanel
{
        JPanel topPanel = new JPanel();
        JPanel inputPanel = new JPanel(new GridLayout(2,1));
        JPanel outputPanel = new JPanel();
        JLabel inputLabel = new JLabel("File Name (*.dat):");
        JTextArea outputLabel = new JTextArea(25,25);
        JTextArea fileList = new JTextArea(25,25);
        final JTextField input = new JTextField(25);
        
        
        public Problem12_7()
        {
                try
                {
                try{RandomAccessFile test = new RandomAccessFile("test.txt","rw");test.close();}catch(IOException e){}

                buildPanel();
                }
                catch(java.security.AccessControlException k)
                {
                        topPanel.removeAll();
                        topPanel.add(new JLabel("ERROR: Applets cannot read or write files"));
                }       
                                
                                
                add(topPanel);
                setVisible(true);
        }
        
        @Override
        public String getDescription()
        {
                return "7. File Encryption Filter"
+"\n\nFile encryption is the science of writing the contents of a file in a secret code. Your encryption program should work like a filter, reading the contents of one file, "
+"\nmodifying the data into a code, and then writing the coded contents out to a second file. The second file will be a version of the first file, but written in a secret code."
+"\nAlthough there are complex encryption techniques, you should come up with a simple one of your own. For example, you could read the first file one character at a time, and add 10 to "
+"\nthe character code of each character before it is written to the second file.";
        }
        
        
        public void buildPanel()
        {
                /*input.addActionListener(new ActionListener() 
                {
                        public void actionPerformed(ActionEvent arg0) 
                        {
                                                if(input.getText().isEmpty()==false)
                                                {
                                                        outputLabel.setText("");
                                                        outputLabel.append(getFileWords(input.getText()));
                                                        outputLabel.setEditable(false);
                                                }                                                               
                        }
                });     */
                                        
                        
                JPanel mainPanel = new JPanel();
                mainPanel.setLayout(new BorderLayout());
                
                JLabel header = new JLabel("Encryption Program");
                JPanel headerFrame = new JPanel();
                headerFrame.add(header);
                mainPanel.add(headerFrame, BorderLayout.NORTH);
                

                
                JPanel wrapper = new JPanel();
                wrapper.add(inputLabel);
                wrapper.add(input);
                wrapper.add(new JLabel("Encryption Key"));
                final JTextField inputKey = new JTextField(10);
                wrapper.add(inputKey);
        
                inputPanel.add(wrapper);
                
                JButton create = new JButton("Write New File");
                create.addActionListener(new ActionListener() 
                {
                        public void actionPerformed(ActionEvent arg0) 
                        {
                                outputLabel.setText(" ");
                                String temp = fileList.getText();
                                String key = inputKey.getText();
                                
                                temp = Encryption.code(temp, key);
                                outputLabel.append(temp);       

                                try
                                {
                                        RandomAccessFile output = new RandomAccessFile(input.getText(),"rw");
                                        output.writeUTF(temp);
                                }
                                catch(IOException e)
                                {
                                        outputLabel.append(e.getMessage());
                                }
        
                        }
                });
                
                                
                
                wrapper = new JPanel();
                wrapper.add(create);
                
                inputPanel.add(wrapper);
                
                
                
                mainPanel.add(inputPanel, BorderLayout.CENTER);
                
        
                outputPanel.setLayout(new GridLayout(0,2));
                
                JPanel left = new JPanel(new BorderLayout());
                JPanel right = new JPanel(new BorderLayout());
                
                left.add(new JLabel("Input"), BorderLayout.NORTH);
                right.add(new JLabel("Encryption"), BorderLayout.NORTH);
                
                JScrollPane scrollFileList = new JScrollPane(fileList);
                left.add(scrollFileList, BorderLayout.CENTER);
                JScrollPane scroll = new JScrollPane(outputLabel);
                right.add(scroll, BorderLayout.CENTER);
                
                outputPanel.add(left);
                outputPanel.add(right);
                
                mainPanel.add(outputPanel, BorderLayout.SOUTH);

                topPanel.add(mainPanel);
        
        }
        
        
}                       

class Encryption
{
        public static String code(String s, String key)
        {
                String temp = "";
                int coder = 0;
                
                for(int i = 0; i<key.length(); i++)
                {
                        coder = coder + (int)key.charAt(i);
                }
                
                for(int i = 0; i<s.length(); i++)
                {
                        int value = (int)s.charAt(i);
                        char k = (char)(value + coder);
                        
                        temp = temp + k;
                }
                
                return temp;
        }
}
Add a comment
Know the answer?
Add Answer to:
Java Programming Part 1 File encryption is the science of writing the contents of a file...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Using java: Store the files in .txt format and use the readByte() and writeByte() methods. File...

    Using java: Store the files in .txt format and use the readByte() and writeByte() methods. File encryption is the science of writing the contents of a file in a secret code. Write an encryption program that works like a filter, reading the contents of one file, modifying the data into a code, and then writing the coded contents out to a second file. The second file will be a version of the first file, but written in a secret code....

  • File Encryption and Decryption chapter 9 programming exercise #3 Design and write a python program to...

    File Encryption and Decryption chapter 9 programming exercise #3 Design and write a python program to successfully complete chapter 9 programming exercise #3. File Encryption and Decryption Write a program that uses a dictionary to assign “codes” to each letter of the alphabet. For example: codes = { ‘A’ : ‘%’, ‘a’ : ‘9’, ‘B’ : ‘@’, ‘b’ : ‘#’, etc . . .} Using this example, the letter A would be assigned the symbol %, the letter a would...

  • program must be written in python and have commentary. thank you 3. File Encryption and Decryption...

    program must be written in python and have commentary. thank you 3. File Encryption and Decryption Write a program that uses a dictionary to assign "codes" to each letter of the alphabet. For example: codes-{A':'8','a' :'9', 'B' : e','b' :'#,, etc ) Using this example, the letter A would be assigned the symbol %, the letter a would be assigned the number 9, the letter B would be assigned the symbol e, and so forth. The program should open a...

  • This is to be written in C++. Thank you so much for you help as I'm...

    This is to be written in C++. Thank you so much for you help as I'm really struggling with this. Must be written in this format: #include <stdio.h> int main(void) { printf("Hello World\n"); return 0; } The explosive growth of Internet communications and data storage on Internet-connected computers has greatly increased privacy concerns. The field of cryptography is concerned with coding data to make it difficult (and hopefully—with the most advanced schemes—impossible) for unauthorized users to read. In this exercise,...

  • In Python, do a basic encryption of a text file in the following manner. The program...

    In Python, do a basic encryption of a text file in the following manner. The program encrypt.py will read in the following text file and rearrange the lines in the file randomly and save the rearranged lines of txt to another file called encrypted.txt. It will also save another file called key.txt that will contain the index of the lines that were rearranged in the encrypted file, so for example if the 4th line from the original file is now...

  • You will be writing a simple Java program that implements an ancient form of encryption known...

    You will be writing a simple Java program that implements an ancient form of encryption known as a substitution cipher or a Caesar cipher (after Julius Caesar, who reportedly used it to send messages to his armies) or a shift cipher. In a Caesar cipher, the letters in a message are replaced by the letters of a "shifted" alphabet. So for example if we had a shift of 3 we might have the following replacements: Original alphabet: A B C...

  • Write a C++ program that reads text from a file and encrypts the file by adding...

    Write a C++ program that reads text from a file and encrypts the file by adding 6 to the ASCII value of each character. See section 5.11 in Starting out with C++ for information on reading and writing to text files. Your program should: 1. Read the provided plain.txt file one line at a time. Because this file has spaces, use getline (see section 3.8). 2. Change each character of the string by adding 6 to it. 3. Write the...

  • Reading and Writing Complete Files in C: The first part of the lab is to write...

    Reading and Writing Complete Files in C: The first part of the lab is to write a program to read the complete contents of a file to a string. This code will be used in subsequent coding problems. You will need 3 functions: main(), read_file() and write_file(). The main function contains the driver code. The read_file() function reads the complete contents of a file to a string. The write_file() writes the complete contents of a string to a file. The...

  • Background: For this assignment, you will write a small encryption utility that implements a simple encryption...

    Background: For this assignment, you will write a small encryption utility that implements a simple encryption algorithm described below. The program will take one command line argument as an input; this will represent the word which is to be encrypted. As an output, your program will print the encrypted version of the word to the console using a simple printf() statement. This is the only output your program needs to produce. There is an important catch, however: your program is...

  • Lab #10 C++ Write a C++ program that reads text from a file and encrypts the...

    Lab #10 C++ Write a C++ program that reads text from a file and encrypts the file by adding an encryption factor (EF) to the ASCII value of each character. The encryption factor is 1 for the first line and increases by 1 for each line up to 4 and then starts over at 1. So, for the 4 th line the EF is 4, for the 5th line it is 1, for the 10th line it is 2. In...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT