1
0
This commit is contained in:
Sky Johnson 2025-09-02 22:08:49 -05:00
parent 0d651853ff
commit 2e1121584e

View File

@ -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