-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
71 lines (58 loc) · 1.3 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
package main
import (
"fmt"
)
func main() {
//sample text input
msg := "THis is the normal text"
//sample key
key := "dog"
// encrypt
encipher := Encipher(msg,key)
fmt.Println(encipher)
// decrypt
decrypt := Decipher(encipher, key)
fmt.Println(decrypt)
}
func Sanitize(in string) string {
out := []rune{}
for _, v := range in {
if 65 <= v && v <= 90 {
out = append(out, v)
} else if 97 <= v && v <= 122 {
out = append(out, v-32)
}
}
return string(out)
}
func Quartets(in string) string {
out := make([]rune, 0, len(in))
for i, v := range in {
if i%4 == 0 && i != 0 {
out = append(out, rune(32))
}
out = append(out, v)
}
return string(out)
}
func EncodePair(a, b rune) rune {
return (((a - 'A') + (b - 'A')) % 26) + 'A'
}
func DecodePair(a, b rune) rune {
return (((((a - 'A') - (b - 'A')) + 26) % 26) + 'A')
}
func Encipher(msg, key string) string {
smsg, skey := Sanitize(msg), Sanitize(key)
out := make([]rune, 0, len(msg))
for i, v := range smsg {
out = append(out, EncodePair(v, rune(skey[i%len(skey)])))
}
return string(out)
}
func Decipher(msg, key string) string {
smsg, skey := Sanitize(msg), Sanitize(key)
out := make([]rune, 0, len(msg))
for i, v := range smsg {
out = append(out, DecodePair(v, rune(skey[i%len(skey)])))
}
return string(out)