2022-05-30 11:52:30 +02:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2022-05-30 12:50:59 +02:00
|
|
|
"fmt"
|
|
|
|
"log"
|
|
|
|
"regexp"
|
|
|
|
"strings"
|
|
|
|
"unicode"
|
2022-05-30 11:52:30 +02:00
|
|
|
)
|
|
|
|
|
2022-05-30 12:50:59 +02:00
|
|
|
const lettersInTheAlphabet = 26
|
|
|
|
const startUpperCase = 65
|
|
|
|
const endUpperCase = 65 + lettersInTheAlphabet
|
|
|
|
const startLowerCase = 97
|
|
|
|
const endLowerCase = 97 + lettersInTheAlphabet
|
|
|
|
|
|
|
|
var key string
|
|
|
|
|
|
|
|
type keyLetter struct {
|
|
|
|
upperCase string
|
|
|
|
position int32
|
|
|
|
}
|
|
|
|
|
|
|
|
func getKeyWord() {
|
|
|
|
var input string
|
|
|
|
fmt.Println("Please enter a key word:")
|
|
|
|
_, err := fmt.Scanln(&input)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatalln(err)
|
|
|
|
}
|
|
|
|
reg, err := regexp.Compile("[^a-zA-Z]+")
|
|
|
|
if err != nil {
|
|
|
|
log.Fatalln(err)
|
|
|
|
}
|
|
|
|
processedString := reg.ReplaceAllString(input, "")
|
|
|
|
key = strings.ToUpper(processedString)
|
|
|
|
}
|
|
|
|
|
|
|
|
func initKeyWord() []keyLetter {
|
|
|
|
var keyWord []keyLetter
|
|
|
|
for _, letter := range key {
|
|
|
|
keyWord = append(keyWord, keyLetter{
|
|
|
|
upperCase: string(letter),
|
|
|
|
position: letter - 'A',
|
|
|
|
})
|
|
|
|
}
|
|
|
|
fmt.Println(keyWord)
|
|
|
|
return keyWord
|
|
|
|
}
|
|
|
|
|
|
|
|
func encryptMessage(keyWord []keyLetter, message string) string {
|
|
|
|
encrypted := ""
|
|
|
|
index := 0
|
|
|
|
for _, letter := range message {
|
|
|
|
if letter == 32 {
|
|
|
|
encrypted += " "
|
|
|
|
} else {
|
|
|
|
keyWordIndex := index % len(keyWord)
|
|
|
|
newLetter := letter + keyWord[keyWordIndex].position
|
|
|
|
if (unicode.IsLower(letter) && newLetter >= endLowerCase) ||
|
|
|
|
(unicode.IsUpper(letter) && newLetter >= endUpperCase) {
|
|
|
|
newLetter -= lettersInTheAlphabet
|
|
|
|
}
|
|
|
|
encrypted += string(newLetter)
|
|
|
|
index += 1
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return encrypted
|
|
|
|
}
|
|
|
|
|
2022-05-30 11:52:30 +02:00
|
|
|
func main() {
|
2022-05-30 12:50:59 +02:00
|
|
|
message := "Make It Happen"
|
|
|
|
fmt.Println(message)
|
|
|
|
getKeyWord()
|
|
|
|
keyWord := initKeyWord()
|
|
|
|
fmt.Println(encryptMessage(keyWord, message))
|
2022-05-30 11:52:30 +02:00
|
|
|
}
|