50 lines
1.1 KiB
Go
50 lines
1.1 KiB
Go
package otpencrypt
|
|
|
|
import (
|
|
"strings"
|
|
"one-time-pad-utils/pad_definitions"
|
|
)
|
|
|
|
func OTPEncrypt(message string, key string) string {
|
|
message = strings.Replace(message, " ", "", -1)
|
|
messagesplit := strings.Split(message,"")
|
|
var messagesplitint []int
|
|
|
|
key = strings.Replace(key, " ", "", -1)
|
|
keysplit := strings.Split(key,"")
|
|
var keysplitint []int
|
|
|
|
for _, char := range messagesplit {
|
|
for n, c := range paddefinitions.NumToCharMap{
|
|
if c == strings.ToUpper(char) {
|
|
messagesplitint = append(messagesplitint, n)
|
|
|
|
}
|
|
}
|
|
}
|
|
|
|
for _, char := range keysplit {
|
|
for n, c := range paddefinitions.NumToCharMap{
|
|
if c == strings.ToUpper(char) {
|
|
keysplitint = append(keysplitint, n)
|
|
|
|
}
|
|
}
|
|
}
|
|
|
|
if len(keysplitint) < len(messagesplitint) {
|
|
return ("KEY TOO SHORT TO ENCRYPT MESSAGE")
|
|
}
|
|
|
|
var encryptedmessage []string
|
|
|
|
for pos, messagenum := range messagesplitint{
|
|
if pos != 0 && pos % 5 == 0 {
|
|
encryptedmessage = append(encryptedmessage, " ")
|
|
}
|
|
encryptedmessage = append(encryptedmessage, paddefinitions.NumToCharMap[(messagenum + keysplitint[pos]) % 35])
|
|
}
|
|
|
|
return (strings.Join(encryptedmessage, ""))
|
|
}
|