Added decoder

master
Aaron Johnon 1 year ago
parent 6e61330e31
commit 37eeccaa73

@ -0,0 +1,74 @@
package main
import (
"bufio"
"flag"
"fmt"
"io"
"os"
"strconv"
"strings"
"unicode"
)
// Function to decode number pairs into characters
func decodePair(pair string) rune {
alphabet := " ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"
num, err := strconv.Atoi(pair)
if err != nil || num < 0 || num >= len(alphabet) {
return rune(-1) // Return -1 if pair is not valid
}
return rune(alphabet[num])
}
// Function to apply the reverse shift and wrap around
func applyReverseShift(value, shift int) int {
shifted := (value - shift) % 37
if shifted < 0 {
shifted += 37
}
return shifted
}
func main() {
// Define command-line flags
shift := flag.Int("s", 0, "Shift amount for the cipher")
flag.Parse()
// Read all input from stdin
reader := bufio.NewReader(os.Stdin)
inputBytes, _ := io.ReadAll(reader)
input := string(inputBytes)
// Remove all spaces and line breaks
input = strings.ReplaceAll(input, " ", "")
input = strings.ReplaceAll(input, "\n", "")
input = strings.TrimSpace(input)
var decoded strings.Builder
// Decode the input string
for i := 0; i < len(input); i += 2 {
if i+1 >= len(input) {
break // If there's an odd number of characters, break out
}
pair := input[i : i+2]
num, err := strconv.Atoi(pair)
if err != nil {
continue // Skip invalid pairs
}
shiftedValue := applyReverseShift(num, *shift)
char := decodePair(fmt.Sprintf("%02d", shiftedValue))
if !unicode.IsPrint(char) || char == rune(-1) {
continue // Skip invalid characters
}
decoded.WriteRune(char)
}
// Output the decoded string
fmt.Println(decoded.String())
}
Loading…
Cancel
Save