Classic Encryption: Vigenère

def gen_list(start):
        li = [chr(i) for i in range(65, 91)]
        return li[start:len(li)] + li[0:start]

def gen_arr():
        arr = []
        for i in range(0, 26):
                arr.append(gen_list(i))
        return arr

def print_arr():
        arr = gen_arr()
        for i in arr:
                print i

def gen_key_repeat(key, length):
        key_len = len(key)
        return key * (length/key_len) + key[:(length%key_len)]

def encrypt_char(in_char, key_char):
        arr = gen_arr()
        x = arr[0].index(in_char)
        for i in range(0, len(arr)):
                if arr[i][0] == key_char:
                        y = i
                        break
        return arr[y][x]

def decrypt_char(en_char, index, key_char):
        arr = gen_arr()
        for i in range(0, len(arr)):
                if arr[i][0] == key_char:
                        y = i
                        break
        x = arr[y].index(en_char)
        return arr[0][x]

def decrypt(input, key):
        key_repeat =  gen_key_repeat(key, len(input))
        arr = gen_arr()
        decrypted = []
        for i in range(0, len(input)):
                decrypted.append(decrypt_char(input[i], i, key_repeat[i]))
        return decrypted

def encrypt(input, key):
        key_repeat =  gen_key_repeat(key, len(input))
        arr = gen_arr()
        encrypted = []
        for i in range(0, len(input)):
                encrypted.append(encrypt_char(input[i], key_repeat[i]))
        return encrypted

key="KEYWORD"
input = raw_input("Input: ")
input = input.replace(" ", "").upper()

print_arr()

encrypted = "".join(encrypt(input, key))
print "Encrypted: ", encrypted

decrypted = "".join(decrypt(encrypted, key))
print "Decrypted: ", decrypted

vig

Advertisements

Published by

Chandan Dutta Chowdhury

Software Engineer

One thought on “Classic Encryption: Vigenère”

  1. You post interesting posts here. Your blog deserves much
    more traffic. It can go viral if you give it initial boost, i know very useful tool that can help you, just type in google: svetsern traffic tips

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s