Skip to content

Commit

Permalink
Add main script + example files
Browse files Browse the repository at this point in the history
  • Loading branch information
theognis1002 committed Aug 1, 2024
1 parent 14f6eec commit 8166a58
Show file tree
Hide file tree
Showing 6 changed files with 127 additions and 0 deletions.
Binary file added .DS_Store
Binary file not shown.
31 changes: 31 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@

### Makefile

```Makefile
# Makefile for the Go File Encryption and Decryption project

# The binary name
BINARY_NAME = file-crypto

# Build the Go binary
build:
@echo "Building the Go binary..."
go build -o $(BINARY_NAME) main.go

# Run the Go application with encryption
encrypt:
@echo "Encrypting the file..."
./$(BINARY_NAME) encrypt example.csv encrypted.csv

# Run the Go application with decryption
decrypt:
@echo "Decrypting the file..."
./$(BINARY_NAME) decrypt encrypted.csv decrypted.csv

# Clean up build files
clean:
@echo "Cleaning up..."
rm -f $(BINARY_NAME)

# Default target: build the binary
all: build
1 change: 1 addition & 0 deletions example.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
stark,lannister,tyrell,targaryen,baratheon
1 change: 1 addition & 0 deletions example.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
ned stark
3 changes: 3 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module go-encrypt

go 1.22.5
91 changes: 91 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
package main

import (
"crypto/aes"
"crypto/cipher"
"crypto/rand"
"fmt"
"io"
"os"
)

func main() {
key := []byte("example key 1234") // 16 bytes key for AES-128
inputFile := "example.csv"
encryptedFile := "encrypted.csv"
decryptedFile := "decrypted.csv"

// Encrypt the file
err := encryptFile(inputFile, encryptedFile, key)
if err != nil {
fmt.Println("Error encrypting file:", err)
return
}
fmt.Println("File encrypted successfully.")

// Decrypt the file
err = decryptFile(encryptedFile, decryptedFile, key)
if err != nil {
fmt.Println("Error decrypting file:", err)
return
}
fmt.Println("File decrypted successfully.")
}

// encryptFile encrypts the file at inputPath and writes the encrypted data to outputPath
func encryptFile(inputPath, outputPath string, key []byte) error {
data, err := os.ReadFile(inputPath)
if err != nil {
return err
}

// Generate a new AES cipher using our 16, 24 or 32 bytes long key
block, err := aes.NewCipher(key)
if err != nil {
return err
}

gcm, err := cipher.NewGCM(block)
if err != nil {
return err
}

nonce := make([]byte, gcm.NonceSize())
if _, err = io.ReadFull(rand.Reader, nonce); err != nil {
return err
}

// Encrypt the data using Seal (which also appends the nonce and the encrypted data together)
ciphertext := gcm.Seal(nonce, nonce, data, nil)

return os.WriteFile(outputPath, ciphertext, 0644)
}

// decryptFile decrypts the file at inputPath and writes the decrypted data to outputPath
func decryptFile(inputPath, outputPath string, key []byte) error {
ciphertext, err := os.ReadFile(inputPath)
if err != nil {
return err
}

block, err := aes.NewCipher(key)
if err != nil {
return err
}

gcm, err := cipher.NewGCM(block)
if err != nil {
return err
}

nonceSize := gcm.NonceSize()
nonce, ciphertext := ciphertext[:nonceSize], ciphertext[nonceSize:]

// Decrypt the data
plaintext, err := gcm.Open(nil, nonce, ciphertext, nil)
if err != nil {
return err
}

return os.WriteFile(outputPath, plaintext, 0644)
}

0 comments on commit 8166a58

Please sign in to comment.