5 kyu

Vigenère Autokey Cipher Helper

624 of 1,230jacobb

Description:

This is the advanced version of the Vigenère Cipher Helper kata. The following assumes that you have already completed that kata -- if you haven't done it yet, you should start there.

The basic concept is the same as in the previous kata (see the detailed explanation there). However, there is a major difference:

With the basic Vigenère Cipher, we assume the key is repeated for the length of the text. In this kata, the key is only used once, and then complemented by the decoded text. Thus every encoding and decoding is independent (still using the same key to begin with). Furthermore, the key index is only incremented if the current letter is in the provided alphabet.

Visual representation:

"password"                         // original key
"my secret code i want to secure"  // message
"pa ssword myse c retc od eiwant"  // full key used

Write a class that, when given a key and an alphabet, can be used to encode and decode from the cipher.

Examples

var alphabet = 'abcdefghijklmnopqrstuvwxyz';
var key = 'password';

// creates a cipher helper with each letter substituted
// by the corresponding character in the key
var c = new VigenèreCipher(key, alphabet);

c.encode('codewars'); // returns 'rovwsoiv'
c.decode('laxxhsj');  // returns 'waffles'

c.encode('amazingly few discotheques provide jukeboxes')
// returns 'pmsrebxoy rev lvynmylatcwu dkvzyxi bjbswwaib'

c.decode('pmsrebxoy rev lvynmylatcwu dkvzyxi bjbswwaib')
// returns 'amazingly few discotheques provide jukeboxes'
alphabet = 'abcdefghijklmnopqrstuvwxyz'
key = 'password'

# creates a cipher helper with each letter substituted
# by the corresponding character in the key
c = VigenereCipher(key, alphabet)

c.encode('codewars') # returns 'rovwsoiv'
c.decode('laxxhsj')  # returns 'waffles'

c.encode('amazingly few discotheques provide jukeboxes')
# returns 'pmsrebxoy rev lvynmylatcwu dkvzyxi bjbswwaib'

c.decode('pmsrebxoy rev lvynmylatcwu dkvzyxi bjbswwaib')
# returns 'amazingly few discotheques provide jukeboxes'
alphabet = 'abcdefghijklmnopqrstuvwxyz'
key = 'password'

# creates a cipher helper with each letter substituted
# by the corresponding character in the key
c = new VigenereCipher(key, alphabet)

c.encode('codewars')  # returns 'rovwsoiv'
c.decode('laxxhsj')   # returns 'waffles'

c.encode('amazingly few discotheques provide jukeboxes')
# returns 'pmsrebxoy rev lvynmylatcwu dkvzyxi bjbswwaib'

c.decode('pmsrebxoy rev lvynmylatcwu dkvzyxi bjbswwaib')
# returns 'amazingly few discotheques provide jukeboxes'

Any character not in the alphabet should be left alone. For example (following from above):

c.encode('CODEWARS'); // returns 'CODEWARS'
c.encode('CODEWARS') # returns 'CODEWARS'
c.encode('CODEWARS') # returns 'CODEWARS'
Algorithms
Ciphers
Security
Object-oriented Programming
Strings

More By Author:

Check out these other kata created by jacobb

Stats:

CreatedJan 12, 2014
PublishedJan 12, 2014
Warriors Trained4931
Total Skips1797
Total Code Submissions26897
Total Times Completed1230
JavaScript Completions610
Python Completions624
CoffeeScript Completions20
Total Stars267
% of votes with a positive feedback rating76% of 217
Total "Very Satisfied" Votes141
Total "Somewhat Satisfied" Votes49
Total "Not Satisfied" Votes27
Ad
Contributors
  • jacobb Avatar
  • jhoffner Avatar
  • talamb Avatar
  • MMMAAANNN Avatar
  • acosta-edgar Avatar
  • anter69 Avatar
  • kazk Avatar
  • Voile Avatar
  • bidouille Avatar
Ad