-
Notifications
You must be signed in to change notification settings - Fork 0
/
aes.go
100 lines (87 loc) · 2.75 KB
/
aes.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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
package crypt
import (
"crypto/aes"
"crypto/cipher"
"crypto/rand"
"fmt"
)
// EncryptAesGcm encrypts and authenticates the given message with AES in GCM mode
// using the given 128, 192 or 256-bit key.
func EncryptAesGcm(key []byte, text string) (ciphertext []byte, nonce []byte, err error) {
// create a new AES cipher block
// the key argument should be the AES key, either 16, 24, or 32 bytes
// to select AES-128, AES-192, or AES-256
block, err := aes.NewCipher(key)
if err != nil {
err = fmt.Errorf("error creating cipher.Block: %v", err)
return
}
// create a GCM cipher instance
aead, err := cipher.NewGCM(block)
if err != nil {
err = fmt.Errorf("error creating AEAD: %v", err)
return
}
// generate a 96-bit random nonce
nonce = make([]byte, aead.NonceSize())
_, err = rand.Read(nonce)
if err != nil {
err = fmt.Errorf("error generating nonce: %v", err)
return
}
// data to be encrypted
data := []byte(text)
// encrypt the data
ciphertext = aead.Seal(nil, nonce, data, nil)
return
}
// DecryptAesGcm decrypts and authenticates the given message with AES in GCM mode
// using the given 128, 192 or 256-bit key and 96-bit nonce.
func DecryptAesGcm(key, nonce, ciphertext []byte) (text string, err error) {
// create a new AES cipher block
// the key argument should be the AES key, either 16, 24, or 32 bytes
// to select AES-128, AES-192, or AES-256
block, err := aes.NewCipher(key)
if err != nil {
err = fmt.Errorf("error creating cipher.Block: %v", err)
return
}
// create a GCM cipher instance
aead, err := cipher.NewGCM(block)
if err != nil {
err = fmt.Errorf("error creating AEAD: %v", err)
return
}
// decrypt the data
plaintext, err := aead.Open(nil, nonce, ciphertext, nil)
if err != nil {
err = fmt.Errorf("error decrypting data: %v", err)
return
}
text = string(plaintext)
return
}
// EncryptAesGcmWithNonceAppended encrypts and authenticates the given message with AES in GCM mode
// using the given 128, 192 or 256-bit key.
// It appends the ciphertext to the nonce.
func EncryptAesGcmWithNonceAppended(key []byte, text string) (ciphertext []byte, err error) {
ciphertext, nonce, err := EncryptAesGcm(key, text)
if err != nil {
return
}
ciphertext = append(nonce, ciphertext...)
return
}
// DecryptAesGcmWithNonceAppended decrypts and authenticates the given message with AES in GCM mode
// using the given 128, 192 or 256-bit key.
// It expects the ciphertext to have the nonce appended.
func DecryptAesGcmWithNonceAppended(key, ciphertext []byte) (text string, err error) {
nonceSize := 12
if len(ciphertext) < nonceSize {
err = fmt.Errorf("ciphertext is too short")
return
}
nonce, ciphertext := ciphertext[:nonceSize], ciphertext[nonceSize:]
text, err = DecryptAesGcm(key, nonce, ciphertext)
return
}