-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathviginere.go
57 lines (48 loc) · 1.13 KB
/
viginere.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
package main
import (
"fmt"
"os"
"strings"
)
var grid = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
var grid_len = len(grid)
var ord_start = grid[0]
type Operation int
const (
ENCIPHER Operation = iota
DECIPHER
)
func process(key, phrase string, method Operation) string {
output := ""
key = strings.ToUpper(key)
key = strings.Replace(key, " ", "", -1)
phrase = strings.ToUpper(phrase)
phrase = strings.Replace(phrase, " ", "", -1)
direction := 1
if method == DECIPHER {
direction = -1
}
key_len := len(key)
for pos, c := range phrase {
char_index := int(byte(c) - ord_start)
key_index := pos % key_len
key_ord := int(key[key_index] - ord_start)
grid_offset := (grid_len + char_index + (key_ord * direction)) % grid_len
output += string(grid[grid_offset])
}
return output
}
func main() {
if len(os.Args) != 3 {
fmt.Println("Usage: viginere <key> <message>")
os.Exit(0)
}
key := os.Args[1]
phrase := os.Args[2]
fmt.Println("Key:", key)
fmt.Println("Phrase:", phrase)
enc := process(key, phrase, ENCIPHER)
fmt.Println("Ciphertext:", enc)
plaintext := process(key, enc, DECIPHER)
fmt.Println("Plaintext:", plaintext)
}