Code: Show/Hide Uint32 encClientKey,encServerKey;
Uint8 encKeyStream[520]; bool encryptionEnabled; void InitEncryption(){ Uint32 oldSeed,tempSeed=encServerKey; Uint16 res; for(int i=0;i<520;i+=sizeof(res)){ oldSeed=tempSeed; tempSeed=((oldSeed * 0x834E0B5F) >> 48) & 0xffffffff; tempSeed=((tempSeed + (tempSeed >> 31)) & 0xffffffff); tempSeed=((((oldSeed % 0x1F31D) * 16807) - (tempSeed * 2836) + 123) & 0xffffffff); if(tempSeed > 0x7fffffff) tempSeed=((tempSeed + 0x7fffffff) & 0xffffffff); res=(Uint16)(tempSeed & 0xffff); memcpy(encKeyStream+i,&res,sizeof(res)); } encryptionEnabled=true; } void SwitchEndianness(void *ptr,int size){ for(int i=0;i<size/2;i++){ ((char*)ptr)[i]^=((char*)ptr)[size-1-i]; ((char*)ptr)[size-1-i]^=((char*)ptr)[i]; ((char*)ptr)[i]^=((char*)ptr)[size-1-i]; } } void Encrypt(char *packet,int size){ if(!encryptionEnabled) return; char *data=packet+1; int len=size-1; if(!packet[0]){ data++; len++; } Uint32 dataInt,streamInt,tempKey=encServerKey,resInt; int count=len + (sizeof(tempKey) - len%sizeof(tempKey)); for(int i=0;i<count;i+=sizeof(tempKey)){ memcpy(&dataInt,data+i,sizeof(dataInt)); SwitchEndianness(&dataInt,sizeof(dataInt)); memcpy(&streamInt,encKeyStream+i,sizeof(streamInt)); SwitchEndianness(&streamInt,sizeof(streamInt)); resInt=dataInt ^ streamInt ^ tempKey; tempKey=resInt; SwitchEndianness(&resInt,sizeof(resInt)); memcpy(data+i,&resInt,sizeof(resInt)); } } void CSSConnection::Decrypt(char *packet,int size){ if(!encryptionEnabled) return; char *data=packet+1; int len=size-1; if(!packet[0]){ data++; len++; } Uint32 dataInt,streamInt,tempKey=encServerKey,resInt; int count=len + (sizeof(tempKey) - len%sizeof(tempKey)); for(int i=0;i<count;i+=sizeof(tempKey)){ memcpy(&dataInt,data+i,sizeof(dataInt)); SwitchEndianness(&dataInt,sizeof(dataInt)); memcpy(&streamInt,encKeyStream+i,sizeof(streamInt)); SwitchEndianness(&streamInt,sizeof(streamInt)); resInt=streamInt ^ tempKey ^ dataInt; tempKey=dataInt; SwitchEndianness(&resInt,sizeof(resInt)); memcpy(data+i,&resInt,sizeof(resInt)); } } |
Code: Show/Hide Uint32 encClientKey, encServerKey;
Uint8 encKeyStream[520]; bool encryptionEnabled; void InitEncryption() { Uint32 oldSeed, tempSeed = encServerKey; Uint16 res; for (int i = 0;i < 520;i += sizeof(res)) { oldSeed = tempSeed; tempSeed = ((oldSeed * 0x834E0B5F) >> 48) & 0xffffffff; tempSeed = ((tempSeed + (tempSeed >> 31)) & 0xffffffff); tempSeed = ((((oldSeed % 0x1F31D) * 16807) - (tempSeed * 2836) + 123) & 0xffffffff); if(tempSeed > 0x7fffffff) tempSeed = ((tempSeed + 0x7fffffff) & 0xffffffff); res = (Uint16) (tempSeed & 0xffff); memcpy(encKeyStream + i, &res, sizeof(res)); } encryptionEnabled = true; } void SwitchEndianness(void *ptr, int size) { for(int i = 0;i < size / 2;i++) { ((char *) ptr)[i] ^= ((char *) ptr) [size - 1 - i]; ((char *) ptr)[size - 1 - i] ^= ((char *) ptr)[i]; ((char *) ptr)[i] ^= ((char *) ptr) [size - 1 - i]; } } void Encrypt(char *packet, int size) { if (!encryptionEnabled) return; char *data = packet + 1; int len = size - 1; if(!packet[0]) { data++; len++; } Uint32 dataInt, streamInt, tempKey = encServerKey, resInt; int count = len + (sizeof(tempKey) - len % sizeof(tempKey)); for(int i = 0;i < count;i += sizeof(tempKey)) { memcpy(&dataInt, data + i, sizeof(dataInt)); SwitchEndianness(&dataInt, sizeof(dataInt)); memcpy(&streamInt, encKeyStream + i, sizeof(streamInt)); SwitchEndianness(&streamInt, sizeof(streamInt)); resInt = dataInt ^ streamInt ^ tempKey; tempKey = resInt; SwitchEndianness(&resInt, sizeof(resInt)); memcpy(data + i, &resInt, sizeof(resInt)); } } void CSSConnection::Decrypt(char *packet, int size) { if (!encryptionEnabled) return; char *data = packet + 1; int len = size - 1; if(!packet[0]) { data++; len++; } Uint32 dataInt, streamInt, tempKey = encServerKey, resInt; int count = len + (sizeof(tempKey) - len % sizeof(tempKey)); for(int i = 0;i < count; i += sizeof(tempKey)) { memcpy(&dataInt, data+i, sizeof(dataInt)); SwitchEndianness(&dataInt, sizeof(dataInt)); memcpy(&streamInt, encKeyStream + i, sizeof(streamInt)); SwitchEndianness(&streamInt, sizeof(streamInt)); resInt = streamInt ^ tempKey ^ dataInt; tempKey = dataInt; SwitchEndianness(&resInt, sizeof(resInt)); memcpy(data + i, &resInt, sizeof(resInt)); } } |
Code: Show/Hide if(tempSeed > 0x7fffffff)
tempSeed = ((tempSeed + 0x7fffffff) & 0xffffffff); |
Cyan~Fire wrote: |
What the hay is the point of the "& 0xffffffff". Doesn't that ALWAYS give the same result as before? |
Smong wrote: |
This was originally Java, which does not have any unsigned data types. |
Quote: |
But how exactly does "& 0xffffffff" emulate unsigned data types? |
Miesco wrote: |
same thing applies to making sure it is an unsigned long or quad (64 bit integer) |
Miesco wrote: |
I hope this helps you understand why you use 0xffffffff |
Miesco wrote: |
res=(Uint16)(tempSeed & 0xffff); memcpy(encKeyStream+i,&res,sizeof(res)); encKeyStream is Uint8, how can you put a 16 bit integer in a 8 bit element? |
Miesco wrote: |
You need to put each 2 bytes from each short into 2 seperate elements |