block cipher q15 school use only
blockcipher_q15_schooluseonly.py
# block_cipher (q15 of SCHOOL USE ONLY mock exam) message_to_send = "DigitalSolutions" ########################################## # 1. create (random) block encryption key: import random key_letters = [] key_numbers = [] #to create a random key: for counter in range(len(message_to_send)): number = random.randint(1,26) key_numbers.append(number) print("Random key as numbers:", key_numbers) #... or to use the keys from mock exam, calculated from the number of #character ordinals shifted from the decrypted to the encrypted msg: key_numbers = [4,4,4,9,4,3,8,2,6,11,14,3,1,1,8,5] #from exam question for index, number in enumerate(key_numbers): if message_to_send[index].isupper(): #deal with case key_letters.append(chr( (ord("A")-1) + number)) else: key_letters.append(chr( (ord("a")-1) + number)) print("Exam key as numbers:", key_numbers) print("Key as letters:", key_letters) #not necessary, but easily found ######################################## # 2. encrypt text: encrypted_letters = [] for index, letter in enumerate(message_to_send): if letter.isupper(): temp = ( ord(letter) - ord("A") + key_numbers[index] ) % 26 encrypted_letters.append( chr( temp + ord("A") ) ) else: temp = ( ord(letter) - ord("a") + key_numbers[index] ) % 26 encrypted_letters.append( chr( temp + ord("a") ) ) cipher_text = "".join(encrypted_letters) print("Ciphertext:", cipher_text) ######################################## # 3. decrypt text: decrypted_letters = [] for index, letter in enumerate(list(cipher_text)): if letter.isupper(): temp = ( ord(letter) - ord("A") - key_numbers[index] ) % 26 decrypted_letters.append( chr( temp + ord("A") ) ) else: temp = ( ord(letter) - ord("a") - key_numbers[index] ) % 26 decrypted_letters.append( chr( temp + ord("a") ) ) decrypted_message = "".join(decrypted_letters) print("Plaintext:", decrypted_message) ######################################## # Strengths - # 1) there is an almost infinite number of possible keys because # of the randomness of each selected shift # 2) there is no proven method to crack this method since it is # based on randomness # # Weaknesses - # 3) it is symmetric in nature and therefore the sender and # receiver must have a copy of the key # 4) the key has to be the exact same length or more of the message