From 2e1121584e4a8aee040a44328b28d1e0c52a85e1 Mon Sep 17 00:00:00 2001 From: Sky Johnson Date: Tue, 2 Sep 2025 22:08:49 -0500 Subject: [PATCH] fix rc4 --- crypto/rc4.go | 32 +++++++++++++++++--------------- 1 file changed, 17 insertions(+), 15 deletions(-) diff --git a/crypto/rc4.go b/crypto/rc4.go index ccc0a79..db5198f 100644 --- a/crypto/rc4.go +++ b/crypto/rc4.go @@ -10,34 +10,36 @@ type Ciphers struct { server *rc4.Cipher // For encryption (server->client) } -// NewCiphers creates a new pair of RC4 ciphers for the client and server +// NewCiphers returns a new pair of ciphers based on the given key func NewCiphers(key int64) (*Ciphers, error) { + // Encryption only when key > 0 + if key <= 0 { + return &Ciphers{}, nil + } + + // key (little-endian bytes) keyBytes := make([]byte, 8) binary.LittleEndian.PutUint64(keyBytes, uint64(key)) - // Client cipher uses NOT of key - clientKeyBytes := make([]byte, 8) - binary.LittleEndian.PutUint64(clientKeyBytes, uint64(^key)) + // ~key (little-endian bytes) + notKeyBytes := make([]byte, 8) + binary.LittleEndian.PutUint64(notKeyBytes, uint64(^key)) - clientCipher, err := rc4.NewCipher(keyBytes) + clientCipher, err := rc4.NewCipher(notKeyBytes) + if err != nil { + return nil, err + } + serverCipher, err := rc4.NewCipher(keyBytes) if err != nil { return nil, err } - serverCipher, err := rc4.NewCipher(clientKeyBytes) - if err != nil { - return nil, err - } - - // Drop first 20 bytes from both ciphers + // Burn first 20 bytes on both streams drop := make([]byte, 20) clientCipher.XORKeyStream(drop, drop) serverCipher.XORKeyStream(drop, drop) - return &Ciphers{ - client: clientCipher, - server: serverCipher, - }, nil + return &Ciphers{client: clientCipher, server: serverCipher}, nil } // Decrypt decrypts data received from the client