-
Notifications
You must be signed in to change notification settings - Fork 0
/
des.go
78 lines (72 loc) · 1.52 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
package wallet
import (
"bytes"
"crypto/des"
"encoding/hex"
"errors"
)
func DesEncrypt(text, key []byte) (string, error) {
block, err := des.NewCipher(key)
if err != nil {
return "", err
}
bs := block.BlockSize()
text = ZeroPadding(text, bs)
if len(text)%bs != 0 {
return "", errors.New("Need a multiple of the blocksize")
}
out := make([]byte, len(text))
dst := out
for len(text) > 0 {
block.Encrypt(dst, text[:bs])
text = text[bs:]
dst = dst[bs:]
}
return hex.EncodeToString(out), nil
}
func DesDecrypt(decrypted string, key []byte) ([]byte, error) {
src, err := hex.DecodeString(decrypted)
if err != nil {
return nil, err
}
block, err := des.NewCipher(key)
if err != nil {
return nil, err
}
out := make([]byte, len(src))
dst := out
bs := block.BlockSize()
if len(src)%bs != 0 {
return nil, errors.New("crypto/cipher: input not full blocks")
}
for len(src) > 0 {
block.Decrypt(dst, src[:bs])
src = src[bs:]
dst = dst[bs:]
}
out = ZeroUnPadding(out)
return out, nil
}
func ZeroPadding(ciphertext []byte, blockSize int) []byte {
padding := blockSize - len(ciphertext)%blockSize
padtext := bytes.Repeat([]byte{0}, padding)
return append(ciphertext, padtext...)
}
func ZeroUnPadding(origData []byte) []byte {
return bytes.TrimFunc(origData,
func(r rune) bool {
return r == rune(0)
})
}
func TrimKey(key []byte) []byte {
l := len(key)
if len(key) < 8 {
for i := 0; i < 8-l; i++ {
key = append(key, byte(i))
}
return key
} else if len(key) > 8 {
return key[:8]
}
return key
}