-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathencrypt.go
50 lines (41 loc) · 1.17 KB
/
encrypt.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
package main
import (
"crypto/aes"
"crypto/cipher"
"crypto/rand"
"fmt"
"io"
"io/ioutil"
)
func main() {
fmt.Println("Encryption Program")
text := []byte("My secret to be encrypt, I love hardcore and dominate sex (This is an ester egg, hmu if someone see this hehe)")
key := []byte("secretToSecretIsGoodPastaAndGood") // Must be 32 bytes
// Creates a new cipher
// func NewCipher(key []byte) (cipher.Block, error)
cblock, err := aes.NewCipher(key)
if err != nil {
panic(err)
}
// gcm = Galois Counter Mode
gcm, err := cipher.NewGCM(cblock)
if err != nil {
panic(err)
}
nonce := make([]byte, gcm.NonceSize())
if _, err = io.ReadFull(rand.Reader, nonce); err != nil {
fmt.Println(err)
}
// Seal the data
// here we encrypt our text using the Seal function
// Seal encrypts and authenticates plaintext, authenticates the
// additional data and appends the result to dst, returning the updated
// slice. The nonce must be NonceSize() bytes long and unique for all
// time, for a given key.
encrypted := gcm.Seal(nonce, nonce, text, nil)
fmt.Println(encrypted)
err = ioutil.WriteFile("encrypt.data", encrypted, 0777)
if err != nil {
fmt.Println(err)
}
}