Encrypt encrypts by right shift of "key" each character of "input"
(input string, key int)
| 8 | |
| 9 | // Encrypt encrypts by right shift of "key" each character of "input" |
| 10 | func 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" |
| 31 | func Decrypt(input string, key int) string { |
no outgoing calls