Lab 7: Simple encryption program
Assignment due on Tuesday, June 17
Today we will do our last programming assignment. In this assignment you will write your own public key encrypting/decrypting program.
You can use this program to secure your data/information from intruders (maybe from your sister?), or securely communicate with your
friends who also have this program.
Like in previous assignments, I will provide you step by step instructions to write the program.
- Create a new project and add a new class.
- Click on Run -- Open Debug Dialog... and select your project.
Select "Arguments" and set the working directory to "workspace"/"project folder"/bin, where you must substitute "workspace" with your workspace and "project folder" with your project folder names
- As we will be working with files, import java.io.* to the project.
- Declare the following class variables: inputFilename, outputFilename, which are Strings and counter, which is int.
Keep in mind that you will need to declare the class variables as static
- Add a method called encrypt(), which has no parameters, and returns void.
Again, keep in mind that the method should be static
-
Copy the following code into the encrypt() method.
try
{
//This code is reading the input.txt file line by line and after encrypting the line
//inserts it in the output.txt file
BufferedReader br1 = new BufferedReader(new FileReader(inputFilename));
FileWriter tempFile = new FileWriter(outputFilename, false);
tempFile.write(""); //emptying the file
tempFile.close();
FileWriter f1 = new FileWriter(outputFilename,true);
String s1 = br1.readLine(), s2 = "";
char oldchar,newchar;
while(s1 != null)
{
for(int i = 0;i
- Call the encrypt method from the main method
- Create a file called input.txt in the "workspace/project folder/bin" folder and input the text that you want to secure/encrypt
- Run the program. The text that you entered in the input.txt can be found encrypted in output.txt
- To decrypt the text, put the encrypted text in input.txt and run the program. The decrypted version will now appear in output.txt
- (Extra credit) As you have noticed your new cryptographic program is writing your secured text in a different file.
Modify your program to write the secured text in the original file(i.e. replace the original content),
so that if you enter "Hello" in the input.txt, after running the program, "Hello" is replaced by the encrypted text.
The easiest way to accomplish is to add a new method that copies the content from output.txt to input.txt after encrypting,
so that you can call your new method after calling encrypt() in the main method.
Tip: Study encrypt() function carefully as all the required code for your new function is already there.
-
If you want to run your program from outside eclipse, for example from in your friend's laptop, put your "program name".class and
input.txt files in the same directory and run the program from command line
Submission: Email me your solutions before the deadline or show me in class.