/*
Encode and decode a string using bitwise rotation
by a value (number of places) derived from a string key.
*/
#include <iostream>
usingnamespace std;
unsignedchar lrotate(unsignedchar val, int n);
unsignedchar rrotate(unsignedchar val, int n);
void show_binary(unsignedchar u);
int main()
{
char *str = "This is yet another test."; // strings
char *key = "Shazam";
int slen = (int) strlen(str); // string lengths
int klen = (int) strlen(key);
int rotnum; // number of places to rotate
cout << "Original string: " << str << "\n";
// encode message using funny
// number of places algorithm based on length of key
//
for(int i = 0; (*str); i++)
{
rotnum = (int) ((key[i % klen] ^ i) % slen); // limit to string length
*str = rrotate((*str), rotnum); // *** PROBLEM ***
str++;
}
cout << "\nEncoded string: " << str << "\n";
// decode message by rotating in opposite direction
//
for(int i = 0; (*str); i++)
{
rotnum = (int) ((key[i % klen] ^ i) % slen);
*str = lrotate((*str), rotnum); // *** PROBLEM ***
str++;
}
cout << "Decoded string: " << str << "\n";
return 0;
}
// left rotate a byte n places
//
unsignedchar lrotate(unsignedchar val, int n)
{
unsignedint t;
t = val;
for(int i = 0; i < n; i++)
{
t <<= 1;
/*
if a bit shifts out it'll be in bit 8
of the integer t. If this is the case
put the bit on the right side
*/
if(t & 256) t |= 1; // switch on bit 0
}
return t;
} // end lrotate()
// right rotate a byte n places
//
unsignedchar rrotate(unsignedchar val, int n)
{
unsignedint t; // integers have 32 bits
t = val;
// first move val to the next higher 8 bits
t <<= 8;
for(int i = 0; i < n; i++)
{
t >>= 1;
/*
if a bit shifts out it'll be in bit 7
of the integer t. If this is the case
put the bit on the left side
*/
if(t & 128) t |= 32768; // switch on bit 15
// 1000 0000 0000 0000
}
// put the result back
// in the lower bits of t
t >>= 8;
return t;
} // end rrotate()
// display the bits within a byte
//
void show_binary(unsignedchar u)
{
int t;
for(t = 128; t > 0; t /=2)
{
if(u & t) cout << "1";
else cout << "0";
}
cout << "\n";
}
Hi again.
I'm a bit of a newbie with pointers and can't figure why
I keep running into the PROBLEM highlighted above ***
I'm simply trying to rotate bits as some sort of rudimentary encryption.
This works without pointers. And the above code compiles without warnings.
So this pointer version keeps running into the problem as follows.
As I loop through the string using pointer arithmetic I can pass the character that's current in the loop to the rotation functions OK by dereferencing the pointer. No problems there.
As soon as I try to set the value it points at to the rotated result it bombs out. This doesn't make much sense to me, so just wanted to ask if anyone may have a solution or be so kind as to explain why the pointer fun is turning into mayhem.
You are absolutely correct. And that is one of my pet peeves about C++.
char* foo = "This is a string";
is really an invalid cast, because the string in reality is a const char*, because string literals typically get compiled into non-writable pages (ie, code pages).