-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcaesarCipher.go
62 lines (54 loc) · 1.88 KB
/
caesarCipher.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
56
57
58
59
60
61
62
// CaesarCipher is a function that takes a string and a number and returns the string encrypted with the caesar cipher.
//
// @function Encrypt - takes up a word with a shift value and returns the word encrypted with the caesar cipher.
// @function Decrypt - takes up an encrypted word with the corresponding shift value and returns the decrypted word.
package caesarCipher
// Encrypt - takes up a word with a shift value and returns an encryption.
//
// @param word - word to be encrypted
// @param shift - shift value
// @return - string
func Encrypt(word string, shift int) string {
var cipher string
// convert shift to int32 type
x := int32(shift)
// loop through the word
for _, char := range word {
// check if the character is a letter with range of a-z, A-Z
if char >= 'a'+x && char <= 'z' || char >= 'A'+x && char <= 'Z' {
// if the character is a letter, shift it
char -= x
} else if char >= 'a' && char < 'a'+x || char >= 'A' && char < 'A'+x {
char = char - x + 26
}
// convert char to string and append to cipher
cipher += string(char)
}
// Return the cipher
return cipher
}
// Decrypt - takes up an encrypted word with the corresponding shift value and returns the decrypted word.
//
// @param cipher - encrypted word
// @param shift - shift value
// @return - string
func Decrypt(cipher string, shift int) string {
var word string
// convert shift to int32 type
x := int32(shift)
// loop through the word
for _, char := range cipher {
// check if char is in the range of a-z or A-Z
if (char >= 'a' && char <= 'z'-x) || (char >= 'A' && char <= 'Z'-x) {
// add shift to char
char += x
} else if (char > 'z'-x && char <= 'z') || (char > 'Z'-x && char <= 'Z') {
// subtract 26(the length of characters) from char
char = char + x - 26
}
// convert char to string and append to cipher
word += string(char)
}
// Return the cipher
return word
}