-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
63 lines (51 loc) · 1.35 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
package main
import (
"bufio"
"flag"
"fmt"
"os"
"strings"
"x_o_r/cipherer"
)
var mode = flag.String("mode", "cipher", "Set to 'cipher' or 'decipher'. Default is 'cipher'.")
var secretKey = flag.String("secret", "", "Your secret key. Must contain at least 1 character")
func main() {
flag.Parse()
if len(*secretKey) == 0 {
fmt.Fprintln(os.Stderr, "No secret is provided! Exiting now ...")
os.Exit(1)
}
switch *mode {
case "cipher":
plaintext := getUserInput("Enter your text to cipher: ")
cipheredText, err := cipherer.Cipher(plaintext, *secretKey)
if err != nil {
fmt.Fprintf(os.Stderr, "Error encrypting text: %v\n", err)
os.Exit(1)
}
fmt.Println(cipheredText)
case "decipher":
cipheredText := getUserInput("Enter your ciphered data to decipher: ")
decipheredText, err := cipherer.Decipher(cipheredText, *secretKey)
if err != nil {
fmt.Fprintf(os.Stderr, "Error decrypting text: %v\n", err)
os.Exit(1)
}
fmt.Println(decipheredText)
default:
fmt.Println("Invalid mode. Use 'cipher' or 'decipher'.")
os.Exit(1)
}
}
func getUserInput(msg string) string {
fmt.Print(msg)
reader := bufio.NewReader(os.Stdin)
for {
result, err := reader.ReadString('\n')
if err != nil {
fmt.Println("An error occured while reading the entered text! Please try again")
continue
}
return strings.TrimRight(result, "\r\n")
}
}