-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathMain.go
More file actions
86 lines (79 loc) · 1.94 KB
/
Main.go
File metadata and controls
86 lines (79 loc) · 1.94 KB
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
package main
import (
"bufio"
"fmt"
"io/ioutil"
"log"
"os"
)
func XOREncrypt(data []byte, key string) []byte {
CL := []byte(key)
FP := 112
BA := make([]byte, len(data)+1)
KA := 0
for MP := 0; MP < len(data); MP++ {
BA[MP] += byte((int(data[MP]) ^ int(CL[KA])) ^ FP)
if KA == len(key)-1 {
KA = 0
} else {
KA = KA + 1
}
}
BA[len(data)] = byte(112 ^ FP)
return BA
}
func XORDecrypt(data []byte, key string) []byte {
X1 := []byte(key)
A1 := int(data[len(data)-1]) ^ 112
BN := make([]byte, len(data))
CC := 0
for EE := 0; EE < len(data)-1; EE++ {
BN[EE] = byte((int(data[EE]) ^ A1) ^ int(X1[CC]))
if CC == len(key)-1 {
CC = 0
} else {
CC = CC + 1
}
}
return BN
}
func main() {
fmt.Println("Enter 0 for encryption or 1 for decryption:")
scanner := bufio.NewScanner(os.Stdin)
var choice int
for scanner.Scan() {
input := scanner.Text()
if input == "0" || input == "1" {
choice = int(input[0] - '0')
break
}
fmt.Println("Invalid choice! Please enter 0 for encryption or 1 for decryption:")
}
fmt.Println("Enter the file path:")
scanner.Scan()
filePath := scanner.Text()
fmt.Println("Enter the key:")
scanner.Scan()
key := scanner.Text()
fmt.Println("Enter the output file name:")
scanner.Scan()
outFilePath := scanner.Text()
data, err := ioutil.ReadFile(filePath)
if err != nil {
log.Fatal(err)
}
var outputData []byte
if choice == 0 {
outputData = XOREncrypt(data, key)
} else if choice == 1 {
outputData = XORDecrypt(data, key)
} else {
fmt.Println("Invalid choice!")
return
}
err = ioutil.WriteFile(outFilePath, outputData, 0644)
if err != nil {
log.Fatal(err)
}
fmt.Println("Done!")
}