forked from rkbalgi/libiso
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcrypto_des.go
196 lines (145 loc) · 4.2 KB
/
crypto_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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
package crypto
import (
"crypto/cipher"
"crypto/des"
"encoding/hex"
"log"
)
type DesMode int
const (
Ecb DesMode = iota + 1
Cbc
)
var ZeroIv []byte = []byte{0, 0, 0, 0, 0, 0, 0, 0}
//Performs TripleDes encryption of data with the key
func EncryptTripleDesEde2(key []byte, data []byte, paddingType PaddingType) ([]byte, error) {
var keyData []byte
keyData = append(keyData, key...)
keyData = append(keyData, key[:8]...)
//add padding
newData := paddingType.Pad(data)
log.Printf("(+Pad) %s ", hex.EncodeToString(newData))
block, err := des.NewTripleDESCipher(keyData)
if err != nil {
log.Printf("failed to encrypt - %s", err.Error())
return nil, err
}
encryptedData := make([]byte, len(newData))
for i := 0; i < len(data); i += 8 {
block.Encrypt(encryptedData[i:i+8], newData[i:i+8])
}
return encryptedData, err
}
//performs Triple DES decryption
func DecryptTripleDesEde2(key []byte, data []byte, paddingType PaddingType) ([]byte, error) {
var keyData []byte
keyData = append(keyData, key...)
keyData = append(keyData, key[:8]...)
block, err := des.NewTripleDESCipher(keyData)
if err != nil {
log.Printf("failed to encrypt - %s", err.Error())
return nil, err
}
decryptedData := make([]byte, len(data))
for i := 0; i < len(data); i += 8 {
block.Decrypt(decryptedData[i:i+8], data[i:i+8])
}
log.Printf("Decrypted Data %s\n", hex.EncodeToString(decryptedData))
decryptedData = paddingType.RemovePad(decryptedData)
return decryptedData, err
}
//exported wrappers to des and triple des routines
func EncryptDes(data []byte, key []byte) ([]byte, error) {
return encryptDes(data, key)
}
func DecryptDes(data []byte, key []byte) ([]byte, error) {
return decryptDes(data, key)
}
func EncryptTripleDes(data []byte, key []byte) ([]byte, error) {
return encrypt3des(data, key)
}
func DecryptTripleDes(data []byte, key []byte) ([]byte, error) {
return decrypt3des(data, key)
}
//single des
func decryptDes(data []byte, key []byte) ([]byte, error) {
desCipher, err := des.NewCipher(key)
if err != nil {
return nil, err
}
result := make([]byte, len(data))
//log.Printf("%s %s", hex.EncodeToString(result), hex.EncodeToString(data))
for i := 0; i < len(data); i += 8 {
desCipher.Decrypt(result[i:], data[i:])
}
return result, nil
}
func encryptDes(data []byte, key []byte) ([]byte, error) {
desCipher, err := des.NewCipher(key)
if err != nil {
return nil, err
}
result := make([]byte, len(data))
//log.Printf("%s %s", hex.EncodeToString(result), hex.EncodeToString(data))
for i := 0; i < len(data); i += 8 {
desCipher.Encrypt(result[i:], data[i:])
}
return result, nil
}
func EncryptDesCbc(data []byte, key []byte) ([]byte, error) {
return encryptDesCbc(data, key)
}
func encryptDesCbc(data []byte, key []byte) ([]byte, error) {
desCipher, err := des.NewCipher(key)
if err != nil {
return nil, err
}
encrypter := cipher.NewCBCEncrypter(desCipher, ZeroIv)
result := make([]byte, len(data))
encrypter.CryptBlocks(result, data)
return result, nil
}
//triple des
func decrypt3des(data []byte, key []byte) ([]byte, error) {
tripleDESKey := make([]byte, 0)
if len(key) == 16 {
//ede2
tripleDESKey = append(tripleDESKey, key...)
tripleDESKey = append(tripleDESKey, key[:8]...)
} else {
//ede3
tripleDESKey = append(tripleDESKey, key...)
}
tripleDESCipher, err := des.NewTripleDESCipher(tripleDESKey)
if err != nil {
return nil, err
}
result := make([]byte, len(data))
//log.Printf("%s %s", hex.EncodeToString(result), hex.EncodeToString(data))
for i := 0; i < len(data); i += 8 {
tripleDESCipher.Decrypt(result[i:], data[i:])
}
return result, err
}
func encrypt3des(data []byte, key []byte) ([]byte, error) {
tripleDESKey := make([]byte, 0)
if len(key) == 16 {
//ede2
tripleDESKey = append(tripleDESKey, key...)
tripleDESKey = append(tripleDESKey, key[:8]...)
} else {
//ede3
tripleDESKey = append(tripleDESKey, key...)
}
log.Println(hex.EncodeToString(tripleDESKey))
tripleDESCipher, err := des.NewTripleDESCipher(tripleDESKey)
if err != nil {
return nil, err
}
result := make([]byte, len(data))
for i := 0; i < len(data); i += 8 {
tripleDESCipher.Encrypt(result[i:], data[i:])
}
//tripleDESCipher.Encrypt(result, data)
return result, nil
}