-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvigenere-encryption.go
96 lines (73 loc) · 2.27 KB
/
vigenere-encryption.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
package main
import (
"bufio"
"fmt"
"os"
"strings"
"github.com/vigenere-cipher/v2/util"
)
func main() {
fmt.Print("Enter phrase to encode: ")
reader := bufio.NewReader(os.Stdin)
// ReadString will block until the delimiter is entered
plaintext, err := reader.ReadString('\n')
if err != nil {
fmt.Println("An error occured while reading input. Please try again", err)
return
}
// remove the delimeter from the string
plaintext = strings.TrimSuffix(plaintext, "\n")
plaintext = strings.ToUpper(plaintext)
fmt.Print("Enter keyword: ")
keyword, err := reader.ReadString('\n')
if err != nil {
fmt.Println("An error occured while reading the keyword. Please try again", err)
return
}
keyword = strings.TrimSuffix(keyword, "\n")
keyword = strings.ToUpper(keyword)
encryptedMsg := encode(plaintext, keyword)
fmt.Printf("Encrypted Message: %v\n", encryptedMsg)
decryptedMsg := decode(encryptedMsg, keyword)
fmt.Printf("Decrypted Message: %v\n", decryptedMsg)
}
func encode(plaintext string, keyword string) string {
ciphertext := ""
vigenereSquare := util.VigenereSquare()
keyQueue := strings.Split(keyword, "")
for _, letter := range plaintext {
if string(letter) != " " && letter != 13 {
// Get the cipher alphabet of the Vigenere Square to use based off the keyword character
var cipherRow int = util.ALPHABET_MAP[keyQueue[0]]
// Get the encrypted value
encryptedValue := vigenereSquare[cipherRow][util.ALPHABET_MAP[string(letter)]]
ciphertext += encryptedValue
// Update keyQueue
keyQueue = append(keyQueue[1:], keyQueue[0])
} else if string(letter) == " " {
ciphertext += " "
}
}
return ciphertext
}
func decode(ciphertext string, keyword string) string {
plaintext := ""
vigenereSquare := util.VigenereSquare()
keyQueue := strings.Split(keyword, "")
for _, letter := range ciphertext {
if string(letter) != " " {
var cipherRow int = util.ALPHABET_MAP[keyQueue[0]]
for i, v := range vigenereSquare[cipherRow] {
if v == string(letter) {
plaintext += util.ALPHABET[i]
break
}
}
// Update keyQueue
keyQueue = append(keyQueue[1:], keyQueue[0])
} else {
plaintext += " "
}
}
return plaintext
}