当前位置:网站首页>Getting started with the go language is simple: go implements the Caesar password
Getting started with the go language is simple: go implements the Caesar password
2022-07-04 13:49:00 【51CTO】
Use Caesar's password to move characters on text . call strings.Map
Method .
1 Caesar code encryption
Caesar code ( English :Caesar cipher), Or Caesar encryption 、 Caesar transforms 、 Transform encryption , It's the simplest and most widely known encryption technology .
The Caesar code is a kind of Replace encryption technology , All the letters in the plaintext go backwards in the alphabet ( Or forward ) Offset by a fixed number and replaced with ciphertext . for example , When the offset is zero 3 When , All the letters A Will be replaced by D;B become E, And so on . This encryption method is named after Caesar in the Roman Republic , It is said that Caesar used this method to contact his generals .
Plaintext alphabet :ABCDEFGHIJKLMNOPQRSTUVWXYZ Ciphertext alphabet :DEFGHIJKLMNOPQRSTUVWXYZABC
design idea
- Set plaintext and move step size ( Secret text )
- Convert clear text to lowercase , Prepare clear text byte slices and ciphertext slices
- Each plaintext character rotates according to the displacement step and is stored in the ciphertext
- Back to ciphertext
2 Go Realization
Caesar passwords are generally delivered in the form of passwords . Like this string of code “exxegoexsrgi
” It's a password .
By moving the letters , We can encode messages . This prevents casual snooping . stay Go in , We can use strings.Map
Method to achieve this .
2.1 Import package
2.2 To write caesar Method
Then let's write caesarEn()
Caesar password encryption method . This will receive a string and return a modified string .
It moves characters , Then move the character to the valid range .
// Caesar code encryption
func caesarEn( strRaw string, step byte) string {
//1. Make text lowercase
str_raw : = strings. ToLower( strRaw)
//2. Define the step size
step_move : = step
//3. Convert a string to a plaintext character slice
str_slice_src : = [] byte( str_raw)
fmt. Println( "Clear text character slice:", str_slice_src)
//4. Create a ciphertext character slice
str_slice_dst : = str_slice_src
//5. Loop through text slices
for i : = 0; i < len( str_slice_src); i ++ {
//6. If the plaintext feature of the current period is within the displacement range , Please add a displacement step directly to save the ciphertext character slice
if str_slice_src[ i] < 123 - step_move {
str_slice_dst[ i] = str_slice_src[ i] + step_move
} else { //7. If plaintext characters are out of range , Then add the step length after displacement and subtract 26
str_slice_dst[ i] = str_slice_src[ i] + step_move - 26
}
}
//8. Output results
fmt. Println( "The encryption result is:", step_move, str_slice_dst, string( str_slice_dst))
return string( str_slice_dst)
}
- 1.
- 2.
- 3.
- 4.
- 5.
- 6.
- 7.
- 8.
- 9.
- 10.
- 11.
- 12.
- 13.
- 14.
- 15.
- 16.
- 17.
- 18.
- 19.
- 20.
- 21.
- 22.
- 23.
- 24.
- 25.
- 26.
3 Caesar password decryption
thought :
- Set ciphertext and displacement steps
- Prepare ciphertext character slice and plaintext character slice
- The characters of each ciphertext rotate according to the displacement step , And stored in the plaintext slice
- Return clear text
Go Caesar decrypts the code :
//2. Caesar password decryption
func caesarDe( strCipher string, step byte) string {
//1. Make text lowercase
str_cipher : = strings. ToLower( strCipher)
//2. Alternate step size
step_move : = step
//3. Convert a string to a plaintext character slice
str_slice_src : = [] byte( str_cipher)
fmt. Println( "Ciphertext character slice:", str_slice_src)
//4. Create a ciphertext character slice
str_slice_dst : = str_slice_src
//5. Loop through character text slices
for i : = 0; i < len( str_slice_src); i ++ {
//6. If the plaintext feature of the current period is within the displacement range , Please add a displacement step directly to save the ciphertext character slice
if str_slice_src[ i] >= 97 + step_move {
str_slice_dst[ i] = str_slice_src[ i] - step_move
} else { //7. If plaintext characters are out of range , Then add 26 Step length after subtracting displacement
str_slice_dst[ i] = str_slice_src[ i] + 26 - step_move
}
}
//8. Output results
fmt. Println( "The decryption result is:", step_move, str_slice_dst, string( str_slice_dst))
return string( str_slice_dst)
}
- 1.
- 2.
- 3.
- 4.
- 5.
- 6.
- 7.
- 8.
- 9.
- 10.
- 11.
- 12.
- 13.
- 14.
- 15.
- 16.
- 17.
- 18.
- 19.
- 20.
- 21.
- 22.
- 23.
- 24.
- 25.
- 26.
4 Other implementations
package main
import (
"errors"
"fmt"
"reflect"
"regexp"
)
var TBL = [] rune( "abcdefghijklmnopqrstuvwxyz")
var CLUES = [] string{ "this", "the", "that"}
var (
ErrLength = errors. New( "invalid length")
ErrChar = errors. New( "invalid char")
ErrNoClue = errors. New( "no clue word")
ErrShift = errors. New( "invalid shift value")
)
func Encrypt( in string, sh int) ( enc string, err error) {
err = assert( in)
if sh < 0 {
err = ErrShift
}
if err != nil {
return
}
enc = shift( in, sh)
return
}
func Decrypt( in string) ( dec string, sh int, err error) {
err = assert( in)
if err != nil {
return
}
var hit bool = false
subin : = subStr( in)
for i : = 0; i < len( CLUES); i ++ {
subclue : = subStr( CLUES[ i])
for j : = 0; j < len( subin) - len( subclue) + 1; j ++ {
if reflect. DeepEqual( subin[ j: j + 1], subclue[ 0: len( subclue) - 1]) {
sh = subtract([] rune( in)[ j], [] rune( CLUES[ i])[ 0])
hit = true
break
}
}
}
if ! hit {
err = ErrNoClue
return
}
dec = shift( in, - sh)
return
}
func assert( in string) ( err error) {
if regexp. MustCompile( `[^a-z\. \r\n]`). MatchString( in) {
err = ErrChar
} else if len( in) > 80 {
err = ErrLength
}
return
}
func shift( in string, sh int) ( out string) {
for _, v : = range in {
if v == '.' || v == ' ' || v == '\r' || v == '\n' {
out += string( v)
continue
}
i : = indexOf( TBL, v)
len : = len( TBL)
var ii int = ( i + sh) % len
if ii < 0 {
ii += len
}
if ii > len {
ii -= len
}
out += string( TBL[ ii])
}
return
}
func subtract( left rune, right rune) ( out int) {
l : = indexOf( TBL, left)
r : = indexOf( TBL, right)
out = l - r
if out < 0 {
out += len( TBL)
}
return
}
func subStr( in string) [] int {
subin : = make([] int, 0, 79)
for i : = range in {
if i > len( in) - 2 {
break
}
subin = append( subin, subtract([] rune( in)[ i], [] rune( in)[ i + 1]))
}
// return
return subin
}
func indexOf( target [] rune, searchChar rune) int {
for i, v : = range target {
if v == searchChar {
return i
}
}
return - 1
}
func main() {
in : = "xlmw mw xli tmgxyvi xlex m xsso mr xli xvmt."
fmt. Printf( "in : '%s'\n", in)
out, sh, err : = Decrypt( in)
fmt. Printf( "out: '%s'\n", out)
fmt. Printf( "sh : %d\n", sh)
fmt. Printf( "err: %v\n", err)
}
- 1.
- 2.
- 3.
- 4.
- 5.
- 6.
- 7.
- 8.
- 9.
- 10.
- 11.
- 12.
- 13.
- 14.
- 15.
- 16.
- 17.
- 18.
- 19.
- 20.
- 21.
- 22.
- 23.
- 24.
- 25.
- 26.
- 27.
- 28.
- 29.
- 30.
- 31.
- 32.
- 33.
- 34.
- 35.
- 36.
- 37.
- 38.
- 39.
- 40.
- 41.
- 42.
- 43.
- 44.
- 45.
- 46.
- 47.
- 48.
- 49.
- 50.
- 51.
- 52.
- 53.
- 54.
- 55.
- 56.
- 57.
- 58.
- 59.
- 60.
- 61.
- 62.
- 63.
- 64.
- 65.
- 66.
- 67.
- 68.
- 69.
- 70.
- 71.
- 72.
- 73.
- 74.
- 75.
- 76.
- 77.
- 78.
- 79.
- 80.
- 81.
- 82.
- 83.
- 84.
- 85.
- 86.
- 87.
- 88.
- 89.
- 90.
- 91.
- 92.
- 93.
- 94.
- 95.
- 96.
- 97.
- 98.
- 99.
- 100.
- 101.
- 102.
- 103.
- 104.
- 105.
- 106.
- 107.
- 108.
- 109.
- 110.
- 111.
- 112.
- 113.
- 114.
- 115.
- 116.
- 117.
- 118.
- 119.
- 120.
- 121.
- 122.
- 123.
- 124.
- 125.
- 126.
- 127.
- 128.
- 129.
- 130.
- 131.
- 132.
- 133.
- 134.
- 135.
- 136.
- 137.
- 138.
- 139.
- 140.
- 141.
- 142.
- 143.
- 144.
- 145.
- 146.
- 147.
- 148.
- 149.
- 150.
- 151.
- 152.
- 153.
5 test
package main
import (
"fmt"
"strings"
)
func caesar( r rune, shift int) rune {
// Shift character by specified number of places.
// ... If beyond range, shift backward or forward.
s : = int( r) + shift
if s > 'z' {
return rune( s - 26)
} else if s < 'a' {
return rune( s + 26)
}
return rune( s)
}
func main() {
value : = "test"
fmt. Println( value)
// Test the caesar method in a func argument to strings.Map.
value2 : = strings. Map( func( r rune) rune {
return caesar( r, 18)
}, value)
value3 : = strings. Map( func( r rune) rune {
return caesar( r, - 18)
}, value2)
fmt. Println( value2, value3)
value4 : = strings. Map( func( r rune) rune {
return caesar( r, 1)
}, value)
value5 : = strings. Map( func( r rune) rune {
return caesar( r, - 1)
}, value4)
fmt. Println( value4, value5)
value = "exxegoexsrgi"
result : = strings. Map( func( r rune) rune {
return caesar( r, - 4)
}, value)
fmt. Println( value, result)
}
- 1.
- 2.
- 3.
- 4.
- 5.
- 6.
- 7.
- 8.
- 9.
- 10.
- 11.
- 12.
- 13.
- 14.
- 15.
- 16.
- 17.
- 18.
- 19.
- 20.
- 21.
- 22.
- 23.
- 24.
- 25.
- 26.
- 27.
- 28.
- 29.
- 30.
- 31.
- 32.
- 33.
- 34.
- 35.
- 36.
- 37.
- 38.
- 39.
- 40.
- 41.
- 42.
- 43.
- 44.
- 45.
- 46.
Run the program :
6 summary
This paper briefly introduces an interesting Caesar cipher in cryptography , The algorithm is an alternative encryption technology , And in Go The code implements the encryption and decryption process of the algorithm .
边栏推荐
- C语言职工管理系统
- 爬虫练习题(一)
- Three schemes to improve the efficiency of MySQL deep paging query
- Xilinx/system-controller-c/boardui/ unable to connect to the development board, the solution of jamming after arbitrary operation
- C basic supplement
- 动画与过渡效果
- E-week finance | Q1 the number of active people in the insurance industry was 86.8867 million, and the licenses of 19 Payment institutions were cancelled
- Comprehensive evaluation of modular note taking software: craft, notation, flowus
- CANN算子:利用迭代器高效实现Tensor数据切割分块处理
- C语言小型商品管理系统
猜你喜欢
【AI系统前沿动态第40期】Hinton:我的深度学习生涯与研究心法;Google辟谣放弃TensorFlow;封神框架正式开源
[AI system frontier dynamics, issue 40] Hinton: my deep learning career and research mind method; Google refutes rumors and gives up tensorflow; The apotheosis framework is officially open source
HAProxy高可用解决方案
How real-time cloud interaction helps the development of education industry
Xue Jing, director of insight technology solutions: Federal learning helps secure the flow of data elements
Interviewer: what is the difference between redis expiration deletion strategy and memory obsolescence strategy?
字节面试算法题
聊聊支付流程的设计与实现逻辑
N++ is not reliable
源码编译安装MySQL
随机推荐
在 Apache 上配置 WebDAV 服务器
动画与过渡效果
高质量软件架构的唯一核心指标
基于STM32+华为云IOT设计的酒驾监控系统
Alibaba cloud award winning experience: build a highly available system with polardb-x
Don't turn down, three sentences to clarify the origin of cross domain resource request errors
Personalized online cloud database hybrid optimization system | SIGMOD 2022 selected papers interpretation
诸神黄昏时代的对比学习
Oracle 被 Ventana Research 评为数字创新奖总冠军
ViewBinding和DataBinding的理解和区别
Personalized online cloud database hybrid optimization system | SIGMOD 2022 selected papers interpretation
Apache服务器访问日志access.log设置
c#数组补充
2022KDD预讲 | 11位一作学者带你提前解锁优秀论文
易周金融 | Q1保险行业活跃人数8688.67万人 19家支付机构牌照被注销
SQL语言
字节面试算法题
Reading cognitive Awakening
Dgraph: large scale dynamic graph dataset
聊聊支付流程的设计与实现逻辑