-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathAesGcm256StringEncryption.go
112 lines (99 loc) · 3.62 KB
/
AesGcm256StringEncryption.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
101
102
103
104
105
106
107
108
109
110
111
112
package main
import (
"strings"
"crypto/aes"
"crypto/cipher"
"crypto/rand"
"encoding/base64"
"fmt"
"io"
)
func main() {
fmt.Printf("AES GCM 256 String encryption with random key\n")
plaintext := string("The quick brown fox jumps over the lazy dog")
fmt.Printf("plaintext: " + plaintext + "\n")
// generate random key
encryptionKey := []byte(GenerateRandomAesKey())
encryptionKeyBase64 := string(Base64Encoding(encryptionKey))
fmt.Printf("encryptionKey (Base64): " + encryptionKeyBase64 + "\n")
fmt.Printf("\n* * * Encryption * * *\n");
ciphertextBase64 := string(AesGcmEncryptToBase64(encryptionKey, plaintext))
fmt.Printf("ciphertext: " + ciphertextBase64 + "\n");
fmt.Printf("output is (Base64) nonce : (Base64) ciphertext : (Base64) gcmTag\n");
fmt.Printf("\n* * * Decryption * * *\n");
decryptionKeyBase64 := string(encryptionKeyBase64);
ciphertextDecryptionBase64 := string(ciphertextBase64);
fmt.Printf("decryptionkey (Base64): " + decryptionKeyBase64 + "\n")
decryptionKey := []byte(Base64Decoding(decryptionKeyBase64))
fmt.Printf("ciphertext: " + ciphertextDecryptionBase64 + "\n");
fmt.Printf("input is (Base64) nonce : (Base64) ciphertext : (Base64) gcmTag\n");
decryptedtext := string(AesGcmDecryptFromBase64(decryptionKey, ciphertextDecryptionBase64))
fmt.Printf("decryptedtext: " + decryptedtext)
}
func AesGcmEncryptToBase64(key []byte, data string)(string) {
plaintext := []byte(data)
nonce := []byte(GenerateRandomNonce())
block, err := aes.NewCipher(key)
if err != nil {
panic(err.Error())
}
aesGCM, err := cipher.NewGCM(block)
if err != nil {
panic(err.Error())
}
ciphertext := aesGCM.Seal(nil, nonce, plaintext, nil)
// ciphertext hold the ciphertext | gcmTag
ciphertextWithTagLength := int(len(ciphertext))
ciphertextWithoutTag, gcmTag := ciphertext[:(ciphertextWithTagLength-16)], ciphertext[(ciphertextWithTagLength-16):]
ciphertextWithoutTagBase64 := string(Base64Encoding(ciphertextWithoutTag))
nonceBase64 := string(Base64Encoding(nonce))
gcmTagBase64 := string(Base64Encoding(gcmTag))
ciphertextCompleteBase64 := string(nonceBase64 + ":" + ciphertextWithoutTagBase64 + ":" + gcmTagBase64)
return ciphertextCompleteBase64
}
func AesGcmDecryptFromBase64(encryptionKey []byte, ciphertextCompleteBase64 string)(string) {
data := strings.Split(ciphertextCompleteBase64, ":")
nonce := []byte(Base64Decoding(data[0]))
ciphertext := []byte(Base64Decoding(data[1]))
gcmTag := []byte(Base64Decoding(data[2]))
ciphertextCompleteLength := int(len(ciphertext) + len(gcmTag))
ciphertextComplete := make([]byte, ciphertextCompleteLength)
ciphertextComplete = append(ciphertext[:], gcmTag[:]...)
block, err := aes.NewCipher(encryptionKey)
if err != nil {
panic(err.Error())
}
aesGCM, err := cipher.NewGCM(block)
if err != nil {
panic(err.Error())
}
plaintext, err := aesGCM.Open(nil, nonce, ciphertextComplete, nil)
if err != nil {
panic(err.Error())
}
return string(plaintext)
}
func GenerateRandomAesKey()([]byte) {
key := make([]byte, 32)
if _, err := io.ReadFull(rand.Reader, key); err != nil {
panic(err.Error())
}
return key
}
func GenerateRandomNonce()([]byte) {
nonce := make([]byte, 12)
if _, err := io.ReadFull(rand.Reader, nonce); err != nil {
panic(err.Error())
}
return nonce
}
func Base64Encoding(input []byte)(string) {
return base64.StdEncoding.EncodeToString(input)
}
func Base64Decoding(input string)([]byte) {
data, err := base64.StdEncoding.DecodeString(input)
if err != nil {
return data
}
return data
}