fix rc4
This commit is contained in:
parent
0d651853ff
commit
2e1121584e
@ -10,34 +10,36 @@ type Ciphers struct {
|
|||||||
server *rc4.Cipher // For encryption (server->client)
|
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) {
|
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)
|
keyBytes := make([]byte, 8)
|
||||||
binary.LittleEndian.PutUint64(keyBytes, uint64(key))
|
binary.LittleEndian.PutUint64(keyBytes, uint64(key))
|
||||||
|
|
||||||
// Client cipher uses NOT of key
|
// ~key (little-endian bytes)
|
||||||
clientKeyBytes := make([]byte, 8)
|
notKeyBytes := make([]byte, 8)
|
||||||
binary.LittleEndian.PutUint64(clientKeyBytes, uint64(^key))
|
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 {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
serverCipher, err := rc4.NewCipher(clientKeyBytes)
|
// Burn first 20 bytes on both streams
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
|
|
||||||
// Drop first 20 bytes from both ciphers
|
|
||||||
drop := make([]byte, 20)
|
drop := make([]byte, 20)
|
||||||
clientCipher.XORKeyStream(drop, drop)
|
clientCipher.XORKeyStream(drop, drop)
|
||||||
serverCipher.XORKeyStream(drop, drop)
|
serverCipher.XORKeyStream(drop, drop)
|
||||||
|
|
||||||
return &Ciphers{
|
return &Ciphers{client: clientCipher, server: serverCipher}, nil
|
||||||
client: clientCipher,
|
|
||||||
server: serverCipher,
|
|
||||||
}, nil
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Decrypt decrypts data received from the client
|
// Decrypt decrypts data received from the client
|
||||||
|
Loading…
x
Reference in New Issue
Block a user