Rc4

class Rc4(key: ByteArray)

RC4 (ARC4) stream cipher. Pure-Kotlin port of the libTomCrypt-derived rc4_init / rc4_encrypt in libtorrent's src/pe_crypto.cpp, used by the Message Stream Encryption / Protocol Encryption (MSE/PE) handshake.

RC4 is symmetric: the same operation both encrypts and decrypts (it XORs the keystream into the data), so process serves both directions. The cipher is stateful, because each call to process advances the keystream, exactly like libtorrent's rc4_handler, which keeps separate incoming and outgoing Rc4 states per connection.

Note on MSE: libtorrent discards the first 1024 bytes of keystream after keying (set_incoming_key / set_outgoing_key). That discard is a property of the MSE handler, not of RC4 itself, so it lives in the protocol layer; discard is provided as a convenience for that.

Validated against the canonical RFC 6229 / Wikipedia RC4 test vectors.

Constructors

Link copied to clipboard
constructor(key: ByteArray)

Functions

Link copied to clipboard
fun discard(count: Int)

Run the keystream forward over count bytes and throw the output away. MSE keys the cipher then discards the first 1024 bytes of keystream (see rc4_handler::set_incoming_key in pe_crypto.cpp).

Link copied to clipboard

Encrypt or decrypt data, returning a new array of the same length. Advances the internal keystream position. The input array is not modified.