PDA

View Full Version : need help "decrypting" a file


uniquity
06-07-2009, 07:18 PM
I'm trying to write a program that reads in a file (a list of words from the Scrabble dictionary) and writes + sorts the dictionary into an arraylist of Strings. However, a catch in the program is that some of the files that are to be read in are encrypted. The way the files were encrypted is as follows:

The encryption of the words in the provided files is relatively simple. The Random class in the Java Standard Library was used with a seed. Recall that a seeded Random object will always produce the identical series of numbers as a different Random object created with the same seed. To encrypt the data an array of 127 bytes was produced with the first 127 unique numbers generated by a seeded Random object (seed known and provided to you) using the nextInt(int n) method of Random where n is the exlusive upper-bound of the number returned - use 128 as that upper-bound. Any numbers in the sequence that had already been produced were ignored - no duplicate values in the array! This array was then used to map each byte in an unordered text file of words (separated by newlines) found in the Official Scrabble Dictionary to a different value as follows:

newByte = map[oldByte];

You will need to reverse this mapping to decrypt the data in the provided input files. The integer-value of oldByte is used as an index into the array of random values and whatever value is there is used as the encryped value for that byte. Notice that the seed used to encrypt the data is included in the names of the files you have been given - use that number as the seed when creating a Random object, then generate an array containing the same 127 "random" values. You can then use the array to reverse the mapping and reconstruct the words. Notice that the newline character (also encrypted) is in the encryped files between each word .

I keep thinking I'm getting close but can never quite put all the pieces together to write code.

The code I have so far is:
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.ObjectOutputStream;
import java.util.ArrayList;
import java.util.Scanner;
import java.util.Random;
import java.util.Iterator;

public class Dictionary implements Iterable<String>
{
//variables
private ArrayList<String> dictionary;

//constructor
public Dictionary( String fileName, boolean sorted ) throws java.io.FileNotFoundException
{
dictionary = new ArrayList<String>();

try
{
FileInputStream fis = new FileInputStream( fileName );

Scanner sc = new Scanner( fis );

while( sc.hasNext() )
{
dictionary.add( sc.nextLine() );
}

if( !sorted )
{
int n = dictionary.size();
boolean swapped;

do
{
swapped = false;
n -= 1;
for( int i = 0; i <= n - 1; i++ )
{
if( dictionary.get( i ).compareTo( dictionary.get( i + 1 ) ) > 0 )
{
swap( i, i + 1 );
swapped = true;
}
}
}
while( swapped );
}

sc.close(); //close scanner
try
{
fis.close(); //close file
}
catch( java.io.IOException ioe )
{
System.out.println( "An IO Exception occurred." );
}

}
catch( java.io.FileNotFoundException fnfe )
{
System.out.println( "Invalid file name." );
}

} //end constructor Dictionary( fileName, sorted )

public Dictionary( String fileName, boolean sorted, long seed )
throws java.io.FileNotFoundException, java.io.IOException
{
dictionary = new ArrayList<String>();
ArrayList<String> encryptDict = new ArrayList<String>();
String decryptWord = new String();

int ctr = 0;
Random rand = new Random( seed );
int[] randArray = new int[128];

while( ctr != 128 )
{
int temp = rand.nextInt( 128 );
boolean alreadyExists = false;

for( int ctr2 = 0; ctr2 < randArray.length - 1; ctr2++ )
{
if( temp == randArray[ctr2] )
{
alreadyExists = true;
}
} //end for

if( alreadyExists == false )
{
randArray[ctr] = temp;
ctr++;
}
} //end while

try
{
FileInputStream fis = new FileInputStream( fileName );

Scanner sc = new Scanner( fis );

while( sc.hasNext() )
{
Byte temp = sc.nextByte();

for( int j = 0; j < randArray.length; j++ )
{
if( temp == '\n' )
{
dictionary.add( decryptWord );
System.out.println( decryptWord );
}
else if( randArray[j] == (int)temp )
{
decryptWord += (char)randArray[j];
}
}
}

//String s += (char)randArray[ctr]????

if( !sorted )
{
int n = dictionary.size();
boolean swapped;

do
{
swapped = false;
n -= 1;
for( int i = 0; i <= n - 1; i++ )
{
if( dictionary.get( i ).compareTo( dictionary.get( i + 1 ) ) > 0 )
{
swap( i, i + 1 );
swapped = true;
}
}
}
while( swapped );
}

}
catch( java.io.FileNotFoundException fnfe )
{
System.out.println( "Invalid file name." );
}


} //end constructor Dictionary( fileName, sorted, seed )

This is just the code for the first part of the class I'm writing...other methods aren't shown. The first constructor in that code should work as expected, its the second constructor where it takes in an encrypted file --that's where I'm having trouble. Any help would be greatly appreciated. Thanks!