Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to realize Caesar encryption through Go language

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

Editor to share with you how to achieve Caesar encryption through the Go language, I believe that most people do not know much about it, so share this article for your reference, I hope you can learn a lot after reading this article, let's go to know it!

In the second century, an effective way to send confidential messages was to shift each letter so that'a 'became' d','b 'became' e', and so on. The result of this treatment looks like a foreign language:

L fdph, L vdz, L frqtxhuhg. -Julius Caesar (Julius Caesar)

As shown in listing 9-6, it is very easy to use a computer to process characters numerically.

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

C: = 'a'c=c+3fmt.Printf ("% c", c) / / print out "d"

(recommended course: Go tutorial)

However, the method shown in listing 9-6 is not perfect because it does not consider what to do with the characters'x','y', and'z', so it cannot encrypt words such as xylophones, yaks, and zebras. In order to solve this problem, the original Caesar encryption took rewinding measures, that is, changing'x'to'a','y'to'b', and'z' to'c'. For the 26-character English alphabet, we can use this code to achieve the above transformation:

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

The decryption method of Caesar password is just the opposite of the encryption method. The program no longer adds 3 but subtracts 3 from the character, and it also requires that the character is too small, that is, c.

< 'a' 的时候, 将字符加上 26 以实施回绕。 虽然上述的加密方法和解密方法都非常直观, 但由于它们都需要处理字符边界以实现回绕, 因此实际的编码过程将变得相当痛苦。 回转 13 (rotate 13,简称ROT13)是凯撒密码在 20 世纪的一个变体, 该变体跟凯撒密码的唯一区别就在于, 它给字符添加的量是 13 而不是 3 , 并且 ROT13 的加密和解密可以通过同一个方法实现, 这是非常方便的。 现在, 假设搜寻外星智能 (Search for Extra-terrestrial Intelligence, SETI) 的相关机构在外太空扫描外星人通信信息的时候, 发现了包含以下消息的广播: message := "uv vagreangvbany fcnpr fgngvba" 我们有预感, 这条消息很可能是使用 ROT13 加密的英文文本, 但是在解密这条消息之前, 我们还需要知悉其包含的字符数量, 这条消息包含 30 个字符, 可以通过内置的 len 函数来确定: fmt.Println(len(message)) // 打印出"30" 注意 Go 拥有少量无须导入语句即可使用的内置函数, len 函数即是其中之一, 它可以测定各种不同类型的值的长度。 例如, 在上面的代码中, len 返回的就是 string 类型的字节长度。 代码清单 9-7 展示的就是外太空消息的解密程序, 你只需要在 Go Playground 运行这段代码, 就会知道外星人在说什么了。 代码清单 9-7 ROT13 消息解密: 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-26}} fmt.Printf ("% c", c)} above is all the content of the article "how to achieve Caesar encryption through Go". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report