#!/usr/bin/env python
#Author: Shriphani Palakodety
#Mail: spalakod@purdue.edu
#Mail: shriphani@shriphani.com
#Blog: http://shriphani.com/blog

import euclid_gcd

import e2

import modular

import string

alpha_dict = {}

for char in string.lowercase:
        if str(string.lowercase.index(char)) == 2:
                alpha_dict[char] = str(string.lowercase.index(char))
        else:
                alpha_dict[char] = str("0"+str(string.lowercase.index(char)))
def groupWord(word):
        word_list = []
        for i in xrange(0, len(word), 2):
                word_list.append(word[i:i+2])
        return word_list

def translate(word_list):
        encrypt_list = []
        for word in word_list:
                num_str=""
                for char in word:
                        num_str+=alpha_dict[char]
                encrypt_list.append(int(num_str))
        return encrypt_list

def getTotient(p, q):
        return (p-1)*(q-1)

def encrypt(num_list, p, q):
        encr_list = []
        for num in num_list:
                encr_list.append(modular.modular(num, 13, p*q))
        return encr_list

def decrypt(crypt, n, public, totient):
        inverse = e2.extended(public, totient)[0]
        print inverse
        return modular.modular(crypt, inverse, n)

word = raw_input("Type the message you want to encrypt: ")
word_list = groupWord(word)
print "The words have been grouped into: ", word_list
new_list = translate(word_list)
print "The words expressed in a numeric format: ", new_list
encr_list = encrypt(new_list, 59, 43)
print "The encrypted list is: ", encr_list
decr_list = []
for encr in encr_list:
        decr_list.append(decrypt(encr, 59*43, 13, 58*42))
print "Upon decrypting the submitted words, we get: ", decr_list