The Author Online Book Forums are Moving

The Author Online Book Forums will soon redirect to Manning's liveBook and liveVideo. All book forum content will migrate to liveBook's discussion forum and all video forum content will migrate to liveVideo. Log in to liveBook or liveVideo with your Manning credentials to join the discussion!

Thank you for your engagement in the AoF over the years! We look forward to offering you a more enhanced forum experience.

mhw (2) [Avatar] Offline
#1
Here is another solution using strings.Repeat:

package main

import "fmt"
import "strings"

func main() {
	cipherText := "CSOITEUIWUIZNSROCNKFD"
	keyword := "GOLANG"
	s := len(cipherText)
	k := len(keyword)
	key := strings.Repeat(keyword, s/k) + keyword[:s%k] // key = "GOLANGGOLANGGOLANGGOL"
	message := ""

	for i, _ := range cipherText {
		if cipherText[i] >= key[i] {
			message += string('A' + (cipherText[i] - key[i]))
		} else {
			message += string('Z' + 1 + (cipherText[i] - key[i]))
		}
	}

	fmt.Println(message)
}


Link to the solution in Go's Playground: https://play.golang.org/p/Em-lUGjRjL
Nathan Youngman (56) [Avatar] Offline
#2
mhw wrote:Here is another solution using strings.Repeat:

package main

import "fmt"
import "strings"

func main() {
	cipherText := "CSOITEUIWUIZNSROCNKFD"
	keyword := "GOLANG"
	s := len(cipherText)
	k := len(keyword)
	key := strings.Repeat(keyword, s/k) + keyword[:s%k] // key = "GOLANGGOLANGGOLANGGOL"
	message := ""

	for i, _ := range cipherText {
		if cipherText[i] >= key[i] {
			message += string('A' + (cipherText[i] - key[i]))
		} else {
			message += string('Z' + 1 + (cipherText[i] - key[i]))
		}
	}

	fmt.Println(message)
}


Link to the solution in Go's Playground: https://play.golang.org/p/Em-lUGjRjL


Interesting approach and nice work using the slice syntax as well.

How would you encode a string though? I'm not entirely sure about the code:
cipherText[i] >= key[i]


It looks like this code is intended to both cipher and decipher text, but if so, there is a bug. Try deciphering what's there, then run that text through the program, and then run that result through again.
mhw (2) [Avatar] Offline
#3
For ciphering and deciphering we need two different functions. Here is the modified program with two functions cipher and decipher:


package main

import "fmt"
import "strings"

func decipher(cipherT string, keyW string) string {
	message := ""
	for i, _ := range cipherT {
		if cipherT[i] >= keyW[i] {
			message += string('A' + (cipherT[i] - keyW[i]))
		} else {
			message += string('Z' + 1 + (cipherT[i] - keyW[i]))
		}
	}
	return message
}

func cipher(plainT string, keyW string) string {
	cipherT := ""
	for i, _ := range plainT {
		if plainT[i] + (keyW[i]-'A') <= 'Z' {
			cipherT += string(plainT[i] + (keyW[i]-'A'))
		} else {
			cipherT += string(plainT[i] + (keyW[i]-'Z'-1))
		}
	}
	return cipherT
}

func main() {
	cipherText := "CSOITEUIWUIZNSROCNKFD"
	keyword := "GOLANG"
	s := len(cipherText)
	k := len(keyword)
	key := strings.Repeat(keyword, s/k) + keyword[:s%k] // key = "GOLANGGOLANGGOLANGGOL" ""
	
	plainText := decipher(cipherText , key)
	newCiphertext := cipher(plainText, key)

	fmt.Println("ciphertext: ", cipherText )
	fmt.Println("original message: ", plainText)
	fmt.Println("ciphering of original message: ", newCiphertext)
	
}



Concerning the condition if cipherText[i] >= key[i]:

In the following I write c[i] for ciphertext[i], m[i] for message[i] and k[i] for key[i].

From the definition of cipher we have either
(1) c[i] = m[i] + k[i] - 'A'
or
(2) c[i] = m[i] + k[i] - ('Z'+1).

Further 'A' <= m[i] <= 'Z'.

We have

(1) if and only if c[i] - k[i] = m[i] - 'A'

(2) if and only if c[i] - k[i] = m[i] - ('Z'+1)

As m[i] >= 'A' we get: (1) if and only if c[i] >= k[i]

As m[i] <= 'Z' we get: (2) if and only if c[i] < k[i].

Therefore from c[i] >= k[i] we get (1) and therefore we further get m[i] = c[i] + 'A' - k[i].