-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsimplecipherV3.go
112 lines (100 loc) · 2.02 KB
/
simplecipherV3.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
package main
import (
"fmt"
)
var letter map[string]int
const key = "somekey"
const plaintext = "thequickbrownfoxjumpedoverthelazydog"
func main() {
letter = map[string]int{
"a": 0,
"b": 1,
"c": 2,
"d": 3,
"e": 4,
"f": 5,
"g": 6,
"h": 7,
"i": 8,
"j": 9,
"k": 10,
"l": 11,
"m": 12,
"n": 13,
"o": 14,
"p": 15,
"q": 16,
"r": 17,
"s": 18,
"t": 19,
"u": 20,
"v": 21,
"w": 22,
"x": 23,
"y": 24,
"z": 25,
}
caesar := improvedcaesar(13)
fmt.Printf("Caesar = %v\n", caesar)
ciphertext := encode(plaintext)
fmt.Printf("Vigenere encoded = %v\n", ciphertext)
deciphertext := decode(ciphertext)
fmt.Printf("Vigenere decoded = %v\n", deciphertext)
}
func encode(plaintext string) string {
var ciphertext string
var x string
for i, v := range plaintext {
if i < len(key) {
x = key[i : i+1]
} else {
mult := int(i / len(key))
x = key[i-len(key)*mult : i-(len(key)*mult)+1]
}
shift := letter[x]
topletter := string('z' - shift)
reverseshift := 26 - shift
if string(v) <= topletter {
ciphertext += string(int(v) + shift)
} else {
ciphertext += string(int(v) - reverseshift)
}
}
return ciphertext
}
func decode(ciphertext string) string {
var plaintext string
var x string
for i, v := range ciphertext {
if i < len(key) {
x = key[i : i+1]
} else {
mult := int(i / len(key))
x = key[i-len(key)*mult : i-(len(key)*mult)+1]
}
shift := letter[x]
bottomletter := string('a' + shift)
reverseshift := 26 - shift
if string(v) >= bottomletter {
plaintext += string(int(v) - shift)
} else {
plaintext += string(int(v) + reverseshift)
}
}
return plaintext
}
func improvedcaesar(shift int) string {
const alphabet = 26
reverseshift := alphabet - shift
topletter := string('z' - shift)
const plaintext = "thequickbrownfoxjumpsoverthelazydog"
var ciphertext string
for _, v := range plaintext {
if string(v) <= topletter {
ciphertext += string(int(v) + shift)
} else {
ciphertext += string(int(v) - reverseshift)
}
}
return ciphertext
}