forked from TheAlgorithms/Go
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcaesar.go
More file actions
29 lines (26 loc) · 976 Bytes
/
Copy pathcaesar.go
File metadata and controls
29 lines (26 loc) · 976 Bytes
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
// Package caesar is the shift cipher
// ref: https://en.wikipedia.org/wiki/Caesar_cipher
package caesar
// Encrypt encrypts by right shift of "key" each character of "input"
func Encrypt(input string, key int) string {
// if key is negative value,
// updates "key" the number which congruents to "key" modulo 26
key = (key%26 + 26) % 26
outputBuffer := []byte{}
for _, r := range input {
newByte := byte(r)
if 'A' <= newByte && newByte <= 'Z' {
outputBuffer = append(outputBuffer, byte(int('A')+int(int(newByte-'A')+key)%26))
} else if 'a' <= newByte && newByte <= 'z' {
outputBuffer = append(outputBuffer, byte(int('a')+int(int(newByte-'a')+key)%26))
} else {
outputBuffer = append(outputBuffer, newByte)
}
}
return string(outputBuffer)
}
// Decrypt decrypts by left shift of "key" each character of "input"
func Decrypt(input string, key int) string {
// left shift of "key" is same as right shift of 26-"key"
return Encrypt(input, 26-key)
}