-
Notifications
You must be signed in to change notification settings - Fork 3
/
triple_des.go
75 lines (64 loc) · 1.94 KB
/
triple_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
package cry
import (
"bytes"
"crypto/cipher"
"crypto/des"
"encoding/base64"
"github.com/pkg/errors"
)
// DESOpt contains all DES session option
type DESOpt struct {
block cipher.Block
blockSize []byte
}
// NewDESOpt is function to create new configuration of des algorithm option
// the secret must be 24 character
func NewDESOpt(secret string) (*DESOpt, error) {
if len(secret) != 24 {
return nil, errors.New("secret must be 24 char")
}
/* #nosec */
block, err := des.NewTripleDESCipher([]byte(secret))
if err != nil {
return nil, errors.Wrap(err, "NewDESOpt.des.NewTripleDESCipher")
}
blockSize := secret[:des.BlockSize]
return &DESOpt{
block: block,
blockSize: []byte(blockSize),
}, nil
}
// Encrypt is function to encrypt data using DES algorithm
func (desOpt *DESOpt) Encrypt(plainText []byte) (string, error) {
block := desOpt.block
iv := desOpt.blockSize
origData := pkcs5Padding(plainText, block.BlockSize())
mode := cipher.NewCBCEncrypter(block, iv)
encrypted := make([]byte, len(origData))
mode.CryptBlocks(encrypted, origData)
return base64.URLEncoding.EncodeToString(encrypted), nil
}
// Decrypt is function to decypt data using DES algorithm
func (desOpt *DESOpt) Decrypt(chiperText []byte) (string, error) {
block := desOpt.block
rbyte, err := base64.URLEncoding.DecodeString(string(chiperText))
if err != nil {
return "", err
}
iv := desOpt.blockSize
decrypter := cipher.NewCBCDecrypter(block, iv)
decrypted := make([]byte, len(rbyte))
decrypter.CryptBlocks(decrypted, rbyte)
decrypted = pkcs5Unpadding(decrypted)
return string(decrypted), 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 pkcs5Unpadding(origData []byte) []byte {
length := len(origData)
unpadding := int(origData[length-1])
return origData[:(length - unpadding)]
}