Warning - This site is moving to https://getthecodingbug.anvil.app
Topics covered
Encryption and Decryption
History
Caesar cipher
Vigenère cipher
Encryption and Decryption
Encryption is a method which allows information (for example, a secret message) to be hidden so that it cannot be read without special knowledge (such as a password). Once this is done, using a secret code or cipher, the information is encrypted.
Decryption is a way to change an encrypted piece of information back into un-encrypted form. This is called the decrypted form. The study of encryption is called cryptography.
History
Historically, cryptography methods primarily involved the use of pen and paper encryption or simple mechanical aids. For example, clay tablets found in Mesopotamia dating from 1500 BC had an encrypted recipe for pottery glaze. And Hebrew scholars were using substitution ciphers as far back as 500 or 600 BC.
Caesar cipher
In Roman times they used a Caesar cipher, named after Julius Caesar who used it to communicate with his army.
To make a message secret with the Caesar cipher, each letter in the message is changed using a simple rule:
Shift each letter a number of letters further up in the alphabet, wrapping around if necessary.
Here is a Python script with an encrypt function that shifts the letters 17 places in the alphabet.
Copy and paste this code into a new file and save it as 'caesarCipher.py'
Your challenge is to write the code in the decrypt function to reveal the original message in plain text.
# caesarCipher.py
alphabet = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ' def encrypt(message, shift): encryptedMessage = '' # Convert message to all upper-case letters message = message.upper() for letter in message: if not letter in alphabet: encryptedMessage = encryptedMessage + letter else: # find position of letter in alphabet index = alphabet.index(letter) # make an offset from this letter using the shift amount offset = index + shift if offset > 25: # if offset goes beyond end, start again at beginning offset = offset - 26 # use shifted letter in encrypted message encryptedMessage = encryptedMessage + alphabet[offset] # return encrypted message to caller return encryptedMessage def decrypt(encrypted, shift): decryptedMessage = '' # Challenge: write code for this function to do # the reverse of the encrypt function return decryptedMessage # The value of shift may be anywhere between 1 and 25 shift = 17 # shift must be the same in both the encrypt and decrypt functions message = "The quick brown fox jumps over the lazy dog." print('Original: ', message) encrypted = encrypt(message, shift) print('Encrypted:', encrypted) decrypted = decrypt(encrypted, shift) if decrypted == '': print('You have not yet completed the decrypt function') elif not decrypted == message.upper(): print('Your decryption is not correct:') print('Your Decrypted message:', decrypted) print('The expected result is:', message.upper()) else: print('Correctly Decrypted:', decrypted) print()
Vigenère cipher
The Caesar cipher was not too difficult to crack, however a Frenchman, Blaise de Vigenère in the 16th century, invented a method, known as a poly-alphabetic cipher because it uses two or more cipher alphabets to encrypt the data.
In other words, the letters in the Vigenère cipher are shifted by different amounts, normally done using a word or phrase as the encryption key.
This link gives a graphical explanation: http://www.counton.org/explorer/codebreaking/vigenere-cipher.php
Here is his method written in Python. It is not the most efficient form of code, it has just been written for ease of understanding. The key ('MYSECRETKEY') is repeated up to the length of the message to be encrypted.
When you understand how it works, try making a function to decrypt the message.
# vigenereCipher.py
table = [ ' ABCDEFGHIJKLMNOPQRSTUVWXYZ', 'AABCDEFGHIJKLMNOPQRSTUVWXYZ', 'BBCDEFGHIJKLMNOPQRSTUVWXYZA', 'CCDEFGHIJKLMNOPQRSTUVWXYZAB', 'DDEFGHIJKLMNOPQRSTUVWXYZABC', 'EEFGHIJKLMNOPQRSTUVWXYZABCD', 'FFGHIJKLMNOPQRSTUVWXYZABCDE', 'GGHIJKLMNOPQRSTUVWXYZABCDEF', 'HHIJKLMNOPQRSTUVWXYZABCDEFG', 'IIJKLMNOPQRSTUVWXYZABCDEFGH', 'JJKLMNOPQRSTUVWXYZABCDEFGHI', 'KKLMNOPQRSTUVWXYZABCDEFGHIJ', 'LLMNOPQRSTUVWXYZABCDEFGHIJK', 'MMNOPQRSTUVWXYZABCDEFGHIJKL', 'NNOPQRSTUVWXYZABCDEFGHIJKLM', 'OOPQRSTUVWXYZABCDEFGHIJKLMN', 'PPQRSTUVWXYZABCDEFGHIJKLMNO', 'QQRSTUVWXYZABCDEFGHIJKLMNOP', 'RRSTUVWXYZABCDEFGHIJKLMNOPQ', 'SSTUVWXYZABCDEFGHIJKLMNOPQR', 'TTUVWXYZABCDEFGHIJKLMNOPQRS', 'UUVWXYZABCDEFGHIJKLMNOPQRST', 'VVWXYZABCDEFGHIJKLMNOPQRSTU', 'WWXYZABCDEFGHIJKLMNOPQRSTUV', 'XXYZABCDEFGHIJKLMNOPQRSTUVW', 'YYZABCDEFGHIJKLMNOPQRSTUVWX', 'ZZABCDEFGHIJKLMNOPQRSTUVWXY' ] def encryptLetter(letterToBeEncypted, key): for row in range(len(table)): # First find row which starts with key letter if table[row][0] == key: # Now find column that matches letter to be encrypted column = 0 for letter in table[0]: if letter == letterToBeEncypted: # Column found - return encrypted letter encrypted = table[row][column] return encrypted else: # Advance column index column += 1 print("'ERROR: Cannot find a match for: '" + letterToBeEncypted + "'") return(' ') plainText = 'THEGOLDISUNDERTHEBENCH' keyText = 'MYSECRETKEYMYSECRETKEY' cipherText = '' expected = 'FFWKQCHBCYLPCJXJVFXXGF' keyIndex = 0 for plainLetter in plainText: if len(keyText) < len(plainText): print('ERROR: keyText is not same length as plainText') break keyLetter = keyText[keyIndex] encryptedLetter = encryptLetter(plainLetter, keyLetter) cipherText += encryptedLetter keyIndex += 1 print('cipherText = \t', cipherText) print('expected = \t', expected)