Header

Header

Thursday, May 30, 2019

Converting Python Caesar Cipher to Go Language Part 5

In this post we will continue converting the Caesar Cipher Python program to Go Language.

Let us take a look at the main function:

 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)))  
           }  
      }  
 }  

In the main function, everything comes together,  all previous defined functions are now put into place and called to perform the encrypt and decrypt functionality of the Caesar Cipher program.

Please refer to each earlier post for details on the functionality of each function.

We start out by calling the getMode() function and the results of the getMode() function are stored in the mode variable.  The getMode() function determines whether we encrypt, decrypt or brute force decrypt the stored message.

Next  the main() function calls the getMessage() function which stores the results to the message variable.  The getMessage() function captures the encrypted or un-encrypted message, typed into the console by the user.

The program then prints a message before the getTranslatedMessage() function is called informing the user with the string "Your translated text is".

 if mode[0:len(mode)] != "b" {  
           fmt.Println(getTranslatedMessage(mode, message, key))  

In the above if statement:  mode[0:len(mode)] is treated as a slice:  https://blog.golang.org/go-slices-usage-and-internals

Starting at the first index, strings are zero based in Go, till the end of the string, we are looking for any character other than "b".  If the condition evaluates to true we execute the getTranslatedMessage() function.

If mode contains either "b" or "brute" we loop through each key value to brute force the decryption.  Each translated message is printed on it's own line.

In the next post we will list the entire Caesar Cipher program.

No comments:

Post a Comment