summaryrefslogtreecommitdiff
path: root/static/decrypt-aes-gcm.go
blob: bab07b5cb95d11ae088d74817cabab2baead2a5b (plain)
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
package main

import (
	"crypto/aes"
	"crypto/cipher"
	"encoding/hex"
	"errors"
	"io/ioutil"
	"os"
)

func main() {
	err := inner()
	if err != nil {
		panic(err.Error())
	}
}

func inner() error {
	if len(os.Args) < 3 {
		return errors.New("usage: ./decrypt-aes-gcm <key> <nonce> < input-file > output-file")
	}
	key, err := hex.DecodeString(os.Args[1])
	if err != nil {
		return err
	}
	nonce, err := hex.DecodeString(os.Args[2])
	if err != nil {
		return err
	}
	ciphertext, err := ioutil.ReadAll(os.Stdin)
	if err != nil {
		return err
	}
	block, err := aes.NewCipher(key)
	if err != nil {
		return err
	}
	aesgcm, err := cipher.NewGCM(block)
	if err != nil {
		return err
	}
	plaintext, err := aesgcm.Open(nil, nonce, ciphertext, nil)
	if err != nil {
		return err
	}
	_, err = os.Stdout.Write(plaintext)
	if err != nil {
		return err
	}
	return nil
}