-
Notifications
You must be signed in to change notification settings - Fork 2
/
decrypt.go
62 lines (59 loc) · 1.11 KB
/
decrypt.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
package main
import (
"github.com/codegangsta/cli"
"crypto/aes"
"crypto/cipher"
"io"
)
// Decrypt a file with AES.
var decryptCommand = cli.Command{
Name: "decrypt",
Usage: "decrypt a file",
Flags: []cli.Flag{
cli.StringFlag{
Name: "input, i",
Usage: "encrypted file (default: STDIN)",
},
cli.StringFlag{
Name: "output, o",
Usage: "plaintext file (default: STDOUT)",
},
cli.StringFlag{
Name: "key, k",
Usage: "pre-shared key file",
},
},
Action: func(c *cli.Context) {
i, err := openInput(c.String("input"))
if err != nil {
abortWithError(err)
}
defer i.Close()
o, err := openOutput(c.String("output"))
if err != nil {
abortWithError(err)
}
defer o.Close()
k, err := openKey(c.String("key"))
if err != nil {
abortWithError(err)
}
b, err := aes.NewCipher(k)
if err != nil {
abortWithError(err)
}
iv := make([]byte, aes.BlockSize)
_, err = i.Read(iv)
if err != nil {
abortWithError(err)
}
r := &cipher.StreamReader{
S: cipher.NewCFBDecrypter(b, iv),
R: i,
}
_, err = io.Copy(o, r)
if err != nil {
abortWithError(err)
}
},
}