-
Notifications
You must be signed in to change notification settings - Fork 0
/
aesctr.go
43 lines (34 loc) · 1.02 KB
/
aesctr.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
// Package aesctr provides utility for encrypting and decrypting AES with CTR
// mode.
package aesctr
import (
"crypto/aes"
"crypto/cipher"
"crypto/rand"
)
// Encrypt turns plaintext into AES-encrypted ciphertext with CTR mode. Key
// can be 16, 32 or 64 bytes long.
func Encrypt(key []byte, plaintext []byte) ([]byte, error) {
block, err := aes.NewCipher(key)
if err != nil {
return nil, err
}
ciphertext := make([]byte, aes.BlockSize+len(plaintext))
iv := ciphertext[:aes.BlockSize]
rand.Read(iv)
stream := cipher.NewCTR(block, iv)
stream.XORKeyStream(ciphertext[aes.BlockSize:], plaintext)
return ciphertext, nil
}
// Decrypt turns ciphertext using Encrypt into plaintext.
func Decrypt(key []byte, ciphertext []byte) ([]byte, error) {
block, err := aes.NewCipher(key)
if err != nil {
return nil, err
}
iv := ciphertext[:aes.BlockSize]
plaintext := make([]byte, len(ciphertext)-aes.BlockSize)
stream := cipher.NewCTR(block, iv)
stream.XORKeyStream(plaintext, ciphertext[aes.BlockSize:])
return plaintext, nil
}