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())
def decrypt(self): # Retrieves the ciphertext to be decrypted (and key if known) self.ciphertext = str(util.enterCipherText()).lower() self.keyCheck() if (self.keyKnown): self.decryptWithKey() else: util.regularMessage("Function not yet implemented")
def validatePlainText(self): if not util.isAlpha(self.plaintext): util.regularMessage( "\nCaesar cipher can only encode ASCII Text (A-Z) characters. Please enter a valid plaintext to encrypt" ) return False else: return True
def validateKey(self): if not (util.isNumeric(self.key) and int(self.key) > 0 and int(self.key) <= 26): util.regularMessage( "\nA key for Caeser Cipher can only be numeric from 1-26. Please enter a key that fits this criteria\n" ) return False else: return True
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
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))
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())