-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathDesEcbStringEncryption.go
217 lines (191 loc) · 6.83 KB
/
DesEcbStringEncryption.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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
package main
import (
"bytes"
"crypto/des"
"crypto/rand"
"encoding/base64"
"fmt"
"io"
"errors"
)
func main() {
fmt.Printf("\n# # # SECURITY WARNING: This code is provided for achieve # # #\n");
fmt.Printf("# # # compatibility between different programming languages. # # #\n");
fmt.Printf("# # # It is not necessarily fully secure. # # #\n");
fmt.Printf("# # # It uses the OUTDATED and UNSECURE algorithm DES. # # #\n");
fmt.Printf("# # # It uses FIXED key that should NEVER used, # # #\n");
fmt.Printf("# # # instead use random generated keys. # # #\n");
fmt.Printf("# # # DO NOT USE THIS CODE IN PRODUCTION # # #\n");
plaintext := string("The quick brown fox jumps over the lazy dog")
fmt.Printf("\nplaintext: " + plaintext + "\n")
fmt.Printf("\nDES ECB String encryption with fixed key\n")
desEncryptionKey := []byte("12345678");
fmt.Printf("desEncryptionKey: " + Base64Encoding(desEncryptionKey));
fmt.Printf("\n* * * Encryption * * *\n");
desCiphertextBase64, err := DesEcbEncryptToBase64(desEncryptionKey, plaintext)
if err != nil {
panic(err)
}
fmt.Printf("ciphertext: " + desCiphertextBase64 + "\n");
fmt.Printf("output is (Base64) ciphertext\n");
fmt.Printf("\n* * * Decryption * * *\n");
fmt.Printf("ciphertext: " + desCiphertextBase64 + "\n");
fmt.Printf("input is (Base64) ciphertext\n");
desDecryptedtext, err := DesEcbDecryptFromBase64(desEncryptionKey, desCiphertextBase64)
fmt.Printf("decryptedtext: " + desDecryptedtext + "\n")
fmt.Printf("\nTriple DES ECB String encryption with fixed key (16 bytes)\n");
des2EncryptionKey := []byte("1234567890123456");
fmt.Printf("des2EncryptionKey: " + Base64Encoding(des2EncryptionKey));
fmt.Printf("\n* * * Encryption * * *\n");
// convert tdes2 to tdes3 key
var tripleDESKey []byte
tripleDESKey = append(tripleDESKey, des2EncryptionKey[:16]...)
tripleDESKey = append(tripleDESKey, des2EncryptionKey[:8]...)
des2CiphertextBase64, err := Des3EcbEncryptToBase64(tripleDESKey, plaintext)
fmt.Printf("ciphertext: " + des2CiphertextBase64 + "\n");
fmt.Printf("output is (Base64) ciphertext\n");
fmt.Printf("\n* * * Decryption * * *\n");
fmt.Printf("ciphertext: " + des2CiphertextBase64 + "\n");
fmt.Printf("input is (Base64) ciphertext\n");
des2Decryptedtext, err := Des3EcbDecryptFromBase64(tripleDESKey, des2CiphertextBase64)
fmt.Printf("decryptedtext: " + des2Decryptedtext + "\n")
fmt.Printf("\nTriple DES ECB String encryption with fixed key (24 bytes)\n");
des3EncryptionKey := []byte("123456789012345678901234");
fmt.Printf("des3EncryptionKey: " + Base64Encoding(des3EncryptionKey));
fmt.Printf("\n* * * Encryption * * *\n");
des3CiphertextBase64, err := Des3EcbEncryptToBase64(des3EncryptionKey, plaintext)
fmt.Printf("ciphertext: " + des3CiphertextBase64 + "\n");
fmt.Printf("output is (Base64) ciphertext\n");
fmt.Printf("\n* * * Decryption * * *\n");
fmt.Printf("ciphertext: " + des3CiphertextBase64 + "\n");
fmt.Printf("input is (Base64) ciphertext\n");
des3Decryptedtext, err := Des3EcbDecryptFromBase64(des3EncryptionKey, des3CiphertextBase64)
fmt.Printf("decryptedtext: " + des3Decryptedtext + "\n")
}
// Golang does not support DES ECB mode in crypto, used 3rd party routines
// https://developpaper.com/aes-des-and-3des-encryption-and-decryption-in-golang-support-multiple-mode-combinations-such-as-ecb-and-cbc-and-are-compatible-with-java-php-and-other-languages/
func DesEcbEncryptToBase64(key []byte, data string) (string, error) {
plaintext := []byte(data)
plaintext = PKCS5Padding(plaintext, 8)
ciphertext, err := DesEncrypt(plaintext, key)
if err != nil {
return "", err
}
return Base64Encoding(ciphertext), err
}
func DesEcbDecryptFromBase64(key []byte, ciphertextBase64 string) (string, error) {
ciphertext := Base64Decoding(ciphertextBase64)
decryptedtext, err := DesDecrypt(ciphertext, key)
return string(decryptedtext), err
}
func Des3EcbEncryptToBase64(key []byte, data string)(string, error) {
plaintext := []byte(data)
plaintext = PKCS5Padding(plaintext, 8)
tkey := make([]byte, 24, 24)
copy(tkey, key)
k1 := tkey[:8]
k2 := tkey[8:16]
k3 := tkey[16:]
buf1, err := DesEncrypt(plaintext, k1)
if err != nil {
return "", err
}
buf2, err := DesDecrypt(buf1, k2)
if err != nil {
return "", err
}
out, err := DesEncrypt(buf2, k3)
if err != nil {
return "", err
}
return Base64Encoding(out), nil
}
func Des3EcbDecryptFromBase64(key []byte, data string) (string, error) {
tkey := make([]byte, 24, 24)
copy(tkey, key)
k1 := tkey[:8]
k2 := tkey[8:16]
k3 := tkey[16:]
ciphertext := Base64Decoding(data)
buf1, err := DesDecrypt(ciphertext, k3)
if err != nil {
return "", err
}
buf2, err := DesEncrypt(buf1, k2)
if err != nil {
return "", err
}
out, err := DesDecrypt(buf2, k1)
if err != nil {
return "", err
}
return string(out), nil
}
func DesEncrypt(origData, key []byte) ([]byte, error) {
if len(origData) < 1 || len(key) < 1 {
return nil, errors.New("wrong data or key")
}
block, err := des.NewCipher(key)
if err != nil {
return nil, err
}
bs := block.BlockSize()
if len(origData)%bs != 0 {
return nil, errors.New("wrong padding")
}
out := make([]byte, len(origData))
dst := out
for len(origData) > 0 {
block.Encrypt(dst, origData[:bs])
origData = origData[bs:]
dst = dst[bs:]
}
return out, nil
}
func DesDecrypt(crypted, key []byte) ([]byte, error) {
if len(crypted) < 1 || len(key) < 1 {
return nil, errors.New("wrong data or key")
}
block, err := des.NewCipher(key)
if err != nil {
return nil, err
}
out := make([]byte, len(crypted))
dst := out
bs := block.BlockSize()
if len(crypted)%bs != 0 {
return nil, errors.New("wrong crypted size")
}
for len(crypted) > 0 {
block.Decrypt(dst, crypted[:bs])
crypted = crypted[bs:]
dst = dst[bs:]
}
return out, nil
}
func PKCS5Padding(ciphertext []byte, blockSize int) []byte {
padding := blockSize - len(ciphertext)%blockSize
padtext := bytes.Repeat([]byte{byte(padding)}, padding)
return append(ciphertext, padtext...)
}
func PKCS5Trimming(encrypt []byte) []byte {
padding := encrypt[len(encrypt)-1]
return encrypt[:len(encrypt)-int(padding)]
}
func GenerateRandomDesKey()([]byte) {
key := make([]byte, 8) // 8 bytes for des, 16 bytes for TDES 2 and 24 bytes for TDES 3
if _, err := io.ReadFull(rand.Reader, key); err != nil {
panic(err.Error())
}
return key
}
func Base64Encoding(input []byte)(string) {
return base64.StdEncoding.EncodeToString(input)
}
func Base64Decoding(input string)([]byte) {
data, err := base64.StdEncoding.DecodeString(input)
if err != nil {
return data
}
return data
}