Created
March 8, 2024 09:57
-
-
Save aztack/af7e04c4047e067a86eaa851f68b8f61 to your computer and use it in GitHub Desktop.
xor
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
GOOS=windows GOARCH=amd64 go 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
// 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