-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathaes.go
84 lines (70 loc) · 1.93 KB
/
aes.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
package crypto
import (
"bytes"
"crypto/aes"
"crypto/cipher"
"crypto/rand"
"errors"
"io"
)
// ErrMalformedPadding is returned when padding cannot be stripped during
// decryption.
var ErrMalformedPadding = errors.New("malformed padding")
// ErrMalformedCipherText is returned when a cipher text is not a multiple of
// the block size.
var ErrMalformedCipherText = errors.New("malformed cipher text")
type AESCipher struct {
secret []byte
}
func RandomAESCipher() (AESCipher, error) {
secret := [16]byte{}
if _, err := io.ReadFull(rand.Reader, secret[:]); err != nil {
return AESCipher{}, err
}
return AESCipher{secret: secret[:]}, nil
}
func NewAESCipher(secret []byte) AESCipher {
return AESCipher{secret: secret}
}
func (key *AESCipher) Encrypt(plainText []byte) ([]byte, error) {
block, err := aes.NewCipher(key.secret)
if err != nil {
return nil, err
}
paddedPlainText := pad(plainText)
cipherText := make([]byte, aes.BlockSize+len(paddedPlainText))
iv := cipherText[:aes.BlockSize]
if _, err := io.ReadFull(rand.Reader, iv); err != nil {
return nil, err
}
cfb := cipher.NewCFBEncrypter(block, iv)
cfb.XORKeyStream(cipherText[aes.BlockSize:], []byte(paddedPlainText))
return cipherText, nil
}
func (key *AESCipher) Decrypt(cipherText []byte) ([]byte, error) {
block, err := aes.NewCipher(key.secret)
if err != nil {
return nil, err
}
if (len(cipherText) % aes.BlockSize) != 0 {
return nil, ErrMalformedCipherText
}
iv := cipherText[:aes.BlockSize]
message := cipherText[aes.BlockSize:]
cfb := cipher.NewCFBDecrypter(block, iv)
cfb.XORKeyStream(message, message)
return strip(message)
}
func pad(src []byte) []byte {
p := aes.BlockSize - len(src)%aes.BlockSize
padding := bytes.Repeat([]byte{byte(p)}, p)
return append(src, padding...)
}
func strip(src []byte) ([]byte, error) {
length := len(src)
p := int(src[length-1])
if p > length {
return nil, ErrMalformedPadding
}
return src[:(length - p)], nil
}