-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
147 lines (124 loc) · 4 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
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
package main
import (
"bufio"
"fmt"
"math/rand"
"os"
"strings"
"time"
)
func main() {
//Initialize letters slice and map of letters
letters := []string{"A", "B", "C", "D", "E",
"F", "G", "H", "I", "J", "K", "L", "M",
"N", "O", "P", "Q", "R", "S", "T",
"U", "V", "W", "X", "Y", "Z"}
letters_map := make(map[string]int)
for i := 0; i < len(letters); i++ {
letters_map[letters[i]] = i
}
//Create the vigenere square
var square [26][26]string
k := 0
//loop through rows
for i := 0; i < 26; i++ {
//loop through columns
for j := 0; j < 26; j++ {
k = (i + j) % 26
square[i][j] = letters[k]
}
}
fmt.Println("Do you want to:")
fmt.Println("1: Encrypt Message")
fmt.Println("2: Decrypt Message")
reader := bufio.NewReader(os.Stdin)
char, _, err := reader.ReadRune()
if err != nil {
fmt.Println(err)
}
switch char {
case '1':
key := ""
//initiate a new seed on runtime, and use it as seed for random generator
source := rand.NewSource(time.Now().UnixNano())
gen := rand.New(source)
fmt.Println("Random Keys:")
//print 4 random keys of length 8
for i := 0; i < 4; i++ {
key = "" //reset key
for j := 0; j < 8; j++ {
key += letters[gen.Intn(26)]
}
//Print out the key
fmt.Printf("Key %v: %v\n", i, key)
}
fmt.Println("Please enter a key to use for encryption:")
reader := bufio.NewReader(os.Stdin)
raw_key, _ := reader.ReadString('\n')
// print out confirmation
fmt.Printf("You have entered key: %v", raw_key)
fmt.Println("Please enter a plaintext message:")
raw_plaintext, _ := reader.ReadString('\n')
// print out confirmation
fmt.Printf("You have entered text: %v", raw_plaintext)
clean_key := strings.TrimSpace(strings.ToUpper(strings.Replace(raw_key, " ", "", -1)))
clean_plaintext := strings.TrimSpace(strings.ToUpper(strings.Replace(raw_plaintext, " ", "", -1)))
//if key is longer than plaintext, truncate
if len(clean_key) > len(clean_plaintext) {
clean_key = clean_key[:len(clean_plaintext)]
}
key_text := ""
for len(key_text) < len(clean_plaintext) {
key_text += clean_key
}
if len(key_text) > len(clean_plaintext) {
key_text = key_text[:len(clean_plaintext)]
}
ciphertext := ""
for i := 0; i < len(clean_plaintext); i++ {
ciphertext += square[letters_map[string(key_text[i])]][letters_map[string(clean_plaintext[i])]]
}
fmt.Printf("Cleaned and formatted Key: %v\n", clean_key)
fmt.Printf("Cleaned and formatted Plaintext: %v\n", clean_plaintext)
fmt.Printf("Cleaned and formatted Keytext: %v\n", key_text)
fmt.Printf("Cleaned and formatted Ciphertext: %v\n", ciphertext)
break
case '2':
fmt.Println("Please enter a key to use for decryption:")
reader := bufio.NewReader(os.Stdin)
raw_key, _ := reader.ReadString('\n')
// print out confirmation
fmt.Printf("You have entered key: %v", raw_key)
fmt.Println("Please enter a ciphertext message:")
raw_ciphertext, _ := reader.ReadString('\n')
// print out confirmation
fmt.Printf("You have entered text: %v", raw_ciphertext)
clean_key := strings.TrimSpace(strings.ToUpper(strings.Replace(raw_key, " ", "", -1)))
clean_ciphertext := strings.TrimSpace(strings.ToUpper(strings.Replace(raw_ciphertext, " ", "", -1)))
//if key is longer than ciphertext, truncate
if len(clean_key) > len(clean_ciphertext) {
clean_key = clean_key[:len(clean_ciphertext)]
}
key_text := ""
for len(key_text) < len(clean_ciphertext) {
key_text += clean_key
}
if len(key_text) > len(clean_ciphertext) {
key_text = key_text[:len(clean_ciphertext)]
}
plaintext := ""
for i := 0; i < len(clean_ciphertext); i++ {
for j := 0; j < 26; j++ {
if square[letters_map[string(key_text[i])]][j] == string(clean_ciphertext[i]) {
plaintext += letters[j]
break
}
}
}
fmt.Printf("Cleaned and formatted Key: %v\n", clean_key)
fmt.Printf("Cleaned and formatted Ciphertext: %v\n", clean_ciphertext)
fmt.Printf("Cleaned and formatted Keytext: %v\n", key_text)
fmt.Printf("Cleaned and formatted Plaintext: %v\n", plaintext)
break
}
}