[Help]C++ bit shifting

Posts 113 of 13 · Page 1 of 1
[Help]C++ bit shifting
So I wrote a program which encrypts text using a completely different method which I now figured could be somewhat easily decrypted with a decent pc. However I think I know of a way to use bit shifting to make it near impossible. I looked everywhere for an answer but I didn't really find one.

Basically how would you make sure to take the bit that falls off and slap it onto the next byte. The best solution I can think of is this:

say you have the value
1001 1110
you want to shift it left by 2
so you put it in a loop

loop_of_some_sort
{
1001 1110 & 1100 0000 = some_var
some_var / 64
1001 1110 * 2 = some_var2
some_var2 or some_var = some_var
}

Now you should have it shifted left by 2 and you didn't loose the bits. I threw that pseudo code together while writing this post so it might be wrong.

Does anyone know of a simpler way?
Bit shifting in C++:

[php]
int var = 0xFFFFFFFF;
var << 2; //shift left
[/php]

Not sure what you're asking though.
would that turn
1111 0000
into
1100 0000
or
1100 0011

I was under the impression it just cuts the bits off that are shifted or are the placed in the next byte?
I don't know how to do precision bit shifting in C++, only assembly.

Precision shift:
[php]
mov eax,11110000b
mov ebx,11111111b
shld eax,ebx
[/php]

result: 11100001

Normal shift:
[php]
int var = 11111111b; //assuming this is binary
var << 1;
[/php]

result: 11111110

I think, did this off the top of my head.
Hmmm well then I guess I'll have to start looking into assembly or use my overly complex way to do this :/
Quote Originally Posted by hobosrock696 View Post
Hmmm well then I guess I'll have to start looking into assembly or use my overly complex way to do this :/
Lol, as long as it works I guess it's fine.
Oh if I knew if it worked that would be nice.... Will find out tomorow haha wana play with the program I have for now? Simple little thing and unfortunately makes the output 2 times the size of the input due to the method I used 0.o
Ah, I worked on a small encryption algorithm that used this..
But a word of advice, I wouldn't use any self-made 'encryption' algorithms in a real world application of any sort, unless you've been studying advanced cryptography for 20 years.

[php]data[i] = (data[i] << 3) | ((unsigned char)data[i] >> 5); // (11001010 -> 01010110)[/php]

=D
Hehe yes yes well the worst that can happen is it will never be used right?

Ahhhhh your solution is so much more... elegant....

EDIT: Hey razor you wouldnt be able to look at what I did and tell me why its way to decryptable would you? I can already take guesses at why but I would like a second opinion...
Well I'm no professional, but I'm going to guess your algorithm is easily reverse engineered.
That's enough for someone to write a decrypter, if you haven't already included a decrypter in the program for them =p
I think the most vulnerable parts of encryption would be reverse-engineer-ability and having a weak key.

If just for fun, look into public key/private key systems, 'salts', etc.
Cryptography is a fun subject I looked into at some point, but I do accept that you need a very strong background in cryptography for any algorithm to be used in a real-world application.
You can also use inline asm to rotate or shift
Code:
var 1  //binary 
var 2 //places to shift
__asm{
mov al, var1
shl al, var2 ; shifts to the left 
mov var1, al
}
But I don't think you want to shift your bytes, since they'll fall of your string (leaving 0's on the right side)

You'd want to rotate:
Code:
var 1
var 2
__asm{
mov al, var1
rol al, var2 ; Rotates to the left (bits that would fall off your string are appended to the other side, however they are appended in reverce order
mov var1, al
}
I just knew the method VOID posted

00001111;

<< 4

11110000

so <<1 would be similary to *2 and <<2 = *4 and <<X = *2^X

or is that false?
Well hrmm I know how to rotate with one byte im working on some code that will rotate any amount of bytes by any amount. Now as for my encryption method it uses a "password which can be up to the length of the content being encrypted. There is no set key length. Without the key its all guess and check. Either way I am still rewriting it to make it ever so slightly harder to guess and check. I think maybe a few times harder to guess and check...

EDIT: also the bytes are encrypted in lengths of the key. The fact that the encryption repeats over and over with a short key would make it vulnerable since you could look for patters BUT if the key is long enough its difficult.... I will also be bit shifting a LOT of bits at a time in directions based off the key. Then just scramble the bytes up and you're done. Note this is not an encryption program written with the internet in mind but rather safekeeping. Over the internet you would have to send a key which would be decrypted and therefore the file would easily be decrypted.

Off to work on the one that encrypts files....
Posts 113 of 13 · Page 1 of 1

Post a Reply

Similar Threads

Tags for this Thread

None

Need help?