-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
14f6eec
commit 8166a58
Showing
6 changed files
with
127 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
stark,lannister,tyrell,targaryen,baratheon |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
ned stark |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
module go-encrypt | ||
|
||
go 1.22.5 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |