Golang realizes Caesar password

Keywords: Go

I. idea of Caesar password encryption code

Basic ideas:

  1. Set plaintext and shift step size (secret key)
  2. Convert clear text to lowercase, prepare clear text byte slice and ciphertext slice
  3. Each plaintext character is rotated according to the step size of displacement and stored in the ciphertext slice
  4. Return ciphertext

  • Import package
import (
    "fmt"
    "strings" // Include string operation related methods
)
  • Caesar code
//I. Caesar password encryption
func caesarEn(strRaw string, step byte) string {
    //1. Clear text to lowercase
    str_raw := strings.ToLower(strRaw)
    //2. Displacement step
    step_move := step
    //3. Convert string to 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. Cycle clear text slice
    for i := 0; i < len(str_slice_src); i++ {
        //6. If the plaintext character of the current cycle is within the displacement range, add the 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 the plaintext character is out of range, the step size after displacement is subtracted by 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)
}

II. Caesar password decryption code ideas

Basic ideas:

  1. Set ciphertext and displacement step
  2. Prepare ciphertext character slice and plaintext character slice
  3. Each ciphertext character is rotated according to the displacement step length, and stored in the plaintext slice
  4. Return plaintext

  • Caesar password decryption code
//2. Caesar password decryption
func caesarDe(strCipher string, step byte) string {
    //1. Clear text to lowercase
    str_cipher := strings.ToLower(strCipher)
    //2. Displacement step
    step_move := step
    //3. Convert string to 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. Cycle clear text slice
    for i := 0; i < len(str_slice_src); i++ {
        //6. If the plaintext character of the current cycle is within the displacement range, add the 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 the plaintext character is out of range, the step size after displacement is subtracted by 26
            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)
}

Posted by Jorge on Wed, 04 Dec 2019 03:36:55 -0800