From b8c0706a71ccae2d10065ab33715f1058fdbe6b8 Mon Sep 17 00:00:00 2001 From: Levi Durfee Date: Tue, 6 Jan 2026 19:18:18 -0500 Subject: Add ability to decrypt a file --- cmd/goaes/commands/decrypt.go | 27 +++++++++++++++++++++++++++ cmd/goaes/main.go | 14 ++++++++++++++ 2 files changed, 41 insertions(+) (limited to 'cmd/goaes') diff --git a/cmd/goaes/commands/decrypt.go b/cmd/goaes/commands/decrypt.go index f89da22..5077e8c 100644 --- a/cmd/goaes/commands/decrypt.go +++ b/cmd/goaes/commands/decrypt.go @@ -2,11 +2,38 @@ package commands import ( "context" + "encoding/gob" + "os" + "github.com/nerdsec/goaes/internal" "github.com/urfave/cli/v3" ) func Decrypt(ctx context.Context, cmd *cli.Command) error { + source := cmd.String("source") + destination := cmd.String("destination") + + file, err := os.Open(source) + if err != nil { + return err + } + defer file.Close() + + enc := gob.NewDecoder(file) + + var encryptedPayload internal.EncryptedDataPayload + + err = enc.Decode(&encryptedPayload) + if err != nil { + return err + } + + plaintext, err := internal.Decrypt(encryptedPayload.DEK, encryptedPayload.Payload) + if err != nil { + return err + } + + os.WriteFile(destination, plaintext, 0666) return nil } diff --git a/cmd/goaes/main.go b/cmd/goaes/main.go index f0b4368..2ed1fc1 100644 --- a/cmd/goaes/main.go +++ b/cmd/goaes/main.go @@ -48,6 +48,20 @@ func main() { Aliases: []string{"d"}, Usage: "Decrypt a file", Action: commands.Decrypt, + Flags: []cli.Flag{ + &cli.StringFlag{ + Name: "source", + Aliases: []string{"s"}, + Usage: "source file to decrypt", + Required: true, + }, + &cli.StringFlag{ + Name: "destination", + Aliases: []string{"d"}, + Usage: "where to write the decrypted file", + Required: true, + }, + }, }, }, } -- cgit v1.2.3