-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcaesar_cipher.go
55 lines (50 loc) · 1.21 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
48
49
50
51
52
53
54
55
package main
import (
"fmt"
)
func main() {
var choice int
fmt.Println("Choose operation:")
fmt.Println("1. Encrypt")
fmt.Println("2. Decrypt")
fmt.Print("Enter your choice (1 or 2): ")
fmt.Scanln(&choice)
switch choice {
case 1:
var plaintext string
var shift int
fmt.Print("Enter plaintext: ")
fmt.Scanln(&plaintext)
fmt.Print("Enter shift (positive integer): ")
fmt.Scanln(&shift)
ciphertext := encrypt(plaintext, shift)
fmt.Println("Ciphertext:", ciphertext)
case 2:
var ciphertext string
var shift int
fmt.Print("Enter ciphertext: ")
fmt.Scanln(&ciphertext)
fmt.Print("Enter shift (positive integer): ")
fmt.Scanln(&shift)
plaintext := decrypt(ciphertext, shift)
fmt.Println("Plaintext:", plaintext)
default:
fmt.Println("Invalid choice.")
}
}
func encrypt(plaintext string, shift int) string {
var result string
for _, char := range plaintext {
if char >= 'A' && char <= 'Z' {
result += string((char-'A'+rune(shift))%26 + 'A')
} else if char >= 'a' && char <= 'z' {
result += string((char-'a'+rune(shift))%26 + 'a')
} else {
result += string(char)
}
}
return result
}
func decrypt(ciphertext string, shift int) string {
return encrypt(ciphertext, -shift)
}