Skip to content

Instantly share code, notes, and snippets.

@aztack
Created March 8, 2024 09:57
Show Gist options
  • Save aztack/af7e04c4047e067a86eaa851f68b8f61 to your computer and use it in GitHub Desktop.
Save aztack/af7e04c4047e067a86eaa851f68b8f61 to your computer and use it in GitHub Desktop.
xor
GOOS=windows GOARCH=amd64 go build
// main.go
package main
import (
"fmt"
"io/ioutil"
"os"
)
func encryptFileWithPassword(filePath string, password string) error {
file, err := os.Open(filePath)
if err != nil {
return err
}
defer file.Close()
// Read the entire file into memory
content, err := ioutil.ReadAll(file)
if err != nil {
return err
}
// XOR encrypt the file content with the password
for i := 0; i < len(content); i++ {
content[i] ^= password[i%len(password)]
}
// Write the encrypted content to a new file
outputFile, err := os.Create(filePath + ".encrypted")
if err != nil {
return err
}
defer outputFile.Close()
_, err = outputFile.Write(content)
if err != nil {
return err
}
return nil
}
func main() {
if len(os.Args) < 3 {
fmt.Println("Usage: xor <file path> <password>")
return
}
filePath := os.Args[1]
password := os.Args[2]
err := encryptFileWithPassword(filePath, password)
if err != nil {
fmt.Println("Error:", err)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment