Original Post
I'm trying to make a simple encryption algorithm (why? I don't know) that uses a key for XOR encryption on a std::string. I got that algorithm working perfectly (that's now called cipher() in the new version), and decided to add a bitwise rotation on each char in the string before encryption just to throw in a curveball. So what I do is save the highest bit by copying the original value into a char and then ANDing that with 0x80 (128, HOB of char...right?), shift it down to put the high bit in the low position, shift the bits in the original character I'm working on in the string up one, and then OR that value with the char that has the HO bit in it to put it in the low position of the string's char. This, as far as I know, should be a rotation. To undo this, I copy the low order bit into a char like I did with the high order bit, do the opposite shifts, and then OR it again. Obviously, since I'm posting here, you can guess (correctly) that I don't see my original message, just ciphertext. I'm thinking maybe things are getting screwy because of the sign bit or something? Anyway, I call the following functions (something close anyway, I've been playing idly with the code, so I might have made it even worse): This would be a lot easier if C++ had a rotation operator as well as shift. I think someone propsed it for C++0x. Hope it gets in...
void encrypt(string& s, const string& k)
{
for(register int i=0;i<s.size()-1;i++){
char c=s;
c&=0x80;
s<<=1;
c>>=7;
s|=c;
}
cout<<s<<endl;
cipher(s, k);
cout<<s<<endl;
}
void decrypt(string& s, const string& k)
{
cipher(s, k);
cout<<s<<endl;
//All output OK up to this point. This decrypts properly.
for(register int i=0;i<s.size()-1;i++){
char c=s;
c&=0x01;
c<<=7;
s>>=1;
s|=c;
}
cout<<endl<<s<<endl;
}