-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdes_encrypt.go
153 lines (124 loc) · 3.51 KB
/
des_encrypt.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
package criptus
import (
"crypto/cipher"
"crypto/des"
"crypto/rand"
"encoding/hex"
"errors"
"fmt"
"io"
"github.com/spf13/cast"
)
type DesEncrypt struct {
SpecialSign string // Encryption and decryption will be based on this string of characters, if not, it will be based on DesBaseSpecialSign.
Key string // Key, it is recommended to use a 5-8 digit key
Kind DesKeyType
}
func NewDesEncrypt(opts ...DESOptions) (*DesEncrypt, error) {
params, err := newDESParams(opts...)
if err != nil {
return nil, err
}
if len(params.GetKey()) == 0 {
return nil, errors.New("need the key to encrypt, please add it. ")
}
if len(params.GetSpecialSign()) == 0 {
params.SetSpecialSign(BaseSpecialSign)
}
if params.GetKeyType() == 0 {
params.SetKeyType(DesEncrypt64)
}
specialSign := formatSpecialSign(params.GetSpecialSign(),
params.GetKey(), params.GetKeyType())
return &DesEncrypt{
SpecialSign: specialSign,
Key: params.GetKey(),
Kind: params.GetKeyType(),
}, nil
}
func (d *DesEncrypt) getPrefix(length int) string {
if len(d.SpecialSign)%2 == 0 {
return d.SpecialSign[len(d.SpecialSign)-length:]
}
return d.SpecialSign[:length]
}
func (d *DesEncrypt) generateDesKey(id interface{}) []byte {
idStr := cast.ToString(id)
length := d.Kind.Length() - len(idStr) - len(d.Key)
buf := make([]byte, 0, d.Kind.Length())
prefix := d.getPrefix(length)
buf = append(buf, []byte(prefix)...)
buf = append(buf, []byte(idStr)...)
buf = append(buf, []byte(d.Key)...)
if len(buf) > 8 {
buf = buf[:d.Kind.Length()+1]
}
return buf
}
func (d *DesEncrypt) SecretEncrypt(secret interface{}, fields ...interface{}) (string, error) {
number := 0
for i := range fields {
number += fields[i].(int)
}
if secret != "" {
desKey := d.generateDesKey(number)
ans, err := d.desEncrypt(cast.ToString(secret), desKey)
if err != nil {
return "", err
}
return ans, nil
}
return "", errors.New("need the secret to encrypt")
}
func (d *DesEncrypt) SecretDecrypt(secret interface{}, fields ...interface{}) (string, error) {
number := 0
for i := range fields {
number += fields[i].(int)
}
if secret == "" {
return "", errors.New("need the secret to decrypt")
}
aesKey := d.generateDesKey(number)
b, err := d.desDecrypt(cast.ToString(secret), aesKey)
if err != nil {
return "", nil
}
return string(b), nil
}
func (d *DesEncrypt) desEncrypt(origData string, key []byte) (string, error) {
block, err := des.NewCipher(key)
if err != nil {
return "", err
}
// Generate a random IV
iv := make([]byte, block.BlockSize())
if _, err := io.ReadFull(rand.Reader, iv); err != nil {
return "", err
}
encodeByte := pkcs5Padding([]byte(origData), block.BlockSize())
blockMode := cipher.NewCBCEncrypter(block, iv)
crypted := make([]byte, len(encodeByte))
blockMode.CryptBlocks(crypted, encodeByte)
// Prepend IV to the ciphertext
cryptedWithIV := append(iv, crypted...)
hexStr := fmt.Sprintf("%x", cryptedWithIV)
return hexStr, nil
}
func (d *DesEncrypt) desDecrypt(decodeStr string, key []byte) ([]byte, error) {
decodeBytes, err := hex.DecodeString(decodeStr)
if err != nil {
return nil, err
}
// Extract IV from the beginning of the ciphertext
iv := decodeBytes[:des.BlockSize]
decodeBytes = decodeBytes[des.BlockSize:]
block, err := des.NewCipher(key)
if err != nil {
return nil, err
}
blockMode := cipher.NewCBCDecrypter(block, iv)
origData := make([]byte, len(decodeBytes))
blockMode.CryptBlocks(origData, decodeBytes)
origData = pkcs5UnPadding(origData)
return origData, nil
}