Skip to content

Commit 86fa072

Browse files
Add files via upload
1 parent e09f3b0 commit 86fa072

File tree

1 file changed

+47
-0
lines changed

1 file changed

+47
-0
lines changed

Caesar Cipher.go

+47
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
package main
2+
3+
import (
4+
"fmt"
5+
"os"
6+
)
7+
8+
func cipher(text string, direction int) string {
9+
10+
shift, offset := rune(3), rune(26)
11+
runes := []rune(text)
12+
13+
for index, char := range runes {
14+
switch direction {
15+
case -1: // encoding
16+
if char >= 'a'+shift && char <= 'z' ||
17+
char >= 'A'+shift && char <= 'Z' {
18+
char = char - shift
19+
} else if char >= 'a' && char < 'a'+shift ||
20+
char >= 'A' && char < 'A'+shift {
21+
char = char - shift + offset
22+
}
23+
case +1: // decoding
24+
if char >= 'a' && char <= 'z'-shift ||
25+
char >= 'A' && char <= 'Z'-shift {
26+
char = char + shift
27+
} else if char > 'z'-shift && char <= 'z' ||
28+
char > 'Z'-shift && char <= 'Z' {
29+
char = char + shift - offset
30+
}
31+
}
32+
runes[index] = char
33+
}
34+
return string(runes)
35+
}
36+
37+
func encode(text string) string { return cipher(text, -1) }
38+
func decode(text string) string { return cipher(text, +1) }
39+
40+
func main() {
41+
sec := os.Args[1]
42+
fmt.Println("[+] Clear text: " + sec)
43+
encoded := encode(sec)
44+
fmt.Println("[+] Encoded: " + encoded)
45+
decoded := decode(encoded)
46+
fmt.Println("[+] Decoded: " + decoded)
47+
}

0 commit comments

Comments
 (0)