ADFGVX Cipher

Coming Soon

#-------------------------------------------------------------------------------
# Name:        ADFGVX Cipher
# Purpose:
#
# Author:      James Woolley
#
# Created:     17/07/2012
# Copyright:   (c) James 2012
# Licence:     Open Source
#-------------------------------------------------------------------------------
###Creates the base Alphabet which is used for finding preceeding characters from the ciphertext.
##baseAlphabet = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z']
adfgvx = ['a', 'd', 'f', 'g', 'v', 'x']#List created to assign numerical values to each cipher letter
print ("Welcome to a ADFGVX encrypter. You will first be asked to enter the characters which will become the polybius square used in the encryption process.")
textToEncode = ['a', 't', 't', 'a', 'c', 'k']
##polybiusSquare = raw_input("Please enter the characters in the order you would like them to appear in the polybius square.")
polybiusSquare = [['p', 'h', '0', 'q', 'g', '6'], ['4', 'm', 'e', 'a', '1', 'y'], ['l', '2', 'n', 'o', 'f', 'd'], ['x', 'k', 'r', '3', 'c', 'v'], ['s', '5', 'z', 'w', '7', 'b'], ['j', '9', 'u', 't', 'i', '8'], ]#The polybius Square stored as nested lists
finalSubstitution = []#Used to store the final substituted letters before being fractionated.
substitutionPart = []#Used to store the ciphertext after substitution.
key = ['g', 'e', 'r', 'm', 'a', 'n']
fractionating = []


incrementCounter = 0#used to increment through each list within the polybiusSquare list
for char in textToEncode:
    while incrementCounter < 6:#MABY delete somhow future James
        if polybiusSquare[incrementCounter].count(char) > 0:#if the current incremented list contains the the char to be encoded it finds its substituted ciphertext value (either ADFGVX) and appends it to the substitutionPart list
            substitutionPart.append(adfgvx[incrementCounter])
            substitutionPart.append(adfgvx[polybiusSquare[incrementCounter].index(char)])
            incrementCounter = 0
            break
        else:
            incrementCounter = incrementCounter + 1#executed if the current incremented list does not contain the char so it moves onto the next list.
            continue

splitValue = len(key)#used to indicate where the substituted values wrap round.
incrementCounter = 0
for char in substitutionPart:#used to generate the finalSubstitution list where each letter pair is assigned to the key letter.
    if incrementCounter < splitValue:
        finalSubstitution.append([key[incrementCounter],char, substitutionPart[incrementCounter + splitValue]])
        incrementCounter = incrementCounter + 1
finalSubstitution.sort()#Sorts the substituted values by the key(the first element.)

cipherTextList = []
incrementCounter = 0
for char in finalSubstitution:#Appends the letter pairs in order of the key to a list called CipherTextList
    cipherTextList.append(char[1])
    cipherTextList.append(char[2])

cipherText = ''.join(cipherTextList)#Converts the cipherTextList to a String so it can be outputted.

print cipherText

Leave a Reply

Your email address will not be published. Required fields are marked *