-
Notifications
You must be signed in to change notification settings - Fork 0
/
des.go
172 lines (155 loc) · 5.58 KB
/
des.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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
package securityutils
import (
"crypto/cipher"
"crypto/des"
"errors"
)
// =================== ECB模式 ======================
// DES加密, 使用ECB模式,注意key必须为8位长度
func DesEncryptECB(src []byte, key []byte) ([]byte, error) {
block, err := des.NewCipher(key)
if err != nil {
return nil, err
}
blockSize := block.BlockSize()
src = PKCS5Padding(src, blockSize) // PKCS5补位
dst := make([]byte, len(src)) // 创建数组
for i, count := 0, len(src)/blockSize; i < count; i++ { // 加密
begin, end := i*blockSize, i*blockSize+blockSize
block.Encrypt(dst[begin:end], src[begin:end])
}
return dst, nil
}
// DES解密, 使用ECB模式,注意key必须为8位长度
func DesDecryptECB(src []byte, key []byte) ([]byte, error) {
block, err := des.NewCipher(key)
if err != nil {
return nil, err
}
blockSize := block.BlockSize()
dst := make([]byte, len(src)) // 创建数组
for i, count := 0, len(dst)/blockSize; i < count; i++ { // 解密
begin, end := i*blockSize, i*blockSize+blockSize
block.Decrypt(dst[begin:end], src[begin:end])
}
dst = PKCS5UnPadding(dst) // 去除PKCS5补位
return dst, nil
}
// 3DES加密, 使用ECB模式,注意key必须为24位长度
func DesEncryptECBTriple(src []byte, key []byte) ([]byte, error) {
block, err := des.NewTripleDESCipher(key)
if err != nil {
return nil, err
}
blockSize := block.BlockSize()
src = PKCS5Padding(src, blockSize) // PKCS5补位
dst := make([]byte, len(src)) // 创建数组
for i, count := 0, len(src)/blockSize; i < count; i++ { // 加密
begin, end := i*blockSize, i*blockSize+blockSize
block.Encrypt(dst[begin:end], src[begin:end])
}
return dst, nil
}
// 3DES解密, 使用ECB模式,注意key必须为24位长度
func DesDecryptECBTriple(src []byte, key []byte) ([]byte, error) {
block, err := des.NewTripleDESCipher(key)
if err != nil {
return nil, err
}
blockSize := block.BlockSize()
dst := make([]byte, len(src)) // 创建数组
for i, count := 0, len(dst)/blockSize; i < count; i++ { // 解密
begin, end := i*blockSize, i*blockSize+blockSize
block.Decrypt(dst[begin:end], src[begin:end])
}
dst = PKCS5UnPadding(dst) // 去除PKCS5补位
return dst, nil
}
// =================== CBC模式 ======================
// DES加密, 使用CBC模式,注意key必须为8位长度,iv初始化向量为非必需参数(长度为8位)
func DesEncryptCBC(src []byte, key []byte, iv ...[]byte) ([]byte, error) {
block, err := des.NewCipher(key)
if err != nil {
return nil, err
}
ivValue := ([]byte)(nil) // 获取初始化向量
if len(iv) > 0 {
ivValue = iv[0]
} else {
ivValue = key
}
src = PKCS5Padding(src, block.BlockSize()) // PKCS5补位
dst := make([]byte, len(src)) // 创建数组
blockMode := cipher.NewCBCEncrypter(block, ivValue) // 加密模式
blockMode.CryptBlocks(dst, src) // 加密
return dst, nil
}
// DES解密, 使用CBC模式,注意key必须为8位长度,iv初始化向量为非必需参数(长度为8位)
func DesDecryptCBC(src []byte, key []byte, iv ...[]byte) ([]byte, error) {
block, err := des.NewCipher(key)
if err != nil {
return nil, err
}
blockSize := block.BlockSize() // 获取秘钥块的长度
if len(src) < blockSize {
return nil, errors.New("src is too short, less than block size")
}
ivValue := ([]byte)(nil) // 获取初始化向量
if len(iv) > 0 {
ivValue = iv[0]
} else {
ivValue = key
}
if len(src)%blockSize != 0 {
return nil, errors.New("src is not a multiple of the block size")
}
dst := make([]byte, len(src)) // 创建数组
blockMode := cipher.NewCBCDecrypter(block, ivValue) // 加密模式
blockMode.CryptBlocks(dst, src) // 解密
dst = PKCS5UnPadding(dst) // 去除PKCS5补位
return dst, nil
}
// 3DES加密, 使用CBC模式,注意key必须为24位长度,iv初始化向量为非必需参数(长度为8位)
func DesEncryptCBCTriple(src []byte, key []byte, iv ...[]byte) ([]byte, error) {
block, err := des.NewTripleDESCipher(key)
if err != nil {
return nil, err
}
blockSize := block.BlockSize()
ivValue := ([]byte)(nil) // 获取初始化向量
if len(iv) > 0 {
ivValue = iv[0]
} else {
ivValue = key[:blockSize]
}
src = PKCS5Padding(src, block.BlockSize()) // PKCS5补位
dst := make([]byte, len(src)) // 创建数组
blockMode := cipher.NewCBCEncrypter(block, ivValue) // 加密模式
blockMode.CryptBlocks(dst, src) // 加密
return dst, nil
}
// 3DES解密, 使用CBC模式,注意key必须为24位长度,iv初始化向量为非必需参数(长度为8位)
func DesDecryptCBCTriple(src []byte, key []byte, iv ...[]byte) ([]byte, error) {
block, err := des.NewTripleDESCipher(key)
if err != nil {
return nil, err
}
blockSize := block.BlockSize() // 获取秘钥块的长度
if len(src) < blockSize {
return nil, errors.New("src is too short, less than block size")
}
ivValue := ([]byte)(nil) // 获取初始化向量
if len(iv) > 0 {
ivValue = iv[0]
} else {
ivValue = key[:blockSize]
}
if len(src)%blockSize != 0 {
return nil, errors.New("src is not a multiple of the block size")
}
dst := make([]byte, len(src)) // 创建数组
blockMode := cipher.NewCBCDecrypter(block, ivValue) // 加密模式
blockMode.CryptBlocks(dst, src) // 解密
dst = PKCS5UnPadding(dst) // 去除PKCS5补位
return dst, nil
}