-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathdes.go
127 lines (111 loc) · 3.02 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
package main
import (
"bytes"
"crypto/cipher"
"crypto/des"
"encoding/base64"
)
type DesEncrypt struct {
PubKey string
}
type TripleDesEncrypt struct {
PubKey string
}
func (this *DesEncrypt) getKey() []byte {
strKey := this.PubKey
keyLen := len(strKey)
if keyLen < 8 {
rs := []rune(tempkey)
strKey = strKey + string(rs[0:8-keyLen])
}
arrKey := []byte(strKey)
return arrKey[:8]
}
func (this *TripleDesEncrypt) getKey() []byte {
strKey := this.PubKey
keyLen := len(strKey)
if keyLen < 24 {
rs := []rune(tempkey)
strKey = strKey + string(rs[0:24-keyLen])
}
arrKey := []byte(strKey)
return arrKey[:24]
}
func (this *DesEncrypt) Encrypt(strMesg string) ([]byte, error) {
key := this.getKey()
origData := []byte(strMesg)
block, err := des.NewCipher(key)
if err != nil {
return nil, err
}
origData = PKCS5Padding(origData, block.BlockSize())
blockMode := cipher.NewCBCEncrypter(block, key)
crypted := make([]byte, len(origData))
blockMode.CryptBlocks(crypted, origData)
return crypted, nil
}
func (this *DesEncrypt) Decrypt(crypted []byte) (decrypted []byte, err error) {
key := this.getKey()
block, err := des.NewCipher(key)
if err != nil {
return nil, err
}
crypted, err = base64.StdEncoding.DecodeString(string(crypted))
if err != nil {
return nil, err
}
blockMode := cipher.NewCBCDecrypter(block, key)
decrypted = make([]byte, len(crypted))
blockMode.CryptBlocks(decrypted, crypted)
decrypted = PKCS5UnPadding(decrypted)
return decrypted, nil
}
func (this *TripleDesEncrypt) Encrypt(strMesg string) ([]byte, error) {
key := this.getKey()
origData := []byte(strMesg)
block, err := des.NewTripleDESCipher(key)
if err != nil {
return nil, err
}
origData = PKCS5Padding(origData, block.BlockSize())
blockMode := cipher.NewCBCEncrypter(block, key[:8])
crypted := make([]byte, len(origData))
blockMode.CryptBlocks(crypted, origData)
return crypted, nil
}
func (this *TripleDesEncrypt) Decrypt(crypted []byte) ([]byte, error) {
key := this.getKey()
block, err := des.NewTripleDESCipher(key)
if err != nil {
return nil, err
}
crypted, err = base64.StdEncoding.DecodeString(string(crypted))
if err != nil {
return nil, err
}
blockMode := cipher.NewCBCDecrypter(block, key[:8])
origData := make([]byte, len(crypted))
blockMode.CryptBlocks(origData, crypted)
origData = PKCS5UnPadding(origData)
return origData, 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.TrimRightFunc(origData, func(r rune) bool {
return r == rune(0)
})
}
func PKCS5Padding(ciphertext []byte, blockSize int) []byte {
padding := blockSize - len(ciphertext)%blockSize
padtext := bytes.Repeat([]byte{byte(padding)}, padding)
return append(ciphertext, padtext...)
}
func PKCS5UnPadding(origData []byte) []byte {
length := len(origData)
unpadding := int(origData[length-1])
return origData[:(length - unpadding)]
}