Пример #1
0
    def encrypt(self):
        # Retrieve the plaintext and key to encrypt the text
        self.plaintext = str(util.enterPlainTextMessage()).lower()
        self.key = str(util.enterKey()).lower()

        # A list to temporarily store characters once encrypted
        encryptedText = list()

        # For every letter in the plaintext
        for letterIndex in range(len(self.plaintext)):
            letter = self.plaintext[letterIndex]  # The plaintext letter
            letterAlphaIndex = util.alphabet().index(
                letter)  # The alphabetic index of the encrypted letter
            keyLetter = self.key[letterIndex % len(
                self.key
            )]  # The character of the Key that corresponds to this letter
            keyAlphaIndex = util.alphabet().index(
                keyLetter)  # The corresponding Keys alphabetic index value

            encryptedCharacterIndex = letterAlphaIndex + keyAlphaIndex  # The encrypted character's index

            if encryptedCharacterIndex >= len(
                    util.alphabet()
            ):  # If the encypted character's index is greater than the length of the alphabet
                encryptedCharacterIndex = encryptedCharacterIndex % len(
                    util.alphabet()
                )  # Then we need to wrap around the alphabet
            encryptedText.append(
                util.alphabet()[encryptedCharacterIndex]
            )  # We now have the correct encrypted character
        util.regularMessage(
            str(self.plaintext).upper() + " has been encoded as " +
            "".join(encryptedText).upper() + " with the key: " +
            str(self.key).upper())
Пример #2
0
    def convert(self, letter, key):
        keyIndex = util.alphabet().index(str(key).lower())
        letterIndex = util.alphabet().index(str(letter).lower())

        value = letterIndex-keyIndex
        if(value < 0): value = len(util.alphabet()) - (keyIndex-letterIndex)
        return str(util.alphabet()[value]).upper()
Пример #3
0
    def decrypt(self):
        self.ciphertext = util.enterCipherText()
        self.key = 1

        while (self.key < len(util.alphabet())):
            decryptedLine = list()
            for letter in self.ciphertext.lower():
                if (letter != " "):
                    decryptedLine.append(
                        util.alphabet()[self.getNextIndex(letter)])
                else:
                    decryptedLine.append(" ")

                #Present the result
                spacer = ""
                if (self.key < 10): spacer = " "
            util.regularMessage("Key: " + str(self.key) + spacer +
                                "    DECRYPTION:  " + ''.join(decryptedLine))
            self.key += 1
Пример #4
0
    def decryptWithKey(self):
        """Decrypts a Vigenere Cipher where the encryption key is known """

        # Initialise the plaintext storage
        self.plaintext = ""

        # For every letter in the ciphertext
        for letterIndex in range(len(self.ciphertext)):
            letter = self.ciphertext[letterIndex]  # The encrypted letter
            letterAlphaIndex = util.alphabet().index(
                letter)  # The alphabetic index of the encrypted letter
            keyLetter = self.key[letterIndex % len(
                self.key
            )]  # The character of the Key that corresponds to this letter
            keyAlphaIndex = util.alphabet().index(
                keyLetter)  # The corresponding Keys alphabetic index value
            decryptedLetterIndex = letterAlphaIndex - keyAlphaIndex
            if decryptedLetterIndex < 0:  # Wraparound alphabet
                decryptedLetterIndex = (len(util.alphabet()) -
                                        (keyAlphaIndex - letterAlphaIndex))
            self.plaintext += util.alphabet()[decryptedLetterIndex]
        util.regularMessage("Message Decypted as: \n" +
                            str(self.plaintext).upper())
Пример #5
0
    def encrypt(self):
        self.key = util.enterKey()
        while (not self.validateKey()):
            self.key = util.enterKey()
        self.plaintext = util.enterPlainTextMessage()
        while (not self.validatePlainText()):
            self.plaintext = util.enterPlainTextMessage()
        self.originaltext = self.plaintext
        self.plaintext = str(self.plaintext).lower()

        self.ciphertext = ""
        for letter in self.plaintext:
            if (not letter == " "):
                self.ciphertext += util.alphabet()[self.getNextIndex(letter)]
            else:
                self.ciphertext += " "
        util.regularMessage("'" + str(self.originaltext).upper() +
                            "' has been encoded as: " +
                            str(self.ciphertext).upper() + "\nWith the Key: " +
                            str(self.key))
Пример #6
0
 def getAlphaNumeric(self, letter):
     return util.alphabet().index(letter) + 1
Пример #7
0
 def getNextIndex(self, letter):
     nextIndex = util.alphabet().index(letter) - int(self.key)
     if (nextIndex < 0):
         nextIndex = len(util.alphabet()) + (util.alphabet().index(letter) -
                                             int(self.key))
     return nextIndex