Hello, I've been wracking my brain and I cant find out where my encryption is going wrong. The file correctly inputs/outputs, but the encryption ROT/Crypto isn't working like planned. I didn't copy paste correctly, the return is in there...its that the encryption isn't working correctly. Hints are welcome, this is an assignment, and thank you.
void Security::EncFileUsingRot(int rotNum)
{
vector<string> s2; //Vector of class string of object s2
getInputFile(s2); //invokes function and passes vector s2
for(int i = 0; i < s2.size(); i++)
{
s2[i] = EncWordUsingRot(rotNum, s2[i]); //calls function and stores the word in s2 element
}
cout<<endl;
getOutputFile(s2); //invokes function to output file using vector s2
}
string Security::EncWordUsingRot(int rotNum, string word)
{
rotNum = rotNum % 26; //ensures variable is not great than alphabet
string encWord = word;
for(int i = 0; i < word.size(); i++)
{
char c = word[i];
c = tolower(c); //lowercases the letter for easier encryption
if((c < 'a')||(c > 'z'))
{
encWord[i] = c;
}
else
{
c += rotNum;
if(c > 'z')
{
c -= 26;
}
encWord[i] = c;
}
}
return encWord;
}
The char type is probably treated as signed value in the range -128 to +127. A value greater than 127 will roll over to a negative value, which messes up the comparison c > 'z'.
Try declaring c as unsigned: unsignedchar c = word[i];
So that helped a lot, thank you! I still get some garbled results with using the crypto method and decryption. This is the rest of my code and I truly appreciate the help.
Well, I wasn't quite sure what algorithm you were trying to implement, I tried an ordinary word such as "Bluegrass" as the key, but that didn't make sense. I figured it had to be a scrambled version of the alphabet.
This was my attempt. But if I'm on the wrong track, then I think you need to explain in more detail the expected input and output.
Wow, that's great. You are correct in the assumption was an alphabet encryption/decryption, apologize for not mentioning that. I lowered all the cases to ensure easier use and just copy/pasted all char outside the alphabet. It was the decryption that was killing me. I tried so many methods, but inside decWord[i] = key[index], e.g. decWord[I] -= index and so on. Thank you very much. How coincidental that I just clicked this solved as you posted your solution!