-
Notifications
You must be signed in to change notification settings - Fork 48
/
des.go
executable file
·57 lines (47 loc) · 1.13 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
package openssl
import (
"bytes"
"crypto/cipher"
"crypto/des"
)
// DesECBEncrypt
func DesECBEncrypt(src, key []byte, padding string) ([]byte, error) {
block, err := DesNewCipher(key)
if err != nil {
return nil, err
}
return ECBEncrypt(block, src, padding)
}
// DesECBDecrypt
func DesECBDecrypt(src, key []byte, padding string) ([]byte, error) {
block, err := DesNewCipher(key)
if err != nil {
return nil, err
}
return ECBDecrypt(block, src, padding)
}
// DesCBCEncrypt
func DesCBCEncrypt(src, key, iv []byte, padding string) ([]byte, error) {
block, err := DesNewCipher(key)
if err != nil {
return nil, err
}
return CBCEncrypt(block, src, iv, padding)
}
// DesCBCDecrypt
func DesCBCDecrypt(src, key, iv []byte, padding string) ([]byte, error) {
block, err := DesNewCipher(key)
if err != nil {
return nil, err
}
return CBCDecrypt(block, src, iv, padding)
}
// DesNewCipher creates and returns a new DES cipher.Block.
func DesNewCipher(key []byte) (cipher.Block, error) {
if len(key) < 8 {
key = append(key, bytes.Repeat([]byte{0}, 8-len(key))...)
} else if len(key) > 8 {
key = key[:8]
}
return des.NewCipher(key)
}