Question 3

advertisement
Introduction to programming Concepts and tools
E2005
Assignment no. 10
17/11-2005
Prepared by the students:
Henrik Loop (loop) and
Flemming Thor Hansen (flemmingth)
Question no.
Link to code
1
http://itu.dk/people/loop/lecture10/BrowseDir.java
2
http://itu.dk/people/loop/lecture10/myCrypt.java
http://itu.dk/people/loop/lecture10/decrypt.txt
http://itu.dk/people/loop/lecture10/encrypt.txt
http://itu.dk/people/loop/lecture10/message.txt
http://itu.dk/people/loop/lecture10/pad.txt
3
http://itu.dk/people/flemmingth/Assignment10/Crypt.java
Question 1:
Please find the code below:
import
import
import
import
import
tio.*;
java.io.File;
java.io.FilenameFilter;
java.io.FileFilter;
java.io.IOException;
class BrowseDir{
private static void printFiles(File dir, String dirName) throws Exception {
try { String[] children = dir.list();
for (int i=0; i<children.length; i++) {
// Get filename of file or directory
String filename = children[i];
if ((new File(dirName + File.separatorChar + filename)).isDirectory()) {
System.out.print("[Directory] : ");
} else {
System.out.print("[File] : ");
}
//System.out.println(dirName + File.separatorChar + filename);
System.out.println(filename );
}
System.out.println();
}
catch(Exception e){
System.out.println("Error with " + dirName);
System.out.println("Either directory does not exist or is not a directory");
//lej med egen error besked
//Exception ex = new Exception("fflfrlfkmlfgm");
//throw ex;
//e.printStackTrace();
throw e;
}
}
private static void doSimpleFileListing(String dirName) throws Exception {
System.out.println();
System.out.println("Simple file listing...");
System.out.println("----------------------");
File dir = new File(dirName);
printFiles(dir, dirName);
}
public static void main (String[] args) throws Exception{
String directory;
//System.out.print(args[0]);
if (args.length == 0){
//System.out.print("Please enter a directory to test: ");
directory = System.getProperty("user.dir");
//Console.in.readLine();
//System.out.println(args[0]);
}else{
directory = args[0];
}
//
Test
doSimpleFileListing(directory);
}
}
Please find screen dump below:
[loop@hulk lecture10]$ java BrowseDir
Simple file listing...
---------------------[File] : BrowseDir.class
[File] : Crypt.class
[Directory] : tio
[File] : BrowseDir.java
[File] : BrowserDir.class
[File] : ListFilesSubs$1.class
[File] : BrowseDir1.java
[File] : ListFilesSubs.java
[File] : ListFilesSubs$2.class
[File] : ListFilesSubs.class
[File] : BrowseDir.java~
[File] : BrowseDir_backup.java
[File] : myCrypt.java
[File] : message.txt
[File] : myCrypt.class
[File] : encrypt.txt
[File] : pad.txt
[File] : decrypt.txt
[loop@hulk lecture10]$ pwd
/import/home/loop/public_html/lecture10
[loop@hulk lecture10]$ java BrowseDir /import/home/loop/public_html
Simple file listing...
---------------------[Directory] : lecture7
[Directory] : tio
[File] : BulbGUIListener.class
[File] : .Rectangle.java.swp
[File] : Assignment1.doc
[File] : Assignment2.doc
[File] : Assignment2_1.doc
[File] : Assignment3.doc
[File] : Assignment4.doc
[File] : Assignment4.odt
[File] : Assignment5.doc
[File] : Assignment6.doc
[File] : Assignment_e_3.doc
[File] : Assignment_f_3.doc
[File] : Bulb.class
Question 2:
Please find our code below.
import
import
import
import
import
tio.*;
java.io.*;
java.lang.String;
java.lang.Object;
java.util.*;
public class myCrypt {
public static void main(String[] args) throws IOException {
encrypt("message.txt","pad.txt","encrypt.txt");
decrypt("encrypt.txt","pad.txt","decrypt.txt");
}
static void encrypt(String source, String pad, String target) throws IOException {
Random generator = new Random();
BufferedReader in = new BufferedReader(new FileReader(source));
String line = in.readLine();
PrintWriter outTarget = new PrintWriter(new FileWriter(target));
PrintWriter outPad = new PrintWriter(new FileWriter(pad));
while (line != null){
line = line.trim();
int index = 0;
while (index < line.length()) {
char c = line.charAt(index++);
int key = generator.nextInt(25);
if (!Character.isWhitespace(c) && Character.isLetter(c)){
outTarget.print((char)((c - 'a' + key) % 26 + 'a'));
outPad.print((char)('a'+key));
}
else{
outTarget.print(c);
outPad.print(c);
}
}
outTarget.print("\n");
outPad.print("\n");
line = in.readLine();
}
outTarget.close();
outPad.close();
}
static void decrypt(String source, String pad, String target) throws IOException {
BufferedReader inSource = new BufferedReader(new FileReader(source));
BufferedReader inPad = new BufferedReader(new FileReader(pad));
String lineSource = inSource.readLine();
String linePad = inPad.readLine();
PrintWriter out = new PrintWriter(new FileWriter(target));
while (lineSource != null){
int index = 0;
int key = 3;
while (index < lineSource.length()) {
char k = linePad.charAt(index);
key = k - 'a';
char c = lineSource.charAt(index++);
if (!Character.isWhitespace(c) && Character.isLetter(c)){
out.print((char)((c-'a'+26-key)%26+'a'));
}
else{
out.print(c);
}
}
out.print("\n");
lineSource = inSource.readLine();
linePad = inPad.readLine();
}
out.close();
}
}
Question 3:
Please find what our time allowed us to make on question 3 below.
// Crypt.class
import java.io.*;
import tio.*;
import java.lang.String;
class Crypt {
public static void main ( String args[] ) throws IOException {
int I; // counter for input arguments
if (args.length != 4) // Error if not excactly 4 arguments
System.out.println ("Sorry wrong number of arguments. 4 expected.");
else {
for ( I = 0; I < args.length; I++) {
if (args[0].equals("-d")) { // Decryption of file
decrypt (args[1], args[2], args[3]);
}
else {
if (args[0].equals("-e")) { //Encryption of file
encrypt (args[1], args[2], args[3]);
}
else { // Unknows command. Notify user
System.out.println ("Unknown command. Please try again.");
}
}
}
}
}
public static void decrypt (String source, String pad, String target)
throws IOException {
ReadInput sourceFile = new ReadInput(source);
ReadInput padFile = new ReadInput(pad);
PrintWriter clearFile = new PrintWriter(new FileWriter(target));
while (sourceFile.hasMoreElements()) { //Read an entire line in each file
String inSource = sourceFile.readLine();
String inPad = padFile.readLine();
for (int i=0; i < inSource.length(); i++) { // Decrypt each character
char inChar = inSource.charAt(i);
char inPadchar = inPad.charAt(i);
if (Character.isLetter(inChar)) { //Coded characters are decoded
int key = (int)(inPadchar - 'a'); // Calculate the key and text
char clearChar = (char)((inChar - 'a' + 26 - key)%26 + 'a');
clearFile.print(clearChar); // write result to output file
}
else clearFile.print(inChar); //just print the character
}
clearFile.println();
}
clearFile.close();
}
public static void encrypt(String source, String pad, String target)
throws IOException {
ReadInput inFile = new ReadInput(source);
PrintWriter padFile = new PrintWriter(new FileWriter(pad));
PrintWriter targetFile = new PrintWriter(new FileWriter(target));
while (inFile.hasMoreElements()) { //read entire line of input file
String inLine = inFile.readLine();
for (int i=0; i < inLine.length(); i++) { //for each character in line
char readChar = inLine.charAt(i);
if (Character.isLetter(readChar)) { // decode real letters
int key = (int)(26*Math.random()); //calculate random key and chipher
char targetChar = (char)(int)((readChar - 'a' + key)%26 + 'a');
targetFile.print(targetChar); //write encoded character to file
char padChar = (char)('a' + key); // calculate pad character
padFile.print(padChar);//write pad character
}
else { //Just print the character
padFile.print(readChar);
targetFile.print(readChar);
}
}
padFile.println(); // finalize the line
targetFile.println(); // finalize the line
}
padFile.close(); //close output files
targetFile.close();
}
}
Download