diff --git a/PA01-0906/EncryptionUtility.c b/PA01-0906/EncryptionUtility.c index c16aa9e..253cfc0 100644 --- a/PA01-0906/EncryptionUtility.c +++ b/PA01-0906/EncryptionUtility.c @@ -84,6 +84,45 @@ Dependencies: as needed */ 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. } @@ -146,9 +185,51 @@ Dependencies: as needed, File_Output_Utility tools */ void downloadData( const char *fileName, const SquareArrayType data ) { + bool fileOpenCheck; + int oneDimensionIndex, twoDimensionIndex; + int currentData; + int checkDigits, tempDigits; // open file. + fileOpenCheck = openOutputFile(fileName); // 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. + closeOutputFile(); // no return. } @@ -177,65 +258,40 @@ SquareArrayType *encryptData( const char *toEncrypt ) int twoDimensionIndex; int differenceIndex; int currentCharacter = 0; - int flagEndString = 0; - // 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--) + for(oneDimensionIndex = stringLenRoot * 2; oneDimensionIndex >= 0; oneDimensionIndex--) { - // - differenceIndex = oneDimensionIndex - twoDimensionIndex; - if(differenceIndex < stringLenRoot && twoDimensionIndex < stringLenRoot) + // does one dimensional indices + for(twoDimensionIndex = oneDimensionIndex; twoDimensionIndex >= 0; twoDimensionIndex--) { - // printf("C "); - // second dimension to move through array - if(toEncrypt[currentCharacter] == NULL_CHAR) + // does two dimensional indices + differenceIndex = oneDimensionIndex - twoDimensionIndex; + if(differenceIndex < stringLenRoot && twoDimensionIndex < stringLenRoot) { - // if the null character is there, replace with the Unprintable, set flag to one. - encryptedData->array[twoDimensionIndex][differenceIndex] = UNPRINTABLE_CHAR_VALUE; + // printf("C "); + // 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; - 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. // returns a Square Array. return encryptedData; @@ -355,6 +411,32 @@ Dependencies: as needed, File_Input_Utility tools */ 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. - return NULL; + return readArray; } diff --git a/PA01-0906/PA01_Main.c b/PA01-0906/PA01_Main.c index 7b33ebb..9cd688f 100644 --- a/PA01-0906/PA01_Main.c +++ b/PA01-0906/PA01_Main.c @@ -43,12 +43,12 @@ int main( int argc, char *args[] ) printf( "\nDisplay encryption as characters:\n" ); displayArray( *encryptedInput, DISPLAY_CHARS ); - /* + printf( "\nDecrypting the input string\n" ); decryptData( decryptedStr, encryptedInput ); printf( "\nDecrypted string: %s\n", decryptedStr ); - + printf( "\nDownloading encryption\n" ); promptForString( "\nEnter file name for download: ", fileName ); downloadData( fileName, *encryptedInput ); @@ -64,12 +64,11 @@ int main( int argc, char *args[] ) decryptData( stringFromFile, uploadedInput ); printf( "\nUploaded string: %s\n", stringFromFile ); - */ // end program // clear arrays - // encryptedInput = clearArrayType( encryptedInput ); + encryptedInput = clearArrayType( encryptedInput ); // display end of program printf( "\nProgram End\n" );