无法解码凯撒加密的消息

问题描述

我必须为学校编写一个解码凯撒加密消息的函数。我们的老师给了我们编码消息的代码,并要求我们根据给定的 b 函数编写一个解码任何加密消息的代码。 (没有给出键/移位,但在编码示例中明确不是 3,我们应该自己弄清楚。使用在线工具,我知道键是 17,但为了解决这个问题,我选择了已知的给定示例3)的移位

The given Function with the Shift 3:

func main () {
    var textN string = "Wenn man jetzt diesen Text Cäsar-Codieren möchte,dann macht man das so!" // Variable was given by tacher
    var textC string = "Zhqq pdq mhwdw glhvhq Whbw Fävdu-Frglhuhq pöfkwh,gdqq pdfkw pdq gdv vr!" // Variable was given by tacher
    fmt.Println(textN)
    encode(textN)
    decode(textC)
}

// The given Function with the shift 3:

func encode (text string){
        var ergebnis string
        for _,w:=range(text){
                if w>='A' && w<='Z' {
                    if w+3>'Z'{
                        ergebnis += string((w+3)-'Z'+'A')
                    } else {
                        ergebnis += string(w+3)
                    }
                } else if  w>='a' && w<='z'{
                    if w+3>'z' {
                        ergebnis += string((w+3)-'z'+'a')
                    } else {
                        ergebnis += string(w+3)
                    }
                } else {
                    ergebnis += string(w)
                }
            }
        fmt.Println(ergebnis)
}

// My decode funtion with the "backwards shift" 3:

func decode (text string) {
    var ergebnis string
    for _,w:=range(text){
        if w >='A' && w <='Z' {
            if w-3 < 'A' {
                // fmt.Println(string(w-3)) // Search for Mistakes made by me
                ergebnis += string((w-3)+'Z'-'A')
            } else {
                // fmt.Println(string(w-3)) // Search for Mistakes made by me
                ergebnis += string(w-3)
            }
        } else if  w>='a' && w<='z'{
            if w-3 < 'a' {
                // fmt.Println(string(w-3)) // Search for Mistakes made by me
                ergebnis += string((w-3)+'z'-'a')
            } else {
                // fmt.Println(string(w-3)) // Search for Mistakes made by me
                ergebnis += string(w-3)
            }
        } else{
            ergebnis += string(w)
        }
    }
    fmt.Println(ergebnis)
}

现在的问题是:textN 不等于 decode(textC),我的代码似乎在小写字母处失败,后移 3 个字母没有成为它们应该成为的解码字母。我在“z”和“x”看到了这个,我不知道为什么。我尝试提高/降低班次,但没有奏效,改变了正负和更大的下标志。我不知道该尝试什么,提前表示感谢。

解决方法

您可以使用一种解决方案,我可以提供其他较慢的解决方案,但如果您愿意,可以提供更有趣的解决方案。

func Chipher(input string,shift int) string {
    bts := []rune(input)
    var cursor *rune
    // i em using it like this to remove repetition but its little advanced
    // it may make you suspicious to teacher
    shiftFn := func(start,end rune) bool {
        r := *cursor
        // not in range we cannot shift it
        if r < start || r > end {
            return false
        }

        res := start + (r-start+rune(shift))%(end-start)
        if res < start {
            res += end - start + 1
        }
        *cursor = res

        return true
    }

    for i := range bts {
        cursor = &bts[i]
        // this is little trick,if one of expresions returns true,expressions
        // after will not get executed as result would be true anyway
        _ = shiftFn('a','z') || shiftFn('A','Z') || shiftFn('0','9')
    }

    return string(bts)
}

现在这一切都很美好,但我们也必须做测试

func TestChipher(t *testing.T) {
    testCases := []struct {
        desc          string
        input,output string
        shift         int
    }{
        {
            desc:   "simple shift",input:  "abcd",shift:  1,output: "bcde",},{
            desc:   "negative shift",shift:  -1,output: "zabc",{
            desc:   "numbers",input:  "0123",output: "1234",{
            desc:   "capital letters",input:  "ABCD",output: "BCDE",{
            desc:   "big shift",shift:  1000,output: "ABCD",}
    for _,tC := range testCases {
        t.Run(tC.desc,func(t *testing.T) {
            res := Chipher(tC.input,tC.shift)
            if res != tC.output {
                t.Errorf("\n%s :result\n%s :expected",res,tC.output)
            }
        })
    }
}

希望你能从中学到新的东西