-
Notifications
You must be signed in to change notification settings - Fork 23
/
cipher_rc4.go
60 lines (47 loc) · 1.08 KB
/
cipher_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
56
57
58
59
60
package ssh
import (
"crypto/rc4"
)
// CipherRC4 加密/解密
type CipherRC4 struct {
keySize int
blockSize int
identifier string
}
// 设置值大小
func (this CipherRC4) WithKeySize(keySize int) CipherRC4 {
this.keySize = keySize
return this
}
// 值大小
func (this CipherRC4) KeySize() int {
return this.keySize
}
// 块大小
func (this CipherRC4) BlockSize() int {
return this.blockSize
}
// 名称
func (this CipherRC4) Name() string {
return this.identifier
}
// 加密
func (this CipherRC4) Encrypt(key, plaintext []byte) ([]byte, error) {
cipher, err := rc4.NewCipher(key)
if err != nil {
return nil, err
}
ciphertext := make([]byte, len(plaintext))
cipher.XORKeyStream(ciphertext, plaintext)
return ciphertext, nil
}
// 解密
func (this CipherRC4) Decrypt(key, ciphertext []byte) ([]byte, error) {
cipher, err := rc4.NewCipher(key)
if err != nil {
return nil, err
}
plaintext := make([]byte, len(ciphertext))
cipher.XORKeyStream(plaintext, ciphertext)
return plaintext, nil
}