-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathencryptify.go
241 lines (206 loc) · 5.2 KB
/
encryptify.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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
package main
import (
"bufio"
"fmt"
"os"
"strconv"
"strings"
)
func main() {
fmt.Println("Welcome to the Encryptify!")
for {
toEncrypt, encoding, message := getInput()
if toEncrypt {
switch encoding {
case "ROT13":
fmt.Println(encryptROT13(message))
case "Reverse":
fmt.Println(encryptReverse(message))
case "XOR":
fmt.Println(encryptXOR(message))
default:
fmt.Println("Invalid encoding method.")
}
} else {
switch encoding {
case "ROT13":
fmt.Println(decryptROT13(message))
case "Reverse":
fmt.Println(decryptReverse(message))
case "XOR":
fmt.Println(decryptXOR(message))
default:
fmt.Println("Invalid encoding method.")
}
}
}
}
func getInput() (bool, string, string) {
reader := bufio.NewReader(os.Stdin)
var operation, encoding string
// Select operation (Encryption/Decryption)
for attempts := 0; attempts < 3; attempts++ {
fmt.Println("Select operation (1/2):")
fmt.Println("1. Encrypt.")
fmt.Println("2. Decrypt.")
operation, _ = reader.ReadString('\n')
operation = strings.TrimSpace(operation)
if operation == "1" || operation == "2" {
break
}
fmt.Println("Invalid input. Please enter 1 or 2.")
if attempts == 2 {
fmt.Println("Too many invalid attempts. Exiting.")
os.Exit(1)
}
}
toEncrypt := (operation == "1")
// Select cipher method
for attempts := 0; attempts < 3; attempts++ {
fmt.Println("Select cipher (1/2/3):")
fmt.Println("1. ROT13.")
fmt.Println("2. Reverse.")
fmt.Println("3. XOR.")
encoding, _ = reader.ReadString('\n')
encoding = strings.TrimSpace(encoding)
switch encoding {
case "1":
encoding = "ROT13"
case "2":
encoding = "Reverse"
case "3":
encoding = "XOR"
default:
fmt.Println("Invalid input. Please enter 1, 2, or 3.")
if attempts == 2 {
fmt.Println("Too many invalid attempts. Exiting.")
os.Exit(1)
}
continue
}
break
}
// Get the message
fmt.Printf("Enter the message: ")
message, _ := reader.ReadString('\n')
message = strings.TrimSpace(message)
return toEncrypt, encoding, message
}
func encryptROT13(s string) string {
var result strings.Builder
for _, char := range s {
switch {
case char >= 'A' && char <= 'Z':
result.WriteRune('A' + (char-'A'+13)%26)
case char >= 'a' && char <= 'z':
result.WriteRune('a' + (char-'a'+13)%26)
default:
result.WriteRune(char)
}
}
return result.String()
}
func decryptROT13(s string) string {
return encryptROT13(s) // ROT13 is symmetrical
}
func encryptReverse(s string) string {
var result strings.Builder
for _, char := range s {
switch {
case char >= 'A' && char <= 'Z':
result.WriteRune('Z' - (char - 'A'))
case char >= 'a' && char <= 'z':
result.WriteRune('z' - (char - 'a'))
default:
result.WriteRune(char)
}
}
return result.String()
}
func decryptReverse(s string) string {
return encryptReverse(s) // Reverse is symmetrical
}
func encryptXOR(s string) string {
reader := bufio.NewReader(os.Stdin)
var key string
for {
fmt.Printf("Please enter a key to encrypt: ")
keyInput, _ := reader.ReadString('\n')
keyInput = strings.TrimSpace(keyInput)
if len(keyInput) > 0 {
key = keyInput
break
}
fmt.Println("The key field is empty. Please try again.")
}
// Adjust key length
key = adjustKeyLength(key, len(s))
// XOR operation
encryptedBytes := make([]byte, len(s))
for i := 0; i < len(s); i++ {
encryptedBytes[i] = s[i] ^ key[i]
}
return bytesToHex(encryptedBytes)
}
func decryptXOR(encryptedS string) string {
reader := bufio.NewReader(os.Stdin)
var key string
for {
fmt.Printf("Please enter the key to decrypt: ")
keyInput, _ := reader.ReadString('\n')
keyInput = strings.TrimSpace(keyInput)
if len(keyInput) > 0 {
key = keyInput
break
}
fmt.Println("The key field is empty. Please try again.")
}
encryptedBytes := hexToBytes(encryptedS)
if len(encryptedBytes) == 0 {
fmt.Println("Invalid encrypted message. Restarting input process.")
return ""
}
// Adjust key length
key = adjustKeyLength(key, len(encryptedBytes))
// XOR operation for decryption
decryptedBytes := make([]byte, len(encryptedBytes))
for i := 0; i < len(encryptedBytes); i++ {
decryptedBytes[i] = encryptedBytes[i] ^ key[i]
}
return string(decryptedBytes)
}
// Adjusts the key length to match the input message length
func adjustKeyLength(key string, length int) string {
if len(key) < length {
repeated := strings.Repeat(key, (length/len(key))+1)
key = repeated[:length]
} else if len(key) > length {
key = key[:length]
}
return key
}
// Converts byte array into hexadecimal string
func bytesToHex(byteArray []byte) string {
var hexString strings.Builder
for _, b := range byteArray {
hexString.WriteString(fmt.Sprintf("%02x", b))
}
return hexString.String()
}
// Converts hexadecimal string into byte array
func hexToBytes(hexString string) []byte {
if len(hexString)%2 != 0 {
fmt.Println("Invalid hex string length.")
return []byte{}
}
byteArray := make([]byte, len(hexString)/2)
for i := 0; i < len(hexString); i += 2 {
byteValue, err := strconv.ParseUint(hexString[i:i+2], 16, 8)
if err != nil {
fmt.Println("Invalid hex input.")
return []byte{}
}
byteArray[i/2] = byte(byteValue)
}
return byteArray
}