@kjk1/

gobook cryptography encrypt decrypt

Go

No description

fork
loading
Files
  • main.go

This Plugin Crashed!

Error: Error: must not create an existing file {"type":"CREATE_FILE","wid":"0.5145522323748928","path":"main.go","file":{"path":"main.go","content":{"asEncoding":{"base64":"cGFja2FnZSBtYWluCgppbXBvcnQgKAoJImJ5dGVzIgoJImNyeXB0by9hZXMiCgkiY3J5cHRvL2NpcGhlciIKCSJjcnlwdG8vcmFuZCIKCSJlcnJvcnMiCgkiZm10IgoJImlvIgoJImlvL2lvdXRpbCIKCSJsb2ciCgoJImdvbGFuZy5vcmcveC9jcnlwdG8vc2NyeXB0IgopCgpmdW5jIGFlc0tleUZyb21QYXNzd29yZChwYXNzd29yZCBzdHJpbmcpIChbXWJ5dGUsIGVycm9yKSB7CgkvLyBETyBOT1QgdXNlIHRoaXMgc2FsdCB2YWx1ZTsgZ2VuZXJhdGUgeW91ciBvd24gcmFuZG9tIHNhbHQuIDggYnl0ZXMgaXMKCS8vIGEgZ29vZCBsZW5ndGguIEtlZXAgdGhlIHNhbHQgc2VjcmV0LgoJc2VjcmV0U2FsdCA6PSBbXWJ5dGV7MHhiYywgMHgxZSwgMHgwNywgMHhkNywgMHhiMiwgMHhhMiwgMHg1ZSwgMHgyY30KCXJldHVybiBzY3J5cHQuS2V5KFtdYnl0ZShwYXNzd29yZCksIHNlY3JldFNhbHQsIDMyNzY4LCA4LCAxLCAzMikKfQoKZnVuYyBhZXNHY21FbmNyeXB0KHVuZW5jcnlwdGVkIFtdYnl0ZSwgcGFzc3dvcmQgc3RyaW5nKSAoW11ieXRlLCBlcnJvcikgewoJa2V5LCBlcnIgOj0gYWVzS2V5RnJvbVBhc3N3b3JkKHBhc3N3b3JkKQoJaWYgZXJyICE9IG5pbCB7CgkJcmV0dXJuIG5pbCwgZXJyCgl9CglibG9jaywgZXJyIDo9IGFlcy5OZXdDaXBoZXIoa2V5KQoJaWYgZXJyICE9IG5pbCB7CgkJcmV0dXJuIG5pbCwgZXJyCgl9CgoJZ2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
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
package main

import (
	"bytes"
	"crypto/aes"
	"crypto/cipher"
	"crypto/rand"
	"errors"
	"fmt"
	"io"
	"io/ioutil"
	"log"

	"golang.org/x/crypto/scrypt"
)

func aesKeyFromPassword(password string) ([]byte, error) {
	// DO NOT use this salt value; generate your own random salt. 8 bytes is
	// a good length. Keep the salt secret.
	secretSalt := []byte{0xbc, 0x1e, 0x07, 0xd7, 0xb2, 0xa2, 0x5e, 0x2c}
	return scrypt.Key([]byte(password), secretSalt, 32768, 8, 1, 32)
}

func aesGcmEncrypt(unencrypted []byte, password string) ([]byte, error) {
	key, err := aesKeyFromPassword(password)
	if err != nil {
		return nil, err
	}
	block, err := aes.NewCipher(key)
	if err != nil {
		return nil, err
	}

	gcm, err := cipher.NewGCM(block)
	if err != nil {
		return nil, err
	}

	// generate a random nonce (makes encryption stronger)
	nonce := make([]byte, gcm.NonceSize())
	if _, err := io.ReadFull(rand.Reader, nonce); err != nil {
		return nil, err
	}

	encrypted := gcm.Seal(nil, nonce, unencrypted, nil)
	// we need nonce for decryption so we put it at the beginning
	// of encrypted text
	return append(nonce, encrypted...), nil
}

func aesGcmDecrypt(encrypted []byte, password string) ([]byte, error) {
	key, err := aesKeyFromPassword(password)
	if err != nil {
		return nil, err
	}

	block, err := aes.NewCipher(key)
	if err != nil {
		return nil, err
	}

	gcm, err := cipher.NewGCM(block)
	if err != nil {
		return nil, err
	}

	if len(encrypted) < gcm.NonceSize() {
		return nil, errors.New("Invalid data")
	}

	// extract random nonce we added to the beginning of the file
	nonce := encrypted[:gcm.NonceSize()]
	encrypted = encrypted[gcm.NonceSize():]

	return gcm.Open(nil, nonce, encrypted, nil)
}

func main() {
	password := "my password"
	d, err := ioutil.ReadFile("main.go")
	if err != nil {
		log.Fatalf("ioutil.ReadFile() failed with %s\n", err)
	}
	encrypted, err := aesGcmEncrypt(d, password)
	if err != nil {
		log.Fatalf("aesGcmEncrypt() failed with %s\n", err)
	}
	decrypted, err := aesGcmDecrypt(encrypted, password)
	if err != nil {
		log.Fatalf("aesGcmDecrypt() failed with %s\n", err)
	}
	if !bytes.Equal(d, decrypted) {
		log.Fatalf("decryption created data different than original\n")
	} else {
		fmt.Printf("Encryption in decryption worked!\n")
	}
}