jeudi 2 novembre 2017

Encrypting file (writing varying encryption values to file while using encryption values on plain text, then outputting to cipher text)

For the sake of convenience I will only show my encryption function (assume that the rest works as intended.) The idea is that the following: "aB4cTx5. 90" would be encrypted to values based on the key. The random generator will generate integers for each character that can be changed (alphas and digits). So if the key made 9 integers: "1 2 3 4 5 6 7 8 9", the encryption would look like: "bD7gYD12.179" (alphas are the character + the key value % 26, digits are character + key value, punctuation is left alone, whitespace values are removed.) The loop needs to run through every character in the file until it reaches EOF, and print out the changed/unchanged values respectively to the output file (the ciphertext.) It also needs to print out the key values in order to the key output file. My issue is that currently it doesn't generate enough keys (if I have 3 characters i.e. "abc" it only makes 2 keys and outputs them), and it only changes the first character (so "abc" with a key of "3 5 7" would become "dbc".) Lastly, the numbers simply do not change correctly.

I'm sorry if this is really tough. I typically don't like asking for help on my homework but I am utterly lost and could use a miracle.

void encryption(ifstream& fileInEn, ofstream& fileOut, char promptUnlock) {
ofstream fileOutKey;
ifstream fileInKey;
char fileOutKeyName[33], c;
int keyStore, num;
keyStore, num = 0;
srand(time(NULL));
string temp, total;
temp = "", total = "";

cout << "Please enter the ouput file for the key: ";
cin >> fileOutKeyName;
fileOutKey.open(fileOutKeyName);
if(fileOutKey.fail())
{
    cout << "Output key file opening failed." << endl;
    exit(EXIT_FAILURE);
}
fileInKey.open(fileOutKeyName);
if(fileInKey.fail())
{
    cout << "Input key file opening failed." << endl;
    exit(EXIT_FAILURE);
}
keyStore = rand() % 277 + 3;
fileOutKey << keyStore << " ";
if(promptUnlock == 'e')
{
    fileInEn.get(c);
    while(!fileInEn.eof())
    {
        if(isalpha(c))
        {
            fileInEn.putback(c+(keyStore%26));
            fileInEn >> temp;
            total += temp;
            fileOut << total;
        }
        else if(isdigit(c))
        {
            fileInEn.putback(c);
            fileInEn >> num;
            total += num;
            fileOut << total;
        }
        total = "";

        fileInEn.get(c);
        keyStore = rand() % 277 + 3;
        fileOutKey << keyStore << " ";
    }
}




Aucun commentaire:

Enregistrer un commentaire