-
Notifications
You must be signed in to change notification settings - Fork 6
/
cipher.go
36 lines (26 loc) · 812 Bytes
/
cipher.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
package p100
import (
"crypto/aes"
"crypto/cipher"
"github.com/mergermarket/go-pkcs7"
)
type P100Cipher struct {
key []byte
iv []byte
}
func (c *P100Cipher) Encrypt(payload []byte) []byte {
block, _ := aes.NewCipher(c.key)
encrypter := cipher.NewCBCEncrypter(block, c.iv)
paddedPayload, _ := pkcs7.Pad(payload, aes.BlockSize)
encryptedPayload := make([]byte, len(paddedPayload))
encrypter.CryptBlocks(encryptedPayload, paddedPayload)
return encryptedPayload
}
func (c *P100Cipher) Decrypt(payload []byte) []byte {
block, _ := aes.NewCipher(c.key)
encrypter := cipher.NewCBCDecrypter(block, c.iv)
decryptedPayload := make([]byte, len(payload))
encrypter.CryptBlocks(decryptedPayload, payload)
unpaddedPayload, _ := pkcs7.Unpad(decryptedPayload, aes.BlockSize)
return unpaddedPayload
}