The introduction to the Go language is simple: implement the Vigenere encryption algorithm

The code was invented in 1553 by Italian cryptologist Giovan Battista Bellaso, but for centuries has been credited to 16th-century French cryptologist Blaise de Vigenere, who devised a similar code in 1586.

Input : Plaintext :   GEEKSFORGEEKS
             Keyword :  AYUSH
Output : Ciphertext :  GCYCZFMLYLEIM
For generating key, the given keyword is repeated
in a circular manner until it matches the length of 
the plain text.
The keyword "AYUSH" generates the key "AYUSHAYUSHAYU"
The plain text is then encrypted using the process 
explained below.

Encryption:

The first letter G of the plaintext is paired with the first letter A of the key. So use row G and column A of the Vigenere square, which is G. Similarly, for the second letter of the plaintext, use the second letter of the key, the letter in line E, and the letter in column Y is C. Plaintext is encrypted in a similar way.

Decryption is done by going to the row in the table corresponding to the key, finding the position of the ciphertext letter in that row, and then taking the label of that column as the plaintext. For example, in line A (from AYUSH), the ciphertext G appears in column G, which is the first plaintext letter. Next, we go to row Y (from AYUSH) and find the ciphertext C found in column E, so E is the second plaintext letter.

package main
import (
    "fmt"
    "strings"
)
func encodeString(cipher, key rune) rune {
    const asciiA rune = 65
    const numLetters = 26
    plainTextIndex := cipher + key
    asciiLetter := (plainTextIndex+numLetters)%numLetters + asciiA
    return asciiLetter
}
func encode(message, kw string) string {
    var plainText strings.Builder
    kwChars := []rune(kw)
    for i, cipherChar := range message {
        key := i % len(kwChars)
        plainText.WriteRune(encodeString(cipherChar, kwChars[key]))
    }
    return plainText.String()
}
func decipherString(cipher, key rune) rune {
    const asciiA rune = 65
    const numLetters = 26
    plainTextIndex := cipher - key
    asciiLetter := (plainTextIndex+numLetters)%numLetters + asciiA
    return asciiLetter
}
func decipher(message, kw string) string {
    var plainText strings.Builder
    kwChars := []rune(kw)
    for i, cipherChar := range message {
        key := i % len(kwChars)
        plainText.WriteRune(decipherString(cipherChar, kwChars[key]))
    }
    return plainText.String()
}
func main() {
    fmt.Println("Enter Your string: ")
    var first string
    fmt.Scanln(&first)
    fmt.Println("Enter your KEY: ")
    var second string
    fmt.Scanln(&second)
    cipherText := first
    keyword := second
    fmt.Print("Do you want to  1. Encrypt or 2. Decrypt")
    var option int
    fmt.Scanln(&option)
    if option == 1 {
        fmt.Println(encode(cipherText, keyword))
    } else if option == 2 {
        fmt.Println(decipher(cipherText, keyword))
    } else {
        fmt.Println("please choose the right option")
    }
}

Leave a Reply

Your email address will not be published. Required fields are marked *

en_USEnglish