Header

Header

Thursday, May 30, 2019

Converting Python Caesar Cipher to Go Language Part 6

In this post the entire Caesar Cipher program will be listed:

 // Caesar Cipher  
 // Ported from https://inventwithpython.com/chapter14.html  
 package main  
 import (  
      "bufio"  
      "fmt"  
      "os"  
      "strconv"  
      "strings"  
      "unicode"  
 )  
 // declare variables and constants  
 const maxKeySize = 26  
 var mode string  
 var message string  
 var key rune  
 // prompt for encrypt or decrypt mode  
 func getMode() string {  
      for {  
           fmt.Println("Do you wish to encrypt or decrypt or brute force a message?")  
           var mode string  
           fmt.Scanln(&mode)  
           mode = strings.ToLower(mode)  
           if strings.Contains("encrypt e decrypt d brute b", mode) {  
                return mode  
           }  
           fmt.Println("Enter either \"encrypt\" or \"e\" or \"decrypt\" or \"d\" or \"brute\" or \"b\".")  
      }  
 }  
 // get message to encrypt or decrypt  
 func getMessage() string {  
      reader := bufio.NewReader(os.Stdin)  
      // print the prompt  
      fmt.Println("Enter your message:")  
      // use new reader to grap message  
      input, _ := reader.ReadString('\n')  
      return input  
 }  
 // Get the key value for the cipher offset  
 func getKey() rune {  
      var key rune  
      for {  
           fmt.Printf("Enter the key number (1 - %d)", maxKeySize)  
           fmt.Scanln(&key)  
           if key >= 1 && key <= maxKeySize {  
                return key  
           }  
      }  
 }  
 func getTranslatedMessage(mode string, message string, key rune) string {  
      if mode[0:len(mode)] == "d" {  
           key = -key  
      }  
      translated := ""  
      for _, symbol := range message {  
           if unicode.IsLetter(symbol) {  
                num := symbol  
                num += key  
                if unicode.IsUpper(symbol) {  
                     if num > 90 { // "Z"  
                          num -= 26  
                     } else if num < 65 { // "A"  
                          num += 26  
                     }  
                } else if unicode.IsLower(symbol) {  
                     if num > 122 { // "z"  
                          num -= 26  
                     } else if num < 97 { // "a"  
                          num += 26  
                     }  
                }  
                translated += string(rune(num))  
           } else {  
                translated += string(rune(symbol))  
           }  
      }  
      return strings.TrimSpace(translated)  
 }  
 func main() {  
      mode = getMode()  
      message = getMessage()  
      if mode[0:len(mode)] != "b" {  
           key = getKey()  
      }  
      fmt.Println("Your translated text is:")  
      if mode[0:len(mode)] != "b" {  
           fmt.Println(getTranslatedMessage(mode, message, key))  
      } else {  
           for i := 1; i <= maxKeySize; i++ {  
                fmt.Println(strconv.Itoa(i) + ". " + getTranslatedMessage("decrypt", message, rune(i)))  
           }  
      }  
 }  

No comments:

Post a Comment