package main
import (
"fmt"
"strconv"
"strings"
)
func encrypt(str string) string {
// 1. Reverse the string
reversed := reverseString(str)
// 2. Apply char map and put it into binary
encrypted := ""
for _, char := range reversed {
encrypted += strconv.FormatInt(int64(charMap(string(char))), 2)
}
return encrypted
}
func decrypt(encrypted string) string {
// 1. Reverse the binary string
reversedBinary := reverseString(encrypted)
// 2. Apply reverse char map and reverse the output
decrypted := ""
for i := 0; i < len(reversedBinary); i += 7 {
binaryChar := reversedBinary[i : i+7]
decrypted += string(reverseCharMap(binaryToDecimal(binaryChar)))
}
return reverseString(decrypted)
}
// Helper function to reverse a string
func reverseString(str string) string {
runes := []rune(str)
for i, j := 0, len(runes)-1; i < j; i, j = i+1, j-1 {
runes[i], runes[j] = runes[j], runes[i]
}
return string(runes)
}
// Helper function to map characters to integers
func charMap(char string) int {
charMap := map[string]int{
"a": 1,
"b": 2,
"c": 3,
// Add more character mappings here
}
return charMap[char]
}
// Helper function to reverse character map
func reverseCharMap(num int) string {
reverseCharMap := map[int]string{
1: "a",
2: "b",
3: "c",
// Add more character mappings here
}
return reverseCharMap[num]
}
// Helper function to convert binary to decimal
func binaryToDecimal(binary string) int {
decimal, _ := strconv.ParseInt(binary, 2, 64)
return int(decimal)
}
func main() {
message := "hello"
encrypted := encrypt(message)
fmt.Println("Encrypted:", encrypted)
decrypted := decrypt(encrypted)
fmt.Println("Decrypted:", decrypted)
}