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;
}
Bitwise foolishness
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...
Quote:Original post by PhotonmanI 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.
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...
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.
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.
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 :)
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 :)
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement