The aim is to write an XOR cipher program using the C programming language. Your program must accept as input from the user a value between 0 and 255 to be used as the secret key, the name of the input file and the name of the output file. No line in the input file should contain more than 4096 characters. After the user would have provided their secret key, your program should read and perform an XOR cipher on the contents of the input file and write the result to the output file. If the input file has already been encrypted and the identical secret key that was used to perform the initial encryption is provided, then the contents of the output file should be deciphered into its original plain text.
Thank you so very much for all the help. Do appreciate it.
#include <iostream>
#include <sstream>
#include <fstream>
#include <string>
using namespace std;
int main(int argc, char* argv[])
{
try
{
if (argc < 4)
{
cout << argv[0] << "\n";
cout << "Usage: cipher key infile outfile\n";
}
else
{
string line;
stringstream ss;
ss << argv[1];
unsigned int key = 0;
ss >> key;
cout << "Key is " << key << "\n";
if (key > 255)
{
cerr << "Key value is to high 255 < " << key << "\n";
return 1;
}
ifstream infile(argv[2]);
ofstream outfile(argv[3]);
if (infile.is_open())
{
while (infile.good())
{
getline(infile, line);
int lineLen = line.length();
if (lineLen > 4096)
{
cerr << "String in file is too long 4096 < " << lineLen << "\n";
infile.close();
outfile.close();
return 1;
}
char toCipher[lineLen];
for (int ii = 0; ii < lineLen; ii++)
{
toCipher[ii] = line[ii] ^ key;
}
outfile << toCipher;
}
infile.close();
outfile.close();
}
}
return 0;
}
catch(...)
{
cerr << "An unknown error occured." << endl;
return 1;
}
}

New Topic/Question
Reply



MultiQuote





|