-
Notifications
You must be signed in to change notification settings - Fork 23
/
cfb1.go
106 lines (83 loc) · 2.32 KB
/
cfb1.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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
package cipher
import (
"crypto/cipher"
"github.com/deatil/go-cryptobin/tool/alias"
)
/**
* cfb1 模式实现, 比对 openssl 测试数据通过
*
* @create 2023-4-19
* @author deatil
*/
type cfb1 struct {
b cipher.Block
in []byte
out []byte
decrypt bool
}
func (x *cfb1) XORKeyStream(dst, src []byte) {
if len(dst) < len(src) {
panic("cipher/cfb1: output smaller than input")
}
if alias.InexactOverlap(dst[:len(src)], src) {
panic("cipher/cfb1: invalid buffer overlap")
}
for i := range src {
for j := 0; j < 8; j++ {
x.b.Encrypt(x.out, x.in)
// 获取高位
outbit := (x.out[0] >> 7) & 1
srcbit := (src[i] >> (7 - j)) & 1
dstbit := outbit ^ srcbit
var movebit byte
if x.decrypt {
movebit = srcbit
} else {
movebit = dstbit
}
x.in = leftShiftBytes(x.in, movebit)
if dstbit == 1 {
dst[i] |= (1 << (7 - j))
} else {
dst[i] &= ^(1 << (7 - j))
}
}
}
}
func leftShiftBytes(bytes []byte, carry byte) []byte {
// 如果字节数组长度为1时
if len(bytes) == 1 {
shiftedByte := (bytes[0] << 1) | carry
return []byte{shiftedByte}
}
shiftedBytes := make([]byte, len(bytes))
for i := 0; i < len(bytes)-1; i++ {
currByte := bytes[i]
nextByte := bytes[i+1]
shiftedBytes[i] = (currByte << 1) | ((nextByte >> 7) & 1)
}
// 处理最后一个字节
lastByte := (bytes[len(bytes)-1] << 1) | carry
shiftedBytes[len(bytes)-1] = lastByte
return shiftedBytes
}
func NewCFB1(block cipher.Block, iv []byte, decrypt bool) cipher.Stream {
blockSize := block.BlockSize()
if len(iv) != blockSize {
panic("cipher/cfb1: IV length must equal block size")
}
x := &cfb1{
b: block,
in: make([]byte, blockSize),
out: make([]byte, blockSize),
decrypt: decrypt,
}
copy(x.in, iv)
return x
}
func NewCFB1Encrypter(block cipher.Block, iv []byte) cipher.Stream {
return NewCFB1(block, iv, false)
}
func NewCFB1Decrypter(block cipher.Block, iv []byte) cipher.Stream {
return NewCFB1(block, iv, true)
}