Skip to content

API reference / @jsprismarine/protocol / EncryptionCodec

Class: EncryptionCodec ​

Defined in: protocol/src/EncryptionCodec.ts:47

The encryption layer, which sits between a batch and the transport.

A batch arrives here already framed and compressed: 0xFE, the algorithm byte, then the compressed packets. Everything after the 0xFE is encrypted, including the algorithm byte. The header stays in the clear because RakNet has to keep recognizing the frame, and the checksum covers what follows it rather than the whole thing.

On the wire an encrypted batch is 0xFE || AES(body || checksum), where the checksum is the first eight bytes of SHA-256(counter as little-endian uint64 || body || key) and the counter is how many batches this direction has sent before this one.

Two things make this unforgiving. The stream is continuous: one keystream runs across every batch in a direction, so a batch that is dropped, reordered or encrypted twice desynchronizes everything after it and there is no resynchronizing. And each direction keeps its own counter, which is hashed into the checksum but never sent, so the only way a peer learns the counters disagree is that every checksum from then on is wrong.

Both of those fail the same way a wrong key does: silently, with the connection simply stopping. Hence decrypt refusing a bad checksum loudly rather than passing on a batch that decrypted to something.

Constructors ​

Constructor ​

new EncryptionCodec(key): EncryptionCodec

Defined in: protocol/src/EncryptionCodec.ts:57

Parameters ​

key ​

Buffer

the 32 bytes both ends derived from the shared secret and the salt.

Returns ​

EncryptionCodec

Methods ​

decrypt() ​

decrypt(batch): Buffer

Defined in: protocol/src/EncryptionCodec.ts:82

The reverse, with the checksum verified and removed.

Parameters ​

batch ​

Buffer

Returns ​

Buffer

Throws ​

when the checksum disagrees, which means the key, the order or the counters do.


encrypt() ​

encrypt(batch): Buffer

Defined in: protocol/src/EncryptionCodec.ts:70

A framed batch, encrypted and carrying its checksum.

Parameters ​

batch ​

Buffer

Returns ​

Buffer