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

Classic Encryption: Four Square

Simple encryption based on classic four square


import random

chars = range(65,91)
chars.pop(16)

def make_array(input):
        arr = []
        li = []
        for i in range(0, len(input)):
                if i % 5 == 0 and i:
                        arr.append(li)
                        li = []
                li.append(chr(input[i]))
        arr.append(li)
        return arr

random.shuffle(chars)
b1 = make_array(chars)
random.shuffle(chars)
b2 = make_array(chars)
random.shuffle(chars)
b3 = make_array(chars)
random.shuffle(chars)
b4 = make_array(chars)

def tokenize(input):
        for i in range(0, len(input), 2):
                yield "%s%s" %(input[i], input[i+1])

def find(ch, block):
        for i in range(0, len(block)):
                for j in range(0, len(block[i])):
                        if ch == block[i][j]:
                                return i, j
def encrypt(input):
        first = find(input[0], b1)
        second = find(input[1], b4)
        return "%s%s" %(b2[first[0]][second[1]], b3[second[0]][first[1]])
        return first, second

def decrypt(input):
        first = find(input[0], b2)
        second = find(input[1], b3)
        return "%s%s" %(b1[first[0]][second[1]], b4[second[0]][first[1]])
        return first, second

def print_block():
        for i in range(0, len(b1)):
                print " ".join(b1[i]), "   ", " ".join(b2[i])

        print "\n"
        for i in range(0, len(b1)):
                print " ".join(b3[i]), "   ", " ".join(b4[i])

input = raw_input("Input: ")
input = input.upper()

if len(input) % 2 != 0:
        input = input[:-1]

print_block()

encrypted = "".join([encrypt(i) for i in tokenize(input)])
print "Encrypted: ", encrypted

decrypted = "".join([decrypt(i) for i in tokenize(encrypted)])
print "Decrypted: ", decrypted

fs3 fs2 fs1