EXTRA - StackingFlowing/CTProgramming GitHub Wiki
If you found the lessons are too easy for you, try the harder version of encryption!!!
(You can copy the code at the bottom and paste it into your program and start playing with it, this might give you a better knowledge of how the program works. When you are ready, create a new file and start.)
E.0 Preparation
You can create a new file in your project for this course.
In your file, write:
import random
As the name suggests, we will be doing something with randomisation.
E.1 RANDOM
Encryption
Before starting, look at the code first.
pickDictionary == "random":
complexDictionary = []
for letter in "`-=~_+ABCDEFGHIJKLMNOPQRSTUVWXYZ!@#$%^&*()[]\\{}|;':\",./<>?abcdefghijklmnopqrstuvwxyz1234567890":
complexDictionary.append(letter)
random.shuffle(complexDictionary)
for letter in complexDictionary: # Note that double quote mark is used.
dictionary += letter
Random encryption: Same encryption method, but different dictionary.
The dictionary will be different from the other kinds of encryption method:
- The dictionary used for encryption will be the
complexDictionary
- The program random shuffles the letters to create the dictionary
- The letters after shuffling will be stored as the
complexDictionary
- The program then uses this dictionary to encrypt the message
For example:
- I want to encrypt the message "Douglas" using random dictionary
- The computer randomly generated a dictionary
=.&mT)2Lpj|A]lN\<'Yf-bOiB{hsvc(Qqn9a[XtV3:k+7U0w>MP@_ey}Z5d?6/o,~DHG*%z;1IFC!JW^r8S$Eu#K4gRx
by shuffling the letters-=~_+ABCDEFGHIJKLMNOPQRSTUVWXYZ!@#$%^&*()[]\\{}|;':\",./<>?abcdefghijklmnopqrstuvwxyz1234567890
- The message after encrypting = "H;mLiUX"
Now =.&mT)2Lpj|A]lN\<'Yf-bOiB{hsvc(Qqn9a[XtV3:k+7U0w>MP@_ey}Z5d?6/o,~DHG*%z;1IFC!JW^r8S$Eu#K4gRx
will become the dictionary, and "H;mLiUX" represents the message "Douglas".
Decryption
To decrypt the message, we need to type in the dictionary =.&mT)2Lpj|A]lN\<'Yf-bOiB{hsvc(Qqn9a[XtV3:k+7U0w>MP@_ey}Z5d?6/o,~DHG*%z;1IFC!JW^r8S$Eu#K4gRx
so that the computer knows which dictionary to use to decrypt the message.
Now the code should be like this:
import random
dictionary = ""
key = 1
encrypting = ""
while encrypting != "e" and encrypting != "d":
encrypting = input("Would you like to encrypt or decrypt? ( e / d ) ")
message = input("Enter your message: ")
newMessage = ""
complexDictionary = []
for letter in "`-=~_+ABCDEFGHIJKLMNOPQRSTUVWXYZ!@#$%^&*()[]\\{}|;':\",./<>?abcdefghijklmnopqrstuvwxyz1234567890":
complexDictionary.append(letter)
random.shuffle(complexDictionary)
for letter in complexDictionary: # Note that double quote mark is used.
dictionary += letter
for character in message:
position = dictionary.find(character)
if position == -1:
newMessage += character
else:
if encrypting == "e":
newPosition = (position + key) % len(dictionary)
elif encrypting == "d":
newPosition = (position - key) % len(dictionary)
newMessage += dictionary[newPosition]
print(newMessage)
print()
print("The dictionary used is printed in the following line.")
print(dictionary)
E.2 Final program
import random
pickDictionary = ""
key = 0
encrypting = ""
dictionary = ""
while encrypting != "e" and encrypting != "d":
encrypting = input("Would you like to encrypt or decrypt? ( e / d ) ")
while True:
try:
key = int(input("Please enter a valid password. It should be a positive integer: ")) # tbh any integer will do.
if 1 <= key:
break
else:
print("Invalid response. Your number is not positive.")
except ValueError:
print("Invalid response. Value must be integer")
while pickDictionary != "letter" and pickDictionary != "advanced" and pickDictionary != "random" \
and pickDictionary != "custom":
pickDictionary = input("Which dictionary would you like to use? ( letter / advanced / random / custom ) ")
if pickDictionary == "letter":
dictionary = "abcdefghijklmnopqrstuvwxyz"
elif pickDictionary == "advanced":
dictionary = "`-=~_+ABCDEFGHIJKLMNOPQRSTUVWXYZ!@#$%^&*()[]\\{}|;':\",./<>?abcdefghijklmnopqrstuvwxyz1234567890"
elif pickDictionary == "random":
complexDictionary = []
for letter in "`-=~_+ABCDEFGHIJKLMNOPQRSTUVWXYZ!@#$%^&*()[]\\{}|;':\",./<>?abcdefghijklmnopqrstuvwxyz1234567890":
complexDictionary.append(letter)
random.shuffle(complexDictionary)
for letter in complexDictionary: # Note that double quote mark is used.
dictionary += letter
elif pickDictionary == "custom":
dictionary = input("Paste your dictionary here: ")
message = input("Enter your message: ")
newMessage = ""
for character in message:
position = dictionary.find(character)
if position == -1:
newMessage += character
else:
if encrypting == "e":
newPosition = (position + key) % len(dictionary)
elif encrypting == "d":
newPosition = (position - key) % len(dictionary)
newMessage += dictionary[newPosition]
print()
print(newMessage)
if pickDictionary == "random":
print("The dictionary used is printed in the following line.")
print(dictionary)
print("Please be advised that the dictionary is defined as a string with a double quote mark.")
This is the final encryption program, it allows the user to choose to encrypt or decrypt, there will be four ways to encrypt a message, the user can choose how to encrypt the message before the encryption starts.
Read through the code, try to understand every line and how it works.
You can copy paste it into your program and start playing with it, this might give you a better knowledge of how the program works.