-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcaesar_cipher.go
More file actions
42 lines (34 loc) · 1.1 KB
/
caesar_cipher.go
File metadata and controls
42 lines (34 loc) · 1.1 KB
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
package main
import (
"fmt"
"strings"
)
// Encrypt performs the Caesar cipher encryption on the given plaintext with the specified shift.
func Encrypt(plaintext string, shift int) string {
var encrypted strings.Builder
for _, char := range plaintext {
if char >= 'a' && char <= 'z' {
shifted := (char-'a'+rune(shift))%26 + 'a'
encrypted.WriteRune(shifted)
} else if char >= 'A' && char <= 'Z' {
shifted := (char-'A'+rune(shift))%26 + 'A'
encrypted.WriteRune(shifted)
} else {
encrypted.WriteRune(char)
}
}
return encrypted.String()
}
// Decrypt performs the Caesar cipher decryption on the given ciphertext with the specified shift.
func Decrypt(ciphertext string, shift int) string {
return Encrypt(ciphertext, -shift) // Decryption is just encryption with the negative shift
}
func main() {
plaintext := "Hello, World!"
shift := 3
encrypted := Encrypt(plaintext, shift)
decrypted := Decrypt(encrypted, shift)
fmt.Printf("Plaintext: %s\n", plaintext)
fmt.Printf("Encrypted: %s\n", encrypted)
fmt.Printf("Decrypted: %s\n", decrypted)
}