1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39
|
#include <iostream>
#include <cstdlib>
#include <cstdio>
#include <string>
#include <fstream>
using namespace std;
int main(int argc, char** argv)
{
if (argc < 2)
{
cout << "Error: First command line argument must be the output filename." << endl;
return -1;
}
char* text = new char;
char key[8] = { 1, 2, 3, 4, 5, 6, 7, 8 };
char* encrypted = new char;
printf("Enter the text to be encrypted: \n");
int x = 0;
scanf("%s", &text);
for (int i = 0; i < strlen(text); i++) // This line causes the error
{
encrypted[i] = text[i] ^ key[x];
if (x++>7) // Key is and 8 element array, so we need another counter to prevent array index out of bounds exception
{
x = 0;
}
}
FILE* file = fopen(argv[1], "w");
fprintf(file, "%s", encrypted); //write to file
return 0;
}
|