Im releasing a library for the encryption/decryption of shaiya network packets.
(The code might not be the best so bear with me.)
The handshake - some basic explanation how it works:
Client side:
- Connect to login server
- Receive RSA 1024 publickey
- Generate a 1024bit random number
- Encrypt the random number with rsa publickey
- Send encrypted random number
- hash the random number using sha256
- Create a hmac(hashed random number, rsa modulus) using sha256
- Use the first 16 bytes of hmac as the base AES key
- Use the second 16 bytes of hmac as the base IV
- Expand the base AES key
- Use the first extended AES key as AES key
- Use the base IV as the send and recieve counter
Server side:
- Accept connection
- Generate RSA 1024 keypair (or use pre generated)
- Send RSA 1024 publickey
- Receive encrypted random number
- Decrypt the random number with rsa privatekey
- hash the random number using sha256
- Create a hmac(hashed random number, rsa modulus) using sha256
- Use the first 16 bytes of hmac as the base AES key
- Use the second 16 bytes of hmac as the base IV
- Expand the base AES key(creates 11 keys)
- Use the first extended AES key as AES key
- Use the base IV as the send and recieve counter
After the handshake both the client and the server use AES 128 counter mode.
So basicly:
- Divide the packet into 16byte or less sized chunks. (chunks smaller than 16 bytes gets padded automatically)
- Do AES encryption/decryption with IV (send and recv packets use different IV as above)
- Increment IV
- Do the next chunk
More info:

The library is written in c++ and uses cryptopp (5.6.x should be fine) also the AES expanson method taken from

The library includes an example "login" server which:
- Accepts the first and only connection
- Initiates handshake
- Receives and the decrypts the login packet
NOTE:
The library part (crypto.cpp, crypto.hh) should compile in windows however the poject was done on linux so no guaranties. The server.cpp (the example file uses BSD sockets, so modifications is needed on windows).
LIMITATIONS:
In the handshake process 11 aes keys are generated, however the first one is observed to be used.Its very likely that the key changes after a certain number of encrypted/decrypted chunks.






