/
rc4.go
55 lines (49 loc) · 1.44 KB
/
rc4.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
// Copyright 2014 The goyy Authors. All rights reserved.
// Use of this source code is governed by a MIT-style
// license that can be found in the LICENSE file.
// Package rc4 implements RC4 encryption, as defined in Bruce Schneier's
// Applied Cryptography.
package rc4
import (
"crypto/rc4"
"encoding/hex"
)
// Encrypt encrypts the first block in src into dst.
// Dst and src may point at the same memory.
func Encrypt(src, key []byte) ([]byte, error) {
cipher, err := rc4.NewCipher(key)
if err != nil {
return nil, err
}
dst := make([]byte, len(src))
cipher.XORKeyStream(dst, src)
return dst, nil
}
// Decrypt decrypts the first block in src into dst.
// Dst and src may point at the same memory.
func Decrypt(src, key []byte) ([]byte, error) {
cipher, err := rc4.NewCipher(key)
if err != nil {
return nil, err
}
dst := make([]byte, len(src))
cipher.XORKeyStream(dst, src)
return dst, nil
}
// EncryptHex encrypts the first block in src into dst.
func EncryptHex(src, key string) (string, error) {
ciphertext, err := Encrypt([]byte(src), []byte(key))
if err != nil {
return "", err
}
return hex.EncodeToString(ciphertext), nil
}
// DecryptHex decrypts the first block in src into dst.
func DecryptHex(src, key string) (string, error) {
plaintext, err := hex.DecodeString(src)
if err != nil {
return "", err
}
dst, err := Decrypt(plaintext, []byte(key))
return string(dst), nil
}