-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathshiftCipher.go
56 lines (44 loc) · 1.26 KB
/
shiftCipher.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
package main
import (
"flag"
"fmt"
"os"
)
const M = 26
func main() {
flag.Usage = func () {
fmt.Fprintln(os.Stdout, "Usage: go run shiftCipher.go [params...]")
flag.PrintDefaults()
}
kPtr := flag.Int("k", 0, "encryption/decryption key")
cipherPtr := flag.String("cipher", "", "Ciphertext")
plainPtr := flag.String("plain", "", "Plaintext")
flag.Parse()
if (len(*cipherPtr) > 0) {
cipher := *cipherPtr
plain := make([]rune, len(cipher))
for k := 0; k < M; k++ {
for i, r := range []rune(cipher) {
if r == '\n' {
plain[i] = r
continue
}
j := int(r) - 'a' - k
if j < 0 {
plain[i] = rune(j + M + 'a')
} else {
plain[i] = rune(j + 'a')
}
}
fmt.Printf("key = %d\nPlaintext = %s\n\n", k, string(plain))
}
} else {
k := *kPtr
plain := *plainPtr
cipher := make([]rune, 0, len(plain))
for _, r := range []rune(plain) {
cipher = append(cipher, rune((int(r)-'a'+k) % 26 + 'a'))
}
fmt.Println("Ciphertext =", string(cipher))
}
}