Finished PA01
+- EncryptionUtility.c +- PA01_Main.c
This commit is contained in:
parent
dc1a4f1638
commit
c1db9f52b8
2 changed files with 138 additions and 57 deletions
|
|
@ -84,6 +84,45 @@ Dependencies: as needed
|
||||||
*/
|
*/
|
||||||
void decryptData( char *decryptStr, SquareArrayType *data )
|
void decryptData( char *decryptStr, SquareArrayType *data )
|
||||||
{
|
{
|
||||||
|
// opposite the encrypt data.
|
||||||
|
// extract the size data from the array
|
||||||
|
int arraySizeData = data->sideSize;
|
||||||
|
// iterate through the array
|
||||||
|
int oneDimensionIndex;
|
||||||
|
int twoDimensionIndex;
|
||||||
|
int differenceIndex;
|
||||||
|
int currentCharacter = 0;
|
||||||
|
int stringComplete = 0;
|
||||||
|
for(oneDimensionIndex = arraySizeData * 2; oneDimensionIndex >= 0; oneDimensionIndex--)
|
||||||
|
{
|
||||||
|
// does one dimensional indices
|
||||||
|
for(twoDimensionIndex = oneDimensionIndex; twoDimensionIndex >= 0; twoDimensionIndex--)
|
||||||
|
{
|
||||||
|
// does two dimensional indices
|
||||||
|
differenceIndex = oneDimensionIndex - twoDimensionIndex;
|
||||||
|
if(differenceIndex < arraySizeData && twoDimensionIndex < arraySizeData)
|
||||||
|
{
|
||||||
|
// printf("C ");
|
||||||
|
// second dimension to move through array
|
||||||
|
if(stringComplete == 0)
|
||||||
|
{
|
||||||
|
if(data->array[twoDimensionIndex][differenceIndex] == UNPRINTABLE_CHAR_VALUE)
|
||||||
|
{
|
||||||
|
// check for the unprintable character
|
||||||
|
decryptStr[currentCharacter] = NULL_CHAR;
|
||||||
|
stringComplete = 1;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
// put the character into the string.
|
||||||
|
decryptStr[currentCharacter] = data->array[twoDimensionIndex][differenceIndex];
|
||||||
|
currentCharacter = currentCharacter + 1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// printf("\n");
|
||||||
|
}
|
||||||
// no return.
|
// no return.
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -146,9 +185,51 @@ Dependencies: as needed, File_Output_Utility tools
|
||||||
*/
|
*/
|
||||||
void downloadData( const char *fileName, const SquareArrayType data )
|
void downloadData( const char *fileName, const SquareArrayType data )
|
||||||
{
|
{
|
||||||
|
bool fileOpenCheck;
|
||||||
|
int oneDimensionIndex, twoDimensionIndex;
|
||||||
|
int currentData;
|
||||||
|
int checkDigits, tempDigits;
|
||||||
// open file.
|
// open file.
|
||||||
|
fileOpenCheck = openOutputFile(fileName);
|
||||||
// write file.
|
// write file.
|
||||||
|
if(fileOpenCheck)
|
||||||
|
{
|
||||||
|
// write the size of the array as the first character.
|
||||||
|
writeIntegerToFile(data.sideSize);
|
||||||
|
// write a new line
|
||||||
|
writeEndlineToFile();
|
||||||
|
// iterate through array outputting each integer.
|
||||||
|
for(oneDimensionIndex = 0; oneDimensionIndex < data.sideSize; oneDimensionIndex++)
|
||||||
|
{
|
||||||
|
//
|
||||||
|
for(twoDimensionIndex = 0; twoDimensionIndex < data.sideSize; twoDimensionIndex++)
|
||||||
|
{
|
||||||
|
//
|
||||||
|
currentData = data.array[oneDimensionIndex][twoDimensionIndex];
|
||||||
|
// check to see if the integer is two digits, if it is, add a 0 before.
|
||||||
|
checkDigits = 1;
|
||||||
|
tempDigits = currentData;
|
||||||
|
while(tempDigits > 9)
|
||||||
|
{
|
||||||
|
tempDigits = tempDigits / 10;
|
||||||
|
checkDigits = checkDigits + 1;
|
||||||
|
}
|
||||||
|
if(checkDigits == 2)
|
||||||
|
{
|
||||||
|
writeCharacterToFile(ZERO_CHAR);
|
||||||
|
writeIntegerToFile(currentData);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
writeIntegerToFile(currentData);
|
||||||
|
}
|
||||||
|
writeCharacterToFile(SPACE);
|
||||||
|
}
|
||||||
|
writeEndlineToFile();
|
||||||
|
}
|
||||||
|
}
|
||||||
// close file.
|
// close file.
|
||||||
|
closeOutputFile();
|
||||||
// no return.
|
// no return.
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -177,65 +258,40 @@ SquareArrayType *encryptData( const char *toEncrypt )
|
||||||
int twoDimensionIndex;
|
int twoDimensionIndex;
|
||||||
int differenceIndex;
|
int differenceIndex;
|
||||||
int currentCharacter = 0;
|
int currentCharacter = 0;
|
||||||
int flagEndString = 0;
|
for(oneDimensionIndex = stringLenRoot * 2; oneDimensionIndex >= 0; oneDimensionIndex--)
|
||||||
// for(oneDimensionIndex = stringLenRoot - 1; oneDimensionIndex >= 0; oneDimensionIndex--)
|
|
||||||
// {
|
|
||||||
// // one dimension to move through array
|
|
||||||
// for(twoDimensionIndex = stringLenRoot - 1; twoDimensionIndex >= 0; twoDimensionIndex--)
|
|
||||||
// {
|
|
||||||
// if(flagEndString == 1)
|
|
||||||
// {
|
|
||||||
// // check the flag, if it is set, then start putting random data into rest of the array.
|
|
||||||
// encryptedData->array[oneDimensionIndex][twoDimensionIndex] = getRandomLetterValue();
|
|
||||||
// }
|
|
||||||
// // second dimension to move through array
|
|
||||||
// if(toEncrypt[currentCharacter] == NULL_CHAR)
|
|
||||||
// {
|
|
||||||
// // if the null character is there, replace with the Unprintable, set flag to one.
|
|
||||||
// encryptedData->array[oneDimensionIndex][twoDimensionIndex] = UNPRINTABLE_CHAR_VALUE;
|
|
||||||
// flagEndString = 1;
|
|
||||||
// }
|
|
||||||
// else
|
|
||||||
// {
|
|
||||||
// // otherwise, continue putting the data into the 2d array.
|
|
||||||
// encryptedData->array[oneDimensionIndex][twoDimensionIndex] = toEncrypt[currentCharacter];
|
|
||||||
// }
|
|
||||||
// currentCharacter = currentCharacter + 1;
|
|
||||||
// }
|
|
||||||
// }
|
|
||||||
for(oneDimensionIndex = stringLenRoot * 2; oneDimensionIndex >= 0; oneDimensionIndex--)
|
|
||||||
{
|
|
||||||
//
|
|
||||||
for(twoDimensionIndex = oneDimensionIndex; twoDimensionIndex >= 0; twoDimensionIndex--)
|
|
||||||
{
|
{
|
||||||
//
|
// does one dimensional indices
|
||||||
differenceIndex = oneDimensionIndex - twoDimensionIndex;
|
for(twoDimensionIndex = oneDimensionIndex; twoDimensionIndex >= 0; twoDimensionIndex--)
|
||||||
if(differenceIndex < stringLenRoot && twoDimensionIndex < stringLenRoot)
|
|
||||||
{
|
{
|
||||||
// printf("C ");
|
// does two dimensional indices
|
||||||
// second dimension to move through array
|
differenceIndex = oneDimensionIndex - twoDimensionIndex;
|
||||||
if(toEncrypt[currentCharacter] == NULL_CHAR)
|
if(differenceIndex < stringLenRoot && twoDimensionIndex < stringLenRoot)
|
||||||
{
|
{
|
||||||
// if the null character is there, replace with the Unprintable, set flag to one.
|
// printf("C ");
|
||||||
encryptedData->array[twoDimensionIndex][differenceIndex] = UNPRINTABLE_CHAR_VALUE;
|
// second dimension to move through array
|
||||||
|
if(currentCharacter >= stringLen)
|
||||||
|
{
|
||||||
|
if(currentCharacter == stringLen)
|
||||||
|
{
|
||||||
|
// if the null character is there, replace with the Unprintable, set flag to one.
|
||||||
|
encryptedData->array[twoDimensionIndex][differenceIndex] = UNPRINTABLE_CHAR_VALUE;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
// if not the null, put random data into array
|
||||||
|
encryptedData->array[twoDimensionIndex][differenceIndex] = getRandomLetterValue();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
// otherwise, continue putting the data into the 2d array.
|
||||||
|
encryptedData->array[twoDimensionIndex][differenceIndex] = toEncrypt[currentCharacter];
|
||||||
|
}
|
||||||
currentCharacter = currentCharacter + 1;
|
currentCharacter = currentCharacter + 1;
|
||||||
flagEndString = 1;
|
|
||||||
}
|
}
|
||||||
else
|
|
||||||
{
|
|
||||||
// otherwise, continue putting the data into the 2d array.
|
|
||||||
encryptedData->array[twoDimensionIndex][differenceIndex] = toEncrypt[currentCharacter];
|
|
||||||
}
|
|
||||||
if(flagEndString == 1)
|
|
||||||
{
|
|
||||||
// check the flag, if it is set, then start putting random data into rest of the array.
|
|
||||||
encryptedData->array[twoDimensionIndex][differenceIndex] = getRandomLetterValue();
|
|
||||||
}
|
|
||||||
currentCharacter = currentCharacter + 1;
|
|
||||||
}
|
}
|
||||||
|
//printf("\n");
|
||||||
}
|
}
|
||||||
//printf("\n");
|
|
||||||
}
|
|
||||||
// once null character is reached, put the 'unprintable character' then random data afterward.
|
// once null character is reached, put the 'unprintable character' then random data afterward.
|
||||||
// returns a Square Array.
|
// returns a Square Array.
|
||||||
return encryptedData;
|
return encryptedData;
|
||||||
|
|
@ -355,6 +411,32 @@ Dependencies: as needed, File_Input_Utility tools
|
||||||
*/
|
*/
|
||||||
SquareArrayType *uploadData( const char *fileName )
|
SquareArrayType *uploadData( const char *fileName )
|
||||||
{
|
{
|
||||||
|
bool fileOpenCheck;
|
||||||
|
int arraySizeFile;
|
||||||
|
int oneDimensionIndex, twoDimensionIndex;
|
||||||
|
SquareArrayType *readArray;
|
||||||
|
// open file.
|
||||||
|
fileOpenCheck = openInputFile(fileName);
|
||||||
|
// read file.
|
||||||
|
if(fileOpenCheck)
|
||||||
|
{
|
||||||
|
// read the size of the array as the first character.
|
||||||
|
arraySizeFile = readIntegerFromFile();
|
||||||
|
// create the square array
|
||||||
|
readArray = createArrayType(arraySizeFile);
|
||||||
|
// read integer, iterate through array inputting each integer.
|
||||||
|
for(oneDimensionIndex = 0; oneDimensionIndex < arraySizeFile; oneDimensionIndex++)
|
||||||
|
{
|
||||||
|
//
|
||||||
|
for(twoDimensionIndex = 0; twoDimensionIndex < arraySizeFile; twoDimensionIndex++)
|
||||||
|
{
|
||||||
|
//
|
||||||
|
readArray->array[oneDimensionIndex][twoDimensionIndex] = readIntegerFromFile();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// close file.
|
||||||
|
closeInputFile();
|
||||||
// returns a Square Array.
|
// returns a Square Array.
|
||||||
return NULL;
|
return readArray;
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -43,12 +43,12 @@ int main( int argc, char *args[] )
|
||||||
|
|
||||||
printf( "\nDisplay encryption as characters:\n" );
|
printf( "\nDisplay encryption as characters:\n" );
|
||||||
displayArray( *encryptedInput, DISPLAY_CHARS );
|
displayArray( *encryptedInput, DISPLAY_CHARS );
|
||||||
/*
|
|
||||||
printf( "\nDecrypting the input string\n" );
|
printf( "\nDecrypting the input string\n" );
|
||||||
decryptData( decryptedStr, encryptedInput );
|
decryptData( decryptedStr, encryptedInput );
|
||||||
|
|
||||||
printf( "\nDecrypted string: %s\n", decryptedStr );
|
printf( "\nDecrypted string: %s\n", decryptedStr );
|
||||||
|
|
||||||
printf( "\nDownloading encryption\n" );
|
printf( "\nDownloading encryption\n" );
|
||||||
promptForString( "\nEnter file name for download: ", fileName );
|
promptForString( "\nEnter file name for download: ", fileName );
|
||||||
downloadData( fileName, *encryptedInput );
|
downloadData( fileName, *encryptedInput );
|
||||||
|
|
@ -64,12 +64,11 @@ int main( int argc, char *args[] )
|
||||||
decryptData( stringFromFile, uploadedInput );
|
decryptData( stringFromFile, uploadedInput );
|
||||||
|
|
||||||
printf( "\nUploaded string: %s\n", stringFromFile );
|
printf( "\nUploaded string: %s\n", stringFromFile );
|
||||||
*/
|
|
||||||
|
|
||||||
// end program
|
// end program
|
||||||
|
|
||||||
// clear arrays
|
// clear arrays
|
||||||
// encryptedInput = clearArrayType( encryptedInput );
|
encryptedInput = clearArrayType( encryptedInput );
|
||||||
|
|
||||||
// display end of program
|
// display end of program
|
||||||
printf( "\nProgram End\n" );
|
printf( "\nProgram End\n" );
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue