示例#1
0
def substitionKeyCipher(
        userCipherText, userKey
):  #maps a ciphertext to plaintext according to the key given to it
    cipherText = tx.convertToASCII(
        list(userCipherText))  #Converting cipher to numbers
    key = tx.convertToASCII(list(userKey))  #Converting key to numbers

    def switchChar(
        cipherChar
    ):  #Switches a single character from its chiphertext to its plaintext
        alphaPerm = newChar = 0
        while alphaPerm < 26:  #as it goes through a letter it changes it
            if cipherChar == key[alphaPerm]:
                newChar = alphabetASCII[alphaPerm]
            alphaPerm += 1
        return newChar

    textPerm = 0
    switchedCipher = []
    while textPerm < len(
            cipherText
    ):  #Goes through each character one by one and sends to the function which converts cipher to plain
        switchedCipher.append(switchChar(cipherText[textPerm]))
        textPerm += 1
    return "".join(tx.convertToCHARACTER(switchedCipher))
示例#2
0
def keyWordCeaser(
    index, shiftIndex
):  #Keyword key generator - filled in bit being the alphabet which is then shifted 26 times for the same keyword
    lenFreq = len(keyWords)
    if index > lenFreq - 1:
        index = index - ((index // lenFreq) * lenFreq)
    key = tx.convertToASCII(list(keyWords[index]))
    countIndex = 0
    while countIndex < 26:  #Removes duplicate letters any words may have
        howMany = key.count(alphabetASCII[countIndex])
        if howMany >= 2:
            where = key.index(alphabetASCII[countIndex])
            while howMany > 1:
                key.pop(where)
                where = key.index(alphabetASCII[countIndex])
                howMany -= 1
        countIndex += 1
    perms = 0
    repeat = False
    endKey = []
    while perms < 26:  #Ensures 26 letters in the alphabet
        newChar = alphabetASCII[perms]
        repeat = tx.search(newChar, key)  #Ensures each letter is unique
        if repeat == False:
            endKey.append(
                newChar
            )  # adds all the extra characters to a new list to be shifted later
        perms += 1
    shiftedEnd = tx.shiftRight(
        endKey, shiftIndex)  # shifts a list "shiftIndex" places to the right
    keyStart = "".join(tx.convertToCHARACTER(key))
    shiftedEnd = "".join(tx.convertToCHARACTER(shiftedEnd))
    return keyStart + shiftedEnd  # joins the two first keyword and the rest of the shifted string together to make an entire key
示例#3
0
def keyWordAlphabet(
    index
):  #Keyword key generator - filled in bit being the alphabet - keyed ceaser
    lenFreq = len(keyWords)
    if index > lenFreq - 1:
        index = index - ((index // lenFreq) * lenFreq)
    key = tx.convertToASCII(list(keyWords[index]))
    countIndex = 0
    while countIndex < 26:  #Removes duplicate letters any words may have
        howMany = key.count(alphabetASCII[countIndex])
        if howMany >= 2:
            where = key.index(alphabetASCII[countIndex])
            while howMany > 1:
                key.pop(where)
                where = key.index(alphabetASCII[countIndex])
                howMany -= 1
        countIndex += 1
    perms = 0
    repeat = False
    while perms < 26:  #Ensures 26 letters in the alphabet
        newChar = alphabetASCII[perms]
        repeat = tx.search(newChar, key)  #Ensures each letter is unique
        if repeat == False:
            key.append(newChar)
        perms += 1
    return "".join(tx.convertToCHARACTER(key))
示例#4
0
def indexOfCoincidence(
        text):  #String input to calculate the Index of Coincidence of a text
    def letter(letterCount, textLength):
        letterValue = (letterCount / textLength) * ((letterCount - 1) /
                                                    (textLength - 1))
        return letterValue

    IoCLIST = []
    alphaIndex = 0
    textList = tx.convertToASCII(list(text))
    textLength = len(textList)
    while alphaIndex < 26:
        letterCount = textList.count(alphaIndex + 97)
        IoCLISTStore = (letter(letterCount, textLength))
        IoCLIST.append(IoCLISTStore)
        alphaIndex += 1
    return sum(IoCLIST)
示例#5
0
def chiSquaredStat(
        text
):  #String input which calculates the chi-squared statistic of a text
    def letter(realLetterCount, expectedLetterCount):
        letterValue = (
            (realLetterCount - expectedLetterCount)**2) / expectedLetterCount
        return letterValue

    chiSquaredLIST = []
    alphaIndex = 0
    textList = tx.convertToASCII(list(text))
    textLength = len(textList)
    while alphaIndex < 26:
        realLetterCount = textList.count(alphaIndex + 97)
        expectedLetterCount = textLength * (
            englishLetterFrequency[alphaIndex]
        ) / 100  #Compares it against an english distribution probability
        chiSquaredLISTStore = letter(realLetterCount, expectedLetterCount)
        chiSquaredLIST.append(chiSquaredLISTStore)
        alphaIndex += 1
    return sum(chiSquaredLIST)
示例#6
0
def keyWordRandom(
        index):  #Keyword key generator - filled in bit being random characters
    lenFreq = len(keyWords)
    if index > lenFreq - 1:
        index = index - ((index // lenFreq) * lenFreq)
    key = tx.convertToASCII(list(keyWords[index]))
    countIndex = 0
    while countIndex < 26:  #Removes duplicate letters any words may have
        howMany = key.count(alphabetASCII[countIndex])
        if howMany >= 2:
            where = key.index(alphabetASCII[countIndex])
            while howMany > 1:
                key.pop(where)
                where = key.index(alphabetASCII[countIndex])
                howMany -= 1
        countIndex += 1
    perms = len(key)
    while perms < 26:  #Ensures 26 letters in the alphabet
        randomNo = random.randint(97, 122)
        repeat = tx.search(randomNo, key)  #Ensures each letter is unique
        if repeat == False:
            key.append(randomNo)
            perms += 1
    return "".join(tx.convertToCHARACTER(key))