-
Notifications
You must be signed in to change notification settings - Fork 2
/
xcypher.go
102 lines (81 loc) · 2.25 KB
/
xcypher.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
package xcrypt
import (
"bytes"
"crypto/aes"
"crypto/cipher"
"crypto/rand"
"encoding/base64"
"errors"
"fmt"
"io"
)
func EncryptCBC(key []byte, plaintextString string) (string, error) {
plaintext := []byte(plaintextString)
padder := padder{
blockSize: aes.BlockSize,
}
plaintext, _ = padder.pad(plaintext)
if len(plaintext)%aes.BlockSize != 0 {
return "", errors.New("plaintext is not a multiple of the block size")
}
block, err := aes.NewCipher(key)
if err != nil {
return "", err
}
ciphertext := make([]byte, aes.BlockSize+len(plaintext))
iv := ciphertext[:aes.BlockSize]
if _, err := io.ReadFull(rand.Reader, iv); err != nil {
return "", err
}
mode := cipher.NewCBCEncrypter(block, iv)
mode.CryptBlocks(ciphertext[aes.BlockSize:], plaintext)
return base64.StdEncoding.EncodeToString(ciphertext), nil
}
func DecryptCBC(key []byte, encryptedText string) (string, error) {
cipherText, _ := base64.StdEncoding.DecodeString(encryptedText)
block, err := aes.NewCipher(key)
if err != nil {
return "", err
}
if len(cipherText) < aes.BlockSize {
return "", errors.New("cipherText too short")
}
iv := cipherText[:aes.BlockSize]
cipherText = cipherText[aes.BlockSize:]
if len(cipherText)%aes.BlockSize != 0 {
return "", errors.New("cipherText is not a multiple of the block size")
}
mode := cipher.NewCBCDecrypter(block, iv)
mode.CryptBlocks(cipherText, cipherText)
padder := padder{
blockSize: aes.BlockSize,
}
cipherText, _ = padder.unpad(cipherText)
return fmt.Sprintf("%s", cipherText), nil
}
func (p *padder) pad(buf []byte) ([]byte, error) {
bufLen := len(buf)
padLen := p.blockSize - (bufLen % p.blockSize)
padText := bytes.Repeat([]byte{byte(padLen)}, padLen)
return append(buf, padText...), nil
}
func (p *padder) unpad(buf []byte) ([]byte, error) {
bufLen := len(buf)
if bufLen == 0 {
return nil, errors.New("cryptgo/padding: invalid padding size")
}
pad := buf[bufLen-1]
padLen := int(pad)
if padLen > bufLen || padLen > p.blockSize {
return nil, errors.New("cryptgo/padding: invalid padding size")
}
for _, v := range buf[bufLen-padLen : bufLen-1] {
if v != pad {
return nil, errors.New("cryptgo/padding: invalid padding")
}
}
return buf[:bufLen-padLen], nil
}
type padder struct {
blockSize int
}