I am working on an Encryption/Decryption program for class and have run into a problem. I seem to have gotten the encryption correct, but I am unable to decrypt what was encrypted in order to get an output equal to what originally went in.
Here is the code I have so far:
#include <iostream>
#include <fstream>
using namespace std;
int main()
{
char userInput[24], inputFileName[51], encryptionFileName[51], outputFileName[51], encChar, decChar;
int weightedSum = 0, modSum = 0, currentPos, intValue, fileValue, encVal, endEnc, decVal, endDec;
fstream inputFile;
fstream encryptionFile;
fstream outputFile;
cout << "Input string: ";
cin >> userInput;
//**********Create Encrypt/Decrypt Value*************//
for(int i = 0; i <= 23; i++)
{
intValue = int(userInput[i]) - int('0');
currentPos = 1 + i;
weightedSum += intValue * currentPos;
}
modSum = weightedSum % 7043;
cout << "modSum: " << modSum;
//***********Encryption Starts Here*************//
cout << "Enter the name of the input file: ";
cin >> inputFileName;
inputFile.open(inputFileName, ios::in);
if(!inputFile)
{
cout << "\n\n\nThe file titled \"" << inputFileName << "\" cannot be opened or does not exist." << endl;
cout << "\n\nPress any key to exit . . .";
_getch();
return 1;
}
cout << "Enter the name of an encryption file: ";
cin >> encryptionFileName;
encryptionFile.open(encryptionFileName, ios::out);
while(inputFile.get(encChar))
{
encVal = int(encChar) - int('0');
endEnc = encVal + modSum;
encryptionFile << endEnc;
}
inputFile.close();
encryptionFile.close();
//********DECRYPTION STARTS HERE**********//
cout << "Name of output file: ";
cin >> outputFileName;
outputFile.open(outputFileName, ios::out);
encryptionFile.open(encryptionFileName, ios::in);
while(encryptionFile.get(decChar))
{
decVal = decChar - modSum;
endDec = int(endDec) + int('0');
outputFile << endDec;
}
encryptionFile.close();
outputFile.close();
}
Here is the assignment that I was given:
Simple encryption and decryption of a text file. Allow the user to input any string of up to 23 characters of digits or upper or lower case letters. Calculate the weighted sum of input using the ascii values of the input character. Take the weighted sum and mod by 7043 to get your encryption/decryption value. Ask for the names of an input and encryption file. Both will be plain text, input must exist, encryption can be created if necessary. Read each character of the input file, take the ascii value of the input add the encryption encryption/decryption value and write that character into the output file. The final output file will be nothing but a string of numbers. Close the files. Ask for the name of an output file, reopen the encryption file and create the output file. Read in the encryption file, decrypt the code and write the result back into the output file. Close both files. Reopen the input file and the output file. Compare the files character by character to confirm the code works.
Sorry for the long post. I am completely open to any advice and/or criticism. Right now I am just needing a little advice as to what I am doing wrong with the decryption part, and if you see something that I could improve upon I am open to that also, as I know this code is not perfect or clean. Thank you so much for your time and help.