Coding With Fun
Home Docker Django Node.js Articles Python pip guide FAQ Policy

How to achieve Caesar encryption in the Go language


Jun 01, 2021 Article blog



In the 2nd century, an effective way to send confidential messages is to shift each letter so that 'a' becomes 'd' 'b' 'e' and so on. The result of this processing looks like a foreign language:

L fdph, L vdz, L frqtxhuhg. Julius Caesar

As Code Listing 9-6 shows, it is very easy to use a computer to process characters numerically.

Code Listing 9-6 deals with a single character: caesar.go

c := 'a'
c=c+3
fmt.Printf("%c", c)    // 打印出“d”

(Recommended course: Go tutorial)

However, the method shown in Code Listing 9-6 is not perfect because it does not consider how to handle the characters 'x' 'y' and 'z' so it cannot encrypt words such as xylophones yaks and zebras T o solve this problem, the original Caesar encryption method took a winding measure, that is, 'x' to 'a' 'y' to 'b' and 'z' to 'c' For a 26-character Alphabet, we can do this with this code:

if c > 'z' {
    c = c - 26
}

Caesar's password is decrypted in the opposite way to encryption, the program no longer adds 3 to the character but subtracts 3, and it also needs to add 26 characters to implement a loop when the character is too small, which is c < 'a' Although the above encryption and decryption methods are very intuitive, the actual encoding process can be quite painful because they all require processing character boundaries for rounding.

Swing 13 (rotate 13, or ROT13 for short) is a variation of the Caesar password in the 20th century, and the only difference between this variant and the Caesar password is that it adds 13 instead of 3 to the character, and ROT13 encryption and decryption can be done in the same way, which is very convenient.

Now, suppose that the agency searching for Extra-terrestrial Intelligence, SETI found a broadcast containing the following messages while scanning alien communications in outer space:

message := "uv vagreangvbany fcnpr fgngvba"

We have a hunch that this message is most likely english text encrypted with ROT13 but before decrypting this message, we also need to know the number of characters it contains, which contains 30 characters and can be determined by the built-in len function:

fmt.Println(len(message))    // 打印出“30”

Note Go has a small number of built-in functions that can be used without importing statements, and len function is one of them that measures the length of different types of values. F or example, in the code above, len returns the byte length of the string type. Code Listing 9-7 shows a decryption program for messages from outer space, and you only need to run the code at Go Playground to know what the aliens are talking about.

Code Listing 9-7 ROT13 message decryption: rot13.go

message := "uv vagreangvbany fcnpr fgngvba"


for i := 0; i < len(message); i++ {    // 迭代字符串中的每一个 ASCII 字符
    c := message[i]
    if c >= 'a' && c <= 'z' {    // 只解密英文字母,至于空格和标点符号则保持不变
        c = c + 13
        if c > 'z' {
            c = c - 26
        }
    }
    fmt.Printf("%c", c)
}

(Recommended micro-class: Go micro-class)

Note that the ROT13 implementation in this code can only handle ASCII characters (bytes), and it cannot handle messages written in Spanish or Russian, although the next section will give a solution to this problem.

Here's a look at the introduction to implementing Caesar encryption in Go language, and I hope it will help you.