forked from go-think/openssl
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcbc.go
More file actions
executable file
·38 lines (27 loc) · 838 Bytes
/
cbc.go
File metadata and controls
executable file
·38 lines (27 loc) · 838 Bytes
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
package openssl
import (
"crypto/cipher"
"errors"
)
// CBCEncrypt
func CBCEncrypt(block cipher.Block, src, iv []byte, padding string) ([]byte, error) {
blockSize := block.BlockSize()
src = Padding(padding, src, blockSize)
encryptData := make([]byte, len(src))
if len(iv) != block.BlockSize() {
return nil, errors.New("CBCEncrypt: IV length must equal block size")
}
mode := cipher.NewCBCEncrypter(block, iv)
mode.CryptBlocks(encryptData, src)
return encryptData, nil
}
// CBCDecrypt
func CBCDecrypt(block cipher.Block, src, iv []byte, padding string) ([]byte, error) {
dst := make([]byte, len(src))
if len(iv) != block.BlockSize() {
return nil, errors.New("CBCDecrypt: IV length must equal block size")
}
mode := cipher.NewCBCDecrypter(block, iv)
mode.CryptBlocks(dst, src)
return UnPadding(padding, dst)
}