Skip to content

Instantly share code, notes, and snippets.

@myano
Created March 27, 2014 20:06
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save myano/a6e78bda2b16d2e59d2c to your computer and use it in GitHub Desktop.
#!/usr/bin/env python
import string
new_alphabet = string.lowercase
top_right_alphabet = new_alphabet.replace('q', '')
new_alphabet = top_right_alphabet
keyword_one = 'exampl'
keyword_two = 'keyword'
encrypted_text = 'FYGMKYHOBXMFKKKIMD'
for char in keyword_one:
top_right_alphabet = top_right_alphabet.replace(char, '')
top_right = keyword_one + top_right_alphabet
bottom_left_alphabet = new_alphabet
for char in keyword_two:
bottom_left_alphabet = bottom_left_alphabet.replace(char, '')
bottom_left = keyword_two + bottom_left_alphabet.replace('q', '')
n = 5
bottom_left = [bottom_left[i:i+n] for i in range(0, len(bottom_left), 5)]
top_right = [top_right[i:i+n] for i in range(0, len(top_right), 5)]
new_alphabet_rows = [new_alphabet[i:i+n] for i in range(0, len(new_alphabet), 5)]
print 'top_right:'
p = 0
for each in top_right:
print '\t%s\t%s' % (new_alphabet_rows[p], each)
p += 1
print 'bottom_left:'
q = 0
for each in bottom_left:
print '\t%s\t%s' % (each, new_alphabet_rows[q])
q += 1
original_message = str()
k = 0
while k < len(encrypted_text):
one = encrypted_text[k]
two = encrypted_text[k + 1]
one_pos = ''.join(top_right).find((one).lower())
two_pos = ''.join(bottom_left).find((two).lower())
one_col = one_pos % 5
if one_col == 0 or one_col == 5:
one_row = one_pos / 5
else:
one_row = (one_pos / 5)
two_col = two_pos % 5
if two_col == 0 or two_col == 5:
two_row = two_pos / 5
else:
two_row = (two_pos / 5)
first_out_char = new_alphabet_rows[one_row][two_col]
second_out_char = new_alphabet_rows[two_row][one_col]
original_message += first_out_char + second_out_char
k += 2
print (original_message).upper()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment