Description

The Alphabet Cipher, published by Lew Carroll in 1868, is a cipher for passing secret messages. The cipher involves alphabet substitution using a shared keyword. Using the alphabet cipher to tranmit messages follows this procedure:

You must make a substitution chart like this, where each row of the alphabet is rotated by one as each letter goes down the chart. All test cases will utilize this same substitution chart.

   ABCDEFGHIJKLMNOPQRSTUVWXYZ
 A abcdefghijklmnopqrstuvwxyz
 B bcdefghijklmnopqrstuvwxyza
 C cdefghijklmnopqrstuvwxyzab
 D defghijklmnopqrstuvwxyzabc
 E efghijklmnopqrstuvwxyzabcd
 F fghijklmnopqrstuvwxyzabcde
 G ghijklmnopqrstuvwxyzabcdef
 H hijklmnopqrstuvwxyzabcdefg
 I ijklmnopqrstuvwxyzabcdefgh
 J jklmnopqrstuvwxyzabcdefghi
 K klmnopqrstuvwxyzabcdefghij
 L lmnopqrstuvwxyzabcdefghijk
 M mnopqrstuvwxyzabcdefghijkl
 N nopqrstuvwxyzabcdefghijklm
 O opqrstuvwxyzabcdefghijklmn
 P pqrstuvwxyzabcdefghijklmno
 Q qrstuvwxyzabcdefghijklmnop
 R rstuvwxyzabcdefghijklmnopq
 S stuvwxyzabcdefghijklmnopqr
 T tuvwxyzabcdefghijklmnopqrs
 U uvwxyzabcdefghijklmnopqrst
 V vwxyzabcdefghijklmnopqrstu
 W wxyzabcdefghijklmnopqrstuv
 X xyzabcdefghijklmnopqrstuvw
 Y yzabcdefghijklmnopqrstuvwx
 Z zabcdefghijklmnopqrstuvwxy

Both people exchanging messages must agree on the secret keyword. To be effective, this keyword should not be written down anywhere, but memorized.

To encode the message, first write it down.

thepackagehasbeendelivered

Then, write the keyword, (for example, snitch), repeated as many times as necessary.

snitchsnitchsnitchsnitchsn
thepackagehasbeendelivered

Now you can look up the column S in the table and follow it down until it meets the T row. The value at the intersection is the letter l. All the letters would be thus encoded.

snitchsnitchsnitchsnitchsn
thepackagehasbeendelivered
lumicjcnoxjhkomxpkwyqogywq

The encoded message is now lumicjcnoxjhkomxpkwyqogywq

To decode, the other person would use the secret keyword and the table to look up the letters in reverse.

Input Description

Each input will consist of two strings, separate by a space. The first word will be the secret word, and the second will be the message to encrypt.

snitch thepackagehasbeendelivered

Output Description

Your program should print out the encrypted message.

lumicjcnoxjhkomxpkwyqogywq

Challenge Inputs

bond theredfoxtrotsquietlyatmidnight
train murderontheorientexpress
garden themolessnuckintothegardenlastnight

Challenge Outputs

uvrufrsryherugdxjsgozogpjralhvg
flrlrkfnbuxfrqrgkefckvsa
zhvpsyksjqypqiewsgnexdvqkncdwgtixkx

Bonus

For a bonus, also implement the decryption portion of the algorithm and try to decrypt the following messages.

Bonus Inputs

cloak klatrgafedvtssdwywcyty
python pjphmfamhrcaifxifvvfmzwqtmyswst
moore rcfpsgfspiecbcc

Bonus Outputs

iamtheprettiestunicorn
alwayslookonthebrightsideoflife
foryoureyesonly

In [69]:
from string import ascii_lowercase
from itertools import cycle

In [70]:
ORD_OFFSET = 97

In [71]:
def shifted_letter_index(letter):
    return ord(letter.lower()) - ORD_OFFSET

def get_letter_from_ord(ord_num):
    return chr(ord_num + ORD_OFFSET).lower()

def encrypt_letter(secret_letter, decrypted_letter):
    secret_shift = shifted_letter_index(secret_letter)
    rotated_letters = ascii_lowercase[secret_shift:] + ascii_lowercase[:secret_shift]
    letter_index = shifted_letter_index(decrypted_letter)
    return rotated_letters[letter_index]

def decrypt_letter(secret_letter, encrypted_letter):
    secret_shift = shifted_letter_index(secret_letter)
    rotated_letters = ascii_lowercase[secret_shift:] + ascii_lowercase[:secret_shift]
    letter_ord = rotated_letters.index(encrypted_letter)
    return get_letter_from_ord(letter_ord)

In [72]:
def encrypt_message(secret, decrypted_message):
    return ''.join(encrypt_letter(*letters) for letters in zip(cycle(secret), decrypted_message))

In [73]:
def decrypt_message(secret, encrypted_message):
    return ''.join(decrypt_letter(*letters) for letters in zip(cycle(secret), encrypted_message))

In [18]:
encrypt_message('snitch', 'thepackagehasbeendelivered')


Out[18]:
'lumicjcnoxjhkomxpkwyqogywq'

In [19]:
encrypt_message('bond', 'theredfoxtrotsquietlyatmidnight')


Out[19]:
'uvrufrsryherugdxjsgozogpjralhvg'

In [20]:
encrypt_message('train', 'murderontheorientexpress')


Out[20]:
'flrlrkfnbuxfrqrgkefckvsa'

In [21]:
encrypt_message('garden','themolessnuckintothegardenlastnight')


Out[21]:
'zhvpsyksjqypqiewsgnexdvqkncdwgtixkx'

In [22]:
x = encrypt_message('cloak', 'iamtheprettiestunicorn')
print(x)
decrypt_message('cloak', x)


klatrgafedvtssdwywcyty
Out[22]:
'iamtheprettiestunicorn'

In [23]:
x = encrypt_message('python', 'alwayslookonthebrightsideoflife')
print(x)
decrypt_message('python', x)


pjphmfamhrcaifxifvvfmzwqtmyswst
Out[23]:
'alwayslookonthebrightsideoflife'

In [24]:
x = encrypt_message('moore', 'foryoureyesonly')
print(x)
decrypt_message('moore', x)


rcfpsgfspiecbcc
Out[24]:
'foryoureyesonly'

In [ ]: