Skip to content

Instantly share code, notes, and snippets.

@husobee
Last active August 29, 2015 14:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save husobee/8fba028b7d18347752e1 to your computer and use it in GitHub Desktop.
Save husobee/8fba028b7d18347752e1 to your computer and use it in GitHub Desktop.
vigenere cipher in go
package main
import (
"flag"
"fmt"
)
// plaintext flag
var plaintext = flag.String("plaintext", "", "plaintext is the message you want to cipher")
// cipher key flag
var key = flag.String("key", "", "key is the key to use to cipher")
// cipher key flag
var ciphertext = flag.String("ciphertext", "", "ciphertext is the encrypted text")
func main() {
// parse the commandline flags
flag.Parse()
// convert strings from inputs to byte arrays
var ba_key = []byte(*key)
var ba_ciphertext = []byte(*ciphertext)
var ba_plaintext = []byte(*plaintext)
// validate inputs
if len(ba_key) < 1 {
panic("Key must exist")
}
// check for valid key chars
for _, letter := range ba_key {
// range checking input key
if letter < 65 || letter > 90 {
panic(fmt.Sprintf("char '%c' in key is out of range, A-Z only"))
}
}
// this counter will tell us which char of the key to use
var Ki_count int = 0
// if we are given plaintext, encrypt
if len(ba_ciphertext) < 1 && len(ba_plaintext) > 1 {
// for each of the characters in the plaintext
for i := 0; i < len(ba_plaintext); i++ {
// get the Mi (message at i)
Mi := ba_plaintext[i]
// get the Ki (key at i)
Ki := ba_key[Ki_count]
// range checking input plaintext
if Mi < 65 || Mi > 90 {
panic(fmt.Sprintf("char '%c' in plaintext is out of range, A-Z only"))
}
// append the ciphertext
ba_ciphertext = append(ba_ciphertext, ((Mi+Ki)-65<<1)%26+65)
// if we get beyond the length of ba_key, circle back to start
if Ki_count++; Ki_count >= len(ba_key) {
// start at the 0th element again
Ki_count = 0
}
}
fmt.Println("plaintext: ", string(ba_plaintext))
fmt.Println("ciphertext: ", string(ba_ciphertext))
} else if len(ba_ciphertext) > 1 && len(ba_plaintext) < 1 {
// for each of the characters in the plaintext
for i := 0; i < len(ba_ciphertext); i++ {
// get the Mi (message at i)
Ci := ba_ciphertext[i]
// get the Ki (key at i)
Ki := ba_key[Ki_count]
// range checking input plaintext
if Ci < 65 || Ci > 90 {
panic(fmt.Sprintf("char '%c' in ciphertext is out of range, A-Z only"))
}
// append the ciphertext
var CiMinusKi byte = 0
if Ci < Ki {
// if Ci - Ki is going to be neg, reverse so we get a positive,
// and then subtract 26 from that to get the letter position
CiMinusKi = 26 - (Ki - Ci)
} else {
// otherwise, subtract Ci from Ki
CiMinusKi = Ci - Ki
}
ba_plaintext = append(ba_plaintext, CiMinusKi%26+65)
// if we get beyond the length of ba_key, circle back to start
if Ki_count++; Ki_count >= len(ba_key) {
// start at the 0th element again
Ki_count = 0
}
}
fmt.Println("ciphertext: ", string(ba_ciphertext))
fmt.Println("plaintext: ", string(ba_plaintext))
} else {
fmt.Println("specify either ciphertext or plaintext")
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment