Bitwise foolishness

Started by
3 comments, last by Photonman 17 years, 9 months ago
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):

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;
}

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...
______________________________Stranger things have happened...The Following Sentence is True. The Above Sentence is False.
Advertisement
If you think the sign bit is causing problems, have you tried using an unsigned char?
Quote:Original post by Photonman
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...
I remember using Pascal on a mac where there was a BROTL "function" for lack of a better word, which compiled down to a bit rotate asm instruction. C & C++ currently have nothing like it, which is a shame. They became useful for CRC calculations, or hashing or something like that.

These lines are the problem:
		char c=s;		c&=0x80;		s<<=1;		c>>=7;
you need to use an unsigned char. Also, the &=0x80 is redundant as the c>>=7 will leave it with clear upper 7 bits.
"In order to understand recursion, you must first understand recursion."
My website dedicated to sorting algorithms
unsigned char c=s;//encrypt...s=c>>7|c<<1; //...or decrypts=c<<7|c>>1; 

Concise but easy to get wrong (e.g. s=c<<7|c<<1;).

P.S. from a cryptographic point of view rotation of character codes isn't much of a curve ball: applying a bijective function is equivalent to writing the plaintext in a funny character encoding.

Omae Wa Mou Shindeiru

Well, that worked. Thanks all.
C++ does have _rotl() and _rotr(), but they take int types (can't remember, but I think they take long) I guess I thought that using and unsigned char wouldn't matter once it got put in the string which uses plain old char, but I can see how that was a little shortsighted now that I think about it.

I know that just rotation isn't much, but I rotate the values and then XOR them with a key. I'm just trying to play with very simple things to throw into the function, and if you have any ideas, they're welcome :)
______________________________Stranger things have happened...The Following Sentence is True. The Above Sentence is False.

This topic is closed to new replies.

Advertisement