PDA

View Full Version : Reading in an encrypted file and decrypting it


uniquity
06-10-2009, 07:44 PM
**This is for an assignment for my 102 class**

You may have seen my previous post for constructing a array of unique random numbers from 0-127 and this problem ties in with it, its just the next step in the assignment. Pretty much, my instructor provided us with files with a given seed. The seed was used to produce an array of random unique numbers that were then mapped to another array so as to encrypt it.

So far, I've tried using a FileInputStream with Scanner and my instructor told me that it wasn't the best idea when working with bytes and to try using just an InputStream of somekind. I stuck with a FileInputStream and used the read() method. To help you understand the problem more, I've quoted the instructions from my instructor as to how he encrypted the file.

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 .

This is the code I have so far to try to decrypt it.

try
{
FileInputStream fis = new FileInputStream( fileName );

while( fis.available() != 0 )
{

int tempEncrypted = fis.read();
System.out.println( tempEncrypted );

for( int j = 0; j < randArray.length - 1; j++ )
{
if( (char)tempEncrypted == '\n' )
{
dictionary.add( decryptWord );
System.out.println( "Word added to dictionary." );
}
else if( tempEncrypted == randArray[ctr] )
{
decryptWord += (char)randArray[tempEncrypted];
}
// 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 )
{
sort();
}

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

I'm not looking for code or anything, but maybe a better library to look in for this task or some debugging tips. Any help would be appreciated, thanks!