-
Notifications
You must be signed in to change notification settings - Fork 1
/
Caesar Cipher.go
47 lines (41 loc) · 1.13 KB
/
Caesar Cipher.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
package main
import (
"fmt"
"os"
)
func cipher(text string, direction int) string {
shift, offset := rune(3), rune(26)
runes := []rune(text)
for index, char := range runes {
switch direction {
case -1: // encoding
if char >= 'a'+shift && char <= 'z' ||
char >= 'A'+shift && char <= 'Z' {
char = char - shift
} else if char >= 'a' && char < 'a'+shift ||
char >= 'A' && char < 'A'+shift {
char = char - shift + offset
}
case +1: // decoding
if char >= 'a' && char <= 'z'-shift ||
char >= 'A' && char <= 'Z'-shift {
char = char + shift
} else if char > 'z'-shift && char <= 'z' ||
char > 'Z'-shift && char <= 'Z' {
char = char + shift - offset
}
}
runes[index] = char
}
return string(runes)
}
func encode(text string) string { return cipher(text, -1) }
func decode(text string) string { return cipher(text, +1) }
func main() {
sec := os.Args[1]
fmt.Println("[+] Clear text: " + sec)
encoded := encode(sec)
fmt.Println("[+] Encoded: " + encoded)
decoded := decode(encoded)
fmt.Println("[+] Decoded: " + decoded)
}