In: Computer Science
- Students must develop the code required to create a square two-dimensional array just large enough to store a given string in encrypted form using the given encryption algorithm. The character used after the end of the encrypted string is the ASCII delete key, set as final UNPRINTABLE_CHAR_VALUE. After this value is stored, the encryption process fills the remaining elements with random values
- Students must develop the code to decrypt a given square two-dimensional array and using the given encryption algorithm
- Students must develop other supporting methods as specified (below) to manage the encryption operations*
- Students will also document all classes and methods using the Javadoc commenting process; comments are not required to be exactly the same as found in the supporting document (below) but they should be semantically equivalent
- Students will upload the EncryptionClass.java file to this assignment; any other uploaded files will result in a reduction of the project grade, and in some cases, may cause a complete loss of credit
- Assignment Specification (Javadoc output for the project):
p1_package
Class EncryptionClass
FileIoToolsForEncryptionClass.java
import
java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
public class
EncryptionClass
{/**
* Constant for maximum input char
limit
*/
private final int MAX_INPUT_CHARS = 256;
/**
* Constant for unprintable char value used
as message end char
*/
private final int UNPRINTABLE_CHAR_VALUE = 127;
// ASCII for delete key
/**
* Constant for minus sign used in
getAnInt
*/
private final char MINUS_SIGN = '-';
/**
* Class Global FileReader variable so
methods can be used
*/
private FileReader fileIn;
/**
* Downloads encrypted data to file
* <p>
* Note: No action taken if array is
empty
* @param fileName String object holding
file name to use
*/
void downloadData( String fileName )
{FileWriter
toFile;
int rowIndex, colIndex;
if( arrSide > 0
)
{
try
{
toFile = new FileWriter( fileName );
toFile.write( "" + arrSide + "\r\n" );
for( rowIndex = 0; rowIndex < arrSide; rowIndex++ )
{
for( colIndex = 0; colIndex < arrSide; colIndex++ )
{
if( encryptedArr[ rowIndex ][ colIndex ] < 100 )
{
toFile.write( "0" );
}
toFile.write(" "+ encryptedArr[ rowIndex ][ colIndex ] + " "
);
}
toFile.write( "\r\n" );
}
toFile.flush();
toFile.close();
}
catch( IOException ioe )
{
ioe.printStackTrace();
}
}
}
/**
* gets an integer from the input
stream
*
* @param maxLength maximum length of
characters
* input to capture the integer
*
* @return integer captured from file
*/
private int getAnInt( int maxLength )
{
int inCharInt;
int index = 0;
String strBuffer =
"";
int intValue;
boolean negativeFlag =
false;
try
{
inCharInt = fileIn.read();
// clear space up to number
while( index < maxLength && !charInString(
(char)inCharInt,
"0123456789+-" ) )
{
inCharInt =
fileIn.read();
index++;
}
if( inCharInt == MINUS_SIGN )
{
negativeFlag = true;
inCharInt = fileIn.read();
}
while( charInString( (char)inCharInt, "0123456789" ) )
{
strBuffer += (char)( inCharInt );
index++;
inCharInt = fileIn.read();
}
}
catch( IOException ioe
)
{
System.out.println( "INPUT ERROR: Failure to capture character"
);
strBuffer = "";
}
intValue =
Integer.parseInt( strBuffer );
if( negativeFlag )
{
intValue *= -1;
}
return intValue;
}
/**
* Uploads data from file holding a square
array
*
* @param fileName String object holding
file name
*/
void uploadData( String fileName )
{
int rowIndex,
colIndex;
try
{
// Open FileReader
fileIn = new FileReader( fileName );
// get side length
arrSide = getAnInt( MAX_INPUT_CHARS
);
encryptedArr = new int[ arrSide ][ arrSide ];
for( rowIndex = 0; rowIndex < arrSide; rowIndex++ )
{
for( colIndex = 0; colIndex < arrSide; colIndex++ )
{
encryptedArr[ rowIndex ][ colIndex ]
= getAnInt( MAX_INPUT_CHARS );
}
}
fileIn.close();
}
// for opening
file
catch(
FileNotFoundException fnfe )
{
fnfe.printStackTrace();
}
// for closing
file
catch (IOException
ioe)
{
ioe.printStackTrace();
}
}
}
p1_package
Class EncryptionClass
FileIoToolsForEncryptionClass.java
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
public class EncryptionClass
{/**
* Constant for maximum input char
limit
*/
private final int MAX_INPUT_CHARS = 256;
/**
* Constant for unprintable char value used
as message end char
*/
private final int UNPRINTABLE_CHAR_VALUE = 127;
// ASCII for delete key
/**
* Constant for minus sign used in
getAnInt
*/
private final char MINUS_SIGN = '-';
/**
* Class Global FileReader variable so
methods can be used
*/
private FileReader fileIn;
/**
* Downloads encrypted data to file
* <p>
* Note: No action taken if array is
empty
* @param fileName String object holding
file name to use
*/
void downloadData( String fileName )
{FileWriter
toFile;
int rowIndex, colIndex;
if( arrSide > 0
)
{
try
{
toFile = new FileWriter( fileName );
toFile.write( "" + arrSide + "\r\n" );
for( rowIndex = 0; rowIndex < arrSide; rowIndex++ )
{
for( colIndex = 0; colIndex < arrSide; colIndex++ )
{
if( encryptedArr[ rowIndex ][ colIndex ] < 100 )
{
toFile.write( "0" );
}
toFile.write(" "+ encryptedArr[ rowIndex ][ colIndex ] + " "
);
}
toFile.write( "\r\n" );
}
toFile.flush();
toFile.close();
}
catch( IOException ioe )
{
ioe.printStackTrace();
}
}
}
/**
* gets an integer from the input
stream
*
* @param maxLength maximum length of
characters
* input to capture the integer
*
* @return integer captured from file
*/
private int getAnInt( int maxLength )
{
int inCharInt;
int index = 0;
String strBuffer =
"";
int intValue;
boolean negativeFlag =
false;
try
{
inCharInt = fileIn.read();
// clear space up to number
while( index < maxLength && !charInString(
(char)inCharInt,
"0123456789+-" ) )
{
inCharInt =
fileIn.read();
index++;
}
if( inCharInt == MINUS_SIGN )
{
negativeFlag = true;
inCharInt = fileIn.read();
}
while( charInString( (char)inCharInt, "0123456789" ) )
{
strBuffer += (char)( inCharInt );
index++;
inCharInt = fileIn.read();
}
}
catch( IOException ioe
)
{
System.out.println( "INPUT ERROR: Failure to capture character"
);
strBuffer = "";
}
intValue =
Integer.parseInt( strBuffer );
if( negativeFlag )
{
intValue *= -1;
}
return intValue;
}
/**
* Uploads data from file holding a square
array
*
* @param fileName String object holding
file name
*/
void uploadData( String fileName )
{
int rowIndex,
colIndex;
try
{
// Open FileReader
fileIn = new FileReader( fileName );
// get side length
arrSide = getAnInt( MAX_INPUT_CHARS
);
encryptedArr = new int[ arrSide ][ arrSide ];
for( rowIndex = 0; rowIndex < arrSide; rowIndex++ )
{
for( colIndex = 0; colIndex < arrSide; colIndex++ )
{
encryptedArr[ rowIndex ][ colIndex ]
= getAnInt( MAX_INPUT_CHARS );
}
}
fileIn.close();
}
// for opening
file
catch(
FileNotFoundException fnfe )
{
fnfe.printStackTrace();
}
// for closing
file
catch (IOException
ioe)
{
ioe.printStackTrace();
}
}
}