MCPcopy
hub / github.com/TheAlgorithms/Go / Encrypt

Function Encrypt

cipher/caesar/caesar.go:10–28  ·  view source on GitHub ↗

Encrypt encrypts by right shift of "key" each character of "input"

(input string, key int)

Source from the content-addressed store, hash-verified

8
9// Encrypt encrypts by right shift of "key" each character of "input"
10func Encrypt(input string, key int) string {
11 // if key is negative value,
12 // updates "key" the number which congruents to "key" modulo 26
13 key8 := byte(key%26+26) % 26
14
15 var outputBuffer []byte
16 // b is a byte, which is the equivalent of uint8.
17 for _, b := range []byte(input) {
18 newByte := b
19 if 'A' <= b && b <= 'Z' {
20 outputBuffer = append(outputBuffer, 'A'+(newByte-'A'+key8)%26)
21 } else if 'a' <= b && b <= 'z' {
22 outputBuffer = append(outputBuffer, 'a'+(newByte-'a'+key8)%26)
23 } else {
24 outputBuffer = append(outputBuffer, newByte)
25 }
26 }
27 return string(outputBuffer)
28}
29
30// Decrypt decrypts by left shift of "key" each character of "input"
31func Decrypt(input string, key int) string {

Callers 5

rot13Function · 0.92
DecryptFunction · 0.70
TestEncryptFunction · 0.70
ExampleFunction · 0.70
FuzzCaesarFunction · 0.70

Calls

no outgoing calls

Tested by 3

TestEncryptFunction · 0.56
ExampleFunction · 0.56
FuzzCaesarFunction · 0.56