From 37eeccaa731b521c4962b9d9485dc72f40e80fe7 Mon Sep 17 00:00:00 2001 From: Aaron Johnon Date: Mon, 2 Sep 2024 02:02:32 -0500 Subject: [PATCH] Added decoder --- nsdecode.go | 74 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 74 insertions(+) create mode 100644 nsdecode.go diff --git a/nsdecode.go b/nsdecode.go new file mode 100644 index 0000000..4b6f4e0 --- /dev/null +++ b/nsdecode.go @@ -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()) +} +