-
Notifications
You must be signed in to change notification settings - Fork 0
/
ecb.go
94 lines (80 loc) · 2.07 KB
/
ecb.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
package aes
import (
"crypto/aes"
"errors"
"flag"
"fmt"
"os"
"bandr.me/p/pocryp/internal/util"
"bandr.me/p/pocryp/internal/util/stdfile"
)
func EcbCmd(args ...string) error {
fset := flag.NewFlagSet("aes-ecb", flag.ContinueOnError)
fset.Usage = func() {
fmt.Fprint(os.Stderr, `Usage: pocryp aes-ecb [-bin] [-e/-d] -key|-key-file [-in INPUT] [-out OUTPUT]
Encrypt/Decrypt INPUT to OUTPUT using AES-ECB.
If -in is not specified, stdin will be read.
If -out is not specified, the output will be printed to stdout.
Options:
`)
fset.PrintDefaults()
}
fEncrypt := fset.Bool("e", false, "Encrypt the input to the output. Default if omitted.")
fDecrypt := fset.Bool("d", false, "Decrypt the input to the output.")
fOutput := fset.String("out", "", "Write the result to the file at path OUTPUT.")
fInput := fset.String("in", "", "Read data from the file at path INPUT.")
fKey := fset.String("key", "", "Key as hex.")
fKeyFile := fset.String("key-file", "", "File which contains the key as binary/text.")
fBin := fset.Bool("bin", false, "Print output in binary form not hex.")
if err := fset.Parse(args); err != nil {
return err
}
key, err := util.FileOrHex(*fKeyFile, *fKey)
if err != nil {
return fmt.Errorf("key: %w", err)
}
sf, err := stdfile.New(*fInput, *fOutput)
if err != nil {
return err
}
defer sf.Close()
input, err := sf.Read()
if err != nil {
return err
}
var output []byte
switch {
case *fEncrypt:
output, err = ecb(key, input, true)
case *fDecrypt:
output, err = ecb(key, input, false)
default:
output, err = ecb(key, input, true)
}
if err != nil {
return err
}
return sf.Write(output, *fBin)
}
func ecb(key, in []byte, direction bool) ([]byte, error) {
block, err := aes.NewCipher(key)
if err != nil {
return nil, err
}
bs := block.BlockSize()
if len(in)%bs != 0 {
return nil, errors.New("need a multiple of the blocksize")
}
result := make([]byte, len(in))
out := result
for len(in) > 0 {
if direction {
block.Encrypt(out, in)
} else {
block.Decrypt(out, in)
}
in = in[bs:]
out = out[bs:]
}
return result, nil
}