crypting

Hello,

I've got a little problem with my program while running it,
it crashes while crypting it

this is the code from debugging :
#0 0040141C cryptPacket(packet=0x28ef08 "\001") (crypt.cpp:31)


cryptPacket :
1
2
3
4
5
6
7
8
void cryptPacket(char *packet)//line 26
{//line 27
	unsigned int paksize = *((unsigned short*)&packet[0]);//line 28
	for(unsigned int i = 2; i < (paksize-2); i++) //line 29
	{ //line 30
            packet[i] = 0x61 ^ packet[i];// line 31
	}//line 32
}//line 33 


and also the input to here :
1
2
    ADDBYTE(pak, 0x01);
    ADDWORD(pak, 0x0f00);


Thanks

EDIT :
i just note that i receive a SIGSEGV segmentation fault too
Last edited on
Can you show how it's called. The function expects the array length as the first element; i.e it's expecting an ASCIIC string.
You are using this code to encrypt packets? I wonder if you could later compress these packets with huffman compression or some other technique to send over winsock. I need to learn to encrypt but i need something that can't be broken easily by very intelligent programmers/hackers who would be trying to find out what i am sending and receiving.

very very very very intelligent programmers and hackers.
Last edited on
@New hope
Who isn't trying to make those kinds of encryptions? =P
Hmm...

I dont understand this row:

unsigned int paksize = *((unsigned short*)&packet[0]);

It`s so complex ...

Try

unsigned int paksize = (unsigned int) packet[0];

OR

unsigned int paksize = packet[0];

OR

unsigned int paksize = 0;
memmove(&paksize, &packet, 1);

And so on ...
Incorrect advise from dudev851. packet[0] returns a char, or 1 byte. Just clarifying. You probably know this and this is why you do it the "complex" (and correct) way. Well, you can simplify like this: *((unsigned short*)packet). But that's as far as you can go.

Your FOR loop is incorrect. It should be for(unsigned int i = 2; i < paksize; i++). Does that alone produces a segmentation fault? No. The problem comes from before. You are probably not allocating memory correctly.

Finally you should declare a struct instead of typecasting the first two bytes as short:

1
2
3
4
5
typedef struct
{
    unsigned short size;
    char *data;
} Packet;
Oh, sorry. Now understood, why the size is 1 byte. It`s actually 2 bytes. So you can do it with

unsigned int paksize = 0;
memmove(&paksize, &packet, 2);

Sorry for the wrong advise, I do not understood correct.

Doesnt matter ... :)

The type casting is better ...
Last edited on
Topic archived. No new replies allowed.