-
Notifications
You must be signed in to change notification settings - Fork 109
/
encryption_cbc.go
81 lines (65 loc) · 1.73 KB
/
encryption_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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
package main
import (
"crypto/aes"
"crypto/cipher"
"encoding/hex"
"errors"
)
// aescbc implements plain AES-CBC encryption-decryption
type aescbc struct {
c cipher.Block
}
func newAesCbc(key string) (PacketEncrypter, error) {
if "" == key {
return nil, errors.New("key is empty")
}
bkey, err := hex.DecodeString(key)
if nil != err {
return nil, errors.New("not valid hex string")
}
if (len(bkey) != 16) && (len(bkey) != 24) && (len(bkey) != 32) {
return nil, errors.New(`Length of key must be 16, 24 or 32 bytes
(32, 48 or 64 hex symbols)
to select AES-128, AES-192 or AES-256`)
}
a := aescbc{}
a.c, err = aes.NewCipher(bkey)
if nil != err {
return nil, err
}
return &a, nil
}
func (a *aescbc) CheckSize(size int) bool {
return size > aes.BlockSize && size%aes.BlockSize == 0
}
func (a *aescbc) AdjustInputSize(size int) int {
if size%aes.BlockSize != 0 {
return size + (aes.BlockSize - (size % aes.BlockSize))
}
return size
}
func (a *aescbc) Encrypt(input []byte, output []byte, iv []byte) int {
copy(output[:aes.BlockSize], iv)
cipher.NewCBCEncrypter(a.c, iv).CryptBlocks(output[aes.BlockSize:], input)
inputLen := len(input)
// whole len of output is len(input) + aes.BlockSize,
// so copy of last aes.BlockSize
copy(iv, output[inputLen:])
return inputLen + aes.BlockSize
}
func (a *aescbc) Decrypt(input []byte, output []byte) (int, error) {
resultLen := len(input) - aes.BlockSize
cipher.NewCBCDecrypter(a.c, input[:aes.BlockSize]).
CryptBlocks(output, input[aes.BlockSize:])
return resultLen, nil
}
func (a *aescbc) OutputAdd() int {
// adding IV to each message
return aes.BlockSize
}
func (a *aescbc) IVLen() int {
return aes.BlockSize
}
func init() {
registeredEncrypters["aescbc"] = newAesCbc
}