Skip to main content

PacketCipher

abstract class

Represents an AES/CFB8 stream cipher that transforms protocol bytes in place.

Declaration​

public abstract class PacketCipher
NamespaceMcProtoNet.Transport.Cryptography
AssemblyMcProtoNet.Transport.dll
Sourcesrc/McProtoNet.Transport/Cryptography/PacketCipher.cs#L11 ↗

Remarks​

A cipher carries the feedback register of the stream it serves, so one instance belongs to one direction of one connection and must be used by one thread at a time. The concrete implementation is chosen at creation time: an x86 AES-NI core, an ARM64 NEON core, or a scalar fallback.

Fields​

SignatureDescription
const int SharedSecretLength = 16The required length of the shared secret, in bytes.

SharedSecretLength​

public const int SharedSecretLength = 16

The required length of the shared secret, in bytes.

Returns: int

Methods​

SignatureDescription
static PacketCipher CreateDecryptor(ReadOnlySpan<byte> sharedSecret)Creates a cipher that decrypts incoming bytes with the specified shared secret.
static PacketCipher CreateEncryptor(ReadOnlySpan<byte> sharedSecret)Creates a cipher that encrypts outgoing bytes with the specified shared secret.
void Dispose()Releases all resources used by the current instance of the PacketCipher class.
void Dispose(bool disposing)Releases the unmanaged resources used by the PacketCipher and optionally releases the managed resources.
void Transform(Span<byte> buffer)When overridden in a derived class, transforms the specified buffer in place and advances the cipher state by its length.

CreateDecryptor(ReadOnlySpan<byte>)​

public static PacketCipher CreateDecryptor(ReadOnlySpan<byte> sharedSecret)

Creates a cipher that decrypts incoming bytes with the specified shared secret.

Parameters: sharedSecret (ReadOnlySpan<byte>)

Returns: PacketCipher - A new PacketCipher that decrypts.

Exceptions: ArgumentException - sharedSecret is not PacketCipher.SharedSecretLength bytes long.

CreateEncryptor(ReadOnlySpan<byte>)​

public static PacketCipher CreateEncryptor(ReadOnlySpan<byte> sharedSecret)

Creates a cipher that encrypts outgoing bytes with the specified shared secret.

Parameters: sharedSecret (ReadOnlySpan<byte>)

Returns: PacketCipher - A new PacketCipher that encrypts.

Exceptions: ArgumentException - sharedSecret is not PacketCipher.SharedSecretLength bytes long.

Dispose()​

public void Dispose()

Releases all resources used by the current instance of the PacketCipher class.

Dispose(bool)​

protected virtual void Dispose(bool disposing)

Releases the unmanaged resources used by the PacketCipher and optionally releases the managed resources.

A derived class clears its key material here.

Parameters: disposing (bool)

Transform(Span<byte>)​

public abstract void Transform(Span<byte> buffer)

When overridden in a derived class, transforms the specified buffer in place and advances the cipher state by its length.

The bytes must be passed in wire order, without gaps, because each byte feeds the register that transforms the next one.

Parameters: buffer (Span<byte>)