Question:
C++ help encrypting a file?
2010-02-23 09:07:54 UTC
i have the following function which works, its encrypts a text file, well its suppose to shift the letters of a text file by whatever the user wants, lets say the text file says "zoo" if the user wants to shift the letters by 1 to the right the output of it should say "app"
but lets say i want to shift it 13 to the right, it wont give the correct results
instead of giving me "mbb" it gives me something random it outputs "\207bb"
anyone wants to help me fix it??


void rotateEncrypt(vector V1, vector V2, int rotKey)
{
char ch;
int k;
string word = V1[0];
for (unsigned i=0; i{
if(word[i]!=' '){
ch = tolower( word[i]);
ch = ch + rotKey;
if(ch > 'z')ch = 'a' + (ch - 'z' -1);
word[i] = ch;
}
}
V2.push_back(word);
ofstream fo("simpler.txt" ,ios::app);
fo<< word <<'\n';
fo.clear();
fo.close();
}
Three answers:
cja
2010-02-23 09:59:36 UTC
The range of values of a signed char is -128 .. +127, so your arithmetic, ch + rotKey, may overflow and give you unexpected results. I think that's your main problem. You might also reconsider the design of your rotateEncrypt function. You're passing in a vector, but only working on the first element of the vector. Also, it looks like you're assuming the file will contain only characters in the range A..Z and a..z, and converting to lowercase so that you won't be able to recover the original contents of the file. You should handle any characters in the input file, and encode in a way that you'll be able to decode to get back to where you started.



I would do it a bit differently, as you can see below. My encode function will show you the correct arithmetic.



#include

#include

#include



using namespace std;



const int MIN_PRINTABLE_ASCII = 32;

const int MAX_PRINTABLE_ASCII = 126;

const int NUM_PRINTABLE = (MAX_PRINTABLE_ASCII - MIN_PRINTABLE_ASCII + 1);

void encode(string&,int);



int main(int argc, char *argv[]) {

    string line;

    stringstream ss;

    int n;

   

    if (argc == 2) ss.str(argv[1]);

    if ((argc == 2) && (ss >> n)) {

        n %= NUM_PRINTABLE;

        do {

            getline(cin,line);

            if (line.size() > 0) {

                encode(line,n);

            }

            if (!cin.eof()) {

                cout << line << endl;

            }

        } while (!cin.eof());

    } else {

        cout << "usage: " << argv[0] << " " << endl;

        return -1;

    }

    return 0;

}



void encode(string &s, int x) {

    string::iterator p = s.begin();

    int c;



    while (p != s.end()) {

        c = static_cast(*p) + x;

        if (c > MAX_PRINTABLE_ASCII) {

            c -= NUM_PRINTABLE;

        } else if (c < MIN_PRINTABLE_ASCII) {

            c += NUM_PRINTABLE;

        }

        *p++ = c;

    }

}



#if 0



Sample run:



$ cat toyota.txt

Toyota's James Lentz repeated Toyota's position that

stuck gas pedals in some of the company's most popular

models were caused by one of two problems -- misplaced

floor mats and sticking accelerator pedals.





Encode; unfortunately, Y!A is going to truncate the 2nd line, chopping this: '4!-z|!"-}|}#yn



$ cat toyota.txt | ./encode 13

a|'|"n4!-Wnzr!-Yr{"(- r}rn"rq-a|'|"n4!-}|!v"v|{-"un"

!"#px-tn!-}rqny!-v{-!|zr-|s-"ur-p|z}n{'4!-z|!"-}|}#yn

z|qry!-%r r-pn#!rq-o'-|{r-|s-"%|-} |oyrz!-::-zv!}ynprq

sy|| -zn"!-n{q-!"vpxv{t-nppryr n"| -}rqny!;



Encode then decode to restore original:



$ cat toyota.txt | ./encode 13 | ./encode -13

Toyota's James Lentz repeated Toyota's position that

stuck gas pedals in some of the company's most popular

models were caused by one of two problems -- misplaced

floor mats and sticking accelerator pedals.



#endif
2016-12-11 11:23:35 UTC
because of the fact it form of feels that your question has already been solved via the 1st placed up, it rather is rather helpful to contemplate something in straightforward terms somewhat greater defend which contain a encryption via matrix multiplication. fairly you may convert hi to ascii code multiply via some random password in straightforward terms you already know, and now it rather is encrypted in a much less glaring way (via the way this methodology became into utilized in WWII to encrypt messages).
AQuestionMark
2010-02-23 16:42:40 UTC
>>>>>>>>>REVISION>>>>>>>>>>>

Try adding this line

else if(ch < 'a')ch=ch+1+'z'-'a';

right after this line

if(ch > 'z')ch = 'a' + (ch - 'z' -1);



And change this line

char ch;

to

unsigned char ch;


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...