登录
首页 >  Golang >  Go问答

无法解码凯撒加密消息

来源:stackoverflow

时间:2024-03-17 17:48:28 252浏览 收藏

对于学校作业,你需要编写一个函数来解码凯撒加密消息。虽然老师提供了编码消息的代码,但解码函数需要你自己编写。尽管已知密钥是 17,但出于练习目的,你选择了已知的位移 3。你的解码函数在解码小写字母时遇到了问题,具体表现在向后移动 3 个字母后,字母没有成为应有的解码字母。

问题内容

我必须为学校编写一个函数来解码凯撒加密的消息。我们的老师给了我们对消息进行编码的代码,并要求我们根据给定的函数编写一个代码来解码任何加密的消息。 (密钥/shift 没有给出,但明确不是 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",
            input:  "abcd",
            shift:  -1,
            output: "zabc",
        },
        {
            desc:   "numbers",
            input:  "0123",
            shift:  1,
            output: "1234",
        },
        {
            desc:   "capital letters",
            input:  "ABCD",
            shift:  1,
            output: "BCDE",
        },
        {
            desc:   "big shift",
            input:  "ABCD",
            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)
            }
        })
    }
}

希望你能从中学到新东西

好了,本文到此结束,带大家了解了《无法解码凯撒加密消息》,希望本文对你有所帮助!关注golang学习网公众号,给大家分享更多Golang知识!

声明:本文转载于:stackoverflow 如有侵犯,请联系study_golang@163.com删除
相关阅读
更多>
最新阅读
更多>
课程推荐
更多>