-
Notifications
You must be signed in to change notification settings - Fork 4
/
des.go
154 lines (137 loc) · 3.89 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
package crypt
import (
"crypto/cipher"
"crypto/des"
"fmt"
ciphers "github.com/kayon/crypt/cipher"
)
const (
desSaltKeyByteSize = 8
tripleDesSaltKeyByteSize = 24
)
var DES cryptDES
type cryptDES struct{}
func (cryptDES) Encrypt(plaintext, key, iv []byte, args ...Options) ([]byte, error) {
c, err := NewDES(key, iv, args...)
if err != nil {
return nil, err
}
return c.Encrypt(plaintext)
}
func (cryptDES) Decrypt(ciphertext, key, iv []byte, args ...Options) ([]byte, error) {
c, err := NewDES(key, iv, args...)
if err != nil {
return nil, err
}
return c.Decrypt(ciphertext)
}
var DES3 tripleDES
type tripleDES struct{}
func (tripleDES) Encrypt(plaintext, key, iv []byte, args ...Options) ([]byte, error) {
c, err := NewDES3(key, iv, args...)
if err != nil {
return nil, err
}
return c.Encrypt(plaintext)
}
func (tripleDES) Decrypt(ciphertext, key, iv []byte, args ...Options) ([]byte, error) {
c, err := NewDES3(key, iv, args...)
if err != nil {
return nil, err
}
return c.Decrypt(ciphertext)
}
func desEncrypt(src, key, iv []byte, block cipher.Block, mode BlockMode, scheme PaddingScheme, triple bool) (ciphertext []byte, err error) {
var header [16]byte
var plaintext []byte
var offset int
if mode.Has(MODE_CBC, MODE_ECB) {
plaintext, err = Padding(scheme, src, des.BlockSize)
if err != nil {
return nil, err
}
} else {
plaintext = append([]byte{}, src...)
}
if mode.Not(MODE_ECB) && iv == nil {
saltKeyByteSize := desSaltKeyByteSize
if triple {
saltKeyByteSize = tripleDesSaltKeyByteSize
}
header, key, iv = genSaltHeader(key, block.BlockSize(), mode, saltKeyByteSize)
if triple {
if block, err = des.NewTripleDESCipher(key); err != nil {
return nil, err
}
} else if block, err = des.NewCipher(key); err != nil {
return nil, err
}
offset = 16
ciphertext = append(header[:], plaintext...)
} else {
ciphertext = append([]byte{}, plaintext...)
}
switch mode {
case MODE_CBC:
bm := cipher.NewCBCEncrypter(block, iv)
bm.CryptBlocks(ciphertext[offset:], plaintext)
case MODE_CFB:
stream := cipher.NewCFBEncrypter(block, iv)
stream.XORKeyStream(ciphertext[offset:], plaintext)
case MODE_CTR:
stream := cipher.NewCTR(block, iv)
stream.XORKeyStream(ciphertext[offset:], plaintext)
case MODE_OFB:
stream := cipher.NewOFB(block, iv)
stream.XORKeyStream(ciphertext[offset:], plaintext)
case MODE_ECB:
bm := ciphers.NewECBEncrypter(block)
bm.CryptBlocks(ciphertext[offset:], plaintext)
}
return
}
func desDecrypt(src, key, iv []byte, block cipher.Block, mode BlockMode, scheme PaddingScheme, triple bool) (plaintext []byte, err error) {
var ciphertext []byte
if salt, ok := getSalt(src); ok {
saltKeyByteSize := desSaltKeyByteSize
if triple {
saltKeyByteSize = tripleDesSaltKeyByteSize
}
key, iv = parseSaltHeader(salt, key, block.BlockSize(), mode, saltKeyByteSize)
if triple {
if block, err = des.NewTripleDESCipher(key); err != nil {
return nil, err
}
} else if block, err = des.NewCipher(key); err != nil {
return nil, err
}
ciphertext = append([]byte{}, src[16:]...)
} else {
ciphertext = append([]byte{}, src...)
}
if mode.Not(MODE_ECB) && len(iv) != block.BlockSize() {
return nil, fmt.Errorf("crypt DES.Decrypt: IV length must equal block size")
}
plaintext = make([]byte, len(ciphertext))
switch mode {
case MODE_CBC:
bm := cipher.NewCBCDecrypter(block, iv)
bm.CryptBlocks(plaintext, ciphertext)
case MODE_CFB:
stream := cipher.NewCFBDecrypter(block, iv)
stream.XORKeyStream(plaintext, ciphertext)
case MODE_CTR:
stream := cipher.NewCTR(block, iv)
stream.XORKeyStream(plaintext, ciphertext)
case MODE_OFB:
stream := cipher.NewOFB(block, iv)
stream.XORKeyStream(plaintext, ciphertext)
case MODE_ECB:
bm := ciphers.NewECBDecrypter(block)
bm.CryptBlocks(plaintext, ciphertext)
}
if mode.Has(MODE_CBC, MODE_ECB) {
plaintext, err = UnPadding(scheme, plaintext, des.BlockSize)
}
return
}