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.
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: *((unsignedshort*)packet). But that's as far as you can go.
Your FOR loop is incorrect. It should be for(unsignedint 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: