-
Notifications
You must be signed in to change notification settings - Fork 0
/
cbc.go
64 lines (60 loc) · 1.46 KB
/
cbc.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
package aes
import (
stdaes "crypto/aes"
"crypto/cipher"
"github.com/colduction/aes/padding"
)
// Encrypts input using AES in CBC mode
func (cbc) Encrypt(input, key, iv []byte, pad padding.Padding) ([]byte, error) {
lenInput := len(input)
if lenInput == 0 {
return nil, InvalidDataError(lenInput)
}
block, err := stdaes.NewCipher(key)
if err != nil {
return nil, err
}
if err = IvSizeEquality(len(iv), block.BlockSize()); err != nil {
return nil, err
}
if pad != nil {
if input, err = pad.Pad(input, block.BlockSize()); err != nil {
return nil, err
}
lenInput = len(input)
}
if lenInput%block.BlockSize() != 0 {
return nil, InvalidDataError(lenInput)
}
ct := make([]byte, lenInput)
mode := cipher.NewCBCEncrypter(block, iv)
mode.CryptBlocks(ct, input)
return ct, nil
}
// Decrypts ciphertext using AES in CBC mode
func (cbc) Decrypt(ciphertext, key, iv []byte, pad padding.Padding) ([]byte, error) {
lenCt := len(ciphertext)
if lenCt == 0 {
return nil, InvalidCiphertextError(lenCt)
}
block, err := stdaes.NewCipher(key)
if err != nil {
return nil, err
}
if lenCt%block.BlockSize() != 0 {
return nil, InvalidDataError(lenCt)
}
if err = IvSizeEquality(len(iv), block.BlockSize()); err != nil {
return nil, err
}
mode := cipher.NewCBCDecrypter(block, iv)
pt := make([]byte, lenCt)
mode.CryptBlocks(pt, ciphertext)
if pad != nil {
pt, err = pad.Unpad(pt, block.BlockSize())
if err != nil {
return nil, err
}
}
return pt, nil
}