Download as doc, pdf, or txt
Download as doc, pdf, or txt
You are on page 1of 45

Topic DECRYPTION

Class Understanding the importance of decryption and applying the


Description same into digital communications is the key to user privacy.
Following the philosophy kids build a cross platform
application software for communication.
Class ADV-C187

Class time 55 mins


Goal ● Create an application to get the encrypted data from the
firebase database, decrypt it and display it on the root
window.

Resources ● Teacher Resources


Required 0 Use gmail login credentials
○ Earphone with mic
○ Notepad and Pen

● Student Resources
0 Use gmail login credentials
○ Earphone with mic (optional)
○ Notepad and Pen
Class structure Warm Up 5 Mins
Teacher-Led Activity 15 Mins
Student-Led Activity 35 Mins
Wrap Up 5 Mins
Start the video call from H2H

Class Steps Say Do


© 2020 - WhiteHat Education Technology Private Limited.
Note: This document is the original copyright of WhiteHat Education Technology Private
Limited.

Please don't share, download or copy this file without permission.

1
Step 1: In the previous class we learnt how to Open Teacher-Activity-3-
Warm up encrypt the data and store the and download the
(5 mins)
data in the firebase database. class_187_Complete_enc
rytion_decryption.py file
and run on the spyder
before class to check
everything is working fine

Please follow the follow


of the class -

- Explain the student


Q - What is the function to send the everything as per the
document
message into the firebase
database? A - put() is used to
- Let the student
send the data into the firebase
start coding to build a
database. encryption and
put() function takes 3 parameter decryption application

Path - directory inside which the data


is stored
Key - key using which we can access
the message

Please don't share, download or copy this file without permission.

2
Q - What is the code to close a tkinter
window which is stored in the
root variable?
© 2020 - WhiteHat Education Technology Private Limited.
Note: This document is the original copyright of WhiteHat Education Technology Private
Limited.

A - root.destory()
As root is the variable holding the
window and destroy() function closes
the window

Now that you have answered the


questions and are clear with the
topics which we learnt in last class,
we will move to our next topic.

Teacher Initiates Screen Share

Please don't share, download or copy this file without permission.

3
Step 2: In today’s class we will learn how to
Teacher-Led get encrypted data from the firebase,
Activity decrypt it and display it on the
(15 mins) message window.

Till now we have created a chat application where we have encrypted the user
message and store it in the database.
In today class we will get the message from firebase database , decrypt the message
and display it to the user
© 2020 - WhiteHat Education Technology Private Limited.
Note: This document is the original copyright of WhiteHat Education Technology Private
Limited.

Please don't share, download or copy this file without permission.

4
The encryption and decryption is used to maintain the privacy or secrecy of a message.

​TEACHER-ACTIVITY-1
NOTE - ALL THE BELOW IMAGES ARE PRESENT IN ​.
​TEACHER-ACTIVITY-1
PLEASE OPEN ​ WHILE EXPLAINING

Let me show you the demonstration of this application:

This is the start screen or login screen of the application


Which contains the username , your code and your friends code
- Username will we your or person name sending messages from this end
© 2020 - WhiteHat Education Technology Private Limited.

Please don't share, download or copy this file without permission.

5
Note: This document is the original copyright of WhiteHat Education Technology Private Limited.

- Before the chat begins the person who will be having an encrypted conversation
should exchange each other's codes.
The data in firebase is stored in key - value format.
Therefore these codes will be the key and the message you send will be the value.
Therefore to access the message sent by the your friend we will require the key of your
friend who will be sending the messages
We will enter the key that I will be using to send data in “Your code” entry element and
the key your friend will be using to send the data in “Your Friends code” entry element.
This way we can get both our and our friends data from firebase database and display
on our root window

© 2020 - WhiteHat Education Technology Private Limited.


Note: This document is the original copyright of WhiteHat Education Technology Private Limited.

Please don't share, download or copy this file without permission.

6
​Start​”“ button
Enter the details and press
When clicked on​Start
“ ​” button the login screen will be closed and the message window
will be opened

© 2020 - WhiteHat Education Technology Private Limited.

Please don't share, download or copy this file without permission.

7
Note: This document is the original copyright of WhiteHat Education Technology Private Limited.

The textarea is to display the messages got from the firebase


And input field is to send the message which will be stored in the firebase

Please don't share, download or copy this file without permission.

8
© 2020 - WhiteHat Education Technology Private Limited.
Note: This document is the original copyright of WhiteHat Education Technology Private Limited.

​hello​. Therefore my
Here I had sent ​name​: ​hello​ is displayed on the textarea and my
friend (courteney) has sent​hi​” which
“ is also displayed in the textarea.
We are able to get our friends message because we had our friends​321 ​, that is
code
using which we got the message from the firebase database

Please don't share, download or copy this file without permission.

9
© 2020 - WhiteHat Education Technology Private Limited.
Note: This document is the original copyright of WhiteHat Education Technology Private Limited.
Data stored in firebase database

As you can see the data stored in the firebase database is not readable, that is the data
is stored in encrypted format. This keeps your data secured , even if the hacker tries to
access the database to get your messages he/she won’t be able to access the data as it
has been encrypted.
We all have used whatsApp , whatsapp does a similar thing, it encrypts the message
and stores it in their database to maintain the confidentiality of the messages sent.

In today's class we won’t be completing the entire application


We will just send the encrypted data which will be stored in the firebase database. The
application demo which we just saw is module one where we can send encoded
messages to firebase and get messages from firebase.
In the next module which is in upcoming classes we will also learn how to create a mini
version of whatsapp - where we send encoded messages from our python code and in
a thunkable mobile app receive the message, decode and show on the app.

NOTE - ALL THE BELOW IMAGES ARE PRESENT IN TEACHER-ACTIVITY-1 .


PLEASE OPEN TEACHER-ACTIVITY-1 WHILE EXPLAINING

Please don't share, download or copy this file without permission.

10
NOTE - ASK THE STUDENT TO CONTINUE CODING IN THE PREVIOUS CLASS
© 2020 - WhiteHat Education Technology Private Limited.
Note: This document is the original copyright of WhiteHat Education Technology Private Limited.

FILE ITSELF

The highlighted code was done in the previous class.

Please don't share, download or copy this file without permission.

11
© 2020 - WhiteHat Education Technology Private Limited.
Note: This document is the original copyright of WhiteHat Education Technology Private Limited.

Please don't share, download or copy this file without permission.

12
© 2020 - WhiteHat Education Technology Private Limited.
Note: This document is the original copyright of WhiteHat Education Technology Private Limited.

Complete code: The highlighted code will be done in today’s class

Please don't share, download or copy this file without permission.

13
© 2020 - WhiteHat Education Technology Private Limited.
Note: This document is the original copyright of WhiteHat Education Technology Private Limited.

Please don't share, download or copy this file without permission.

14
© 2020 - WhiteHat Education Technology Private Limited.
Note: This document is the original copyright of WhiteHat Education Technology Private Limited.

Please don't share, download or copy this file without permission.

15
© 2020 - WhiteHat Education Technology Private Limited.
Note: This document is the original copyright of WhiteHat Education Technology Private Limited.

Please don't share, download or copy this file without permission.

16
© 2020 - WhiteHat Education Technology Private Limited.
Note: This document is the original copyright of WhiteHat Education Technology Private Limited.

Please don't share, download or copy this file without permission.

17
© 2020 - WhiteHat Education Technology Private Limited.
Note: This document is the original copyright of WhiteHat Education Technology Private Limited.

1. Define​getData()
​ function. The functionality of this function is to get the
messages from firebase database , decrypt the message and show on root
window

Please don't share, download or copy this file without permission.

18
© 2020 - WhiteHat Education Technology Private Limited.
Note: This document is the original copyright of WhiteHat Education Technology Private Limited.

● Define​message_text ​ with a global keyword which is defined outside the


function. As this variable holds the textarea which we will be using to
display the messages got from the firebase database.

● Definel​ ast_value​with global keyword which is defined outside the


function

Please don't share, download or copy this file without permission.

19
© 2020 - WhiteHat Education Technology Private Limited.
Note: This document is the original copyright of WhiteHat Education Technology Private Limited.

​ our_code
● Definey ​ with global keyword which is defined outside the
function

● Define​your_friends_code
​ with global keyword which is defined outside
the function

● Get the data from the firebase database. To get the data from the firebase
database we will use​gthe et()​ function.​get()​ function is from the firebase
library that we have installed and imported in the previous class.
Syntax:
firebase.get(​path , key
​)
We use the module name( ​firebase ​) with dot operator ​g et()​ function
and
- Path​ - path or url from which we want to get the data from the
database. We will be getting the data directly from the root
directory(folder). Which is denoted by “/”. Therefore we will pass “/”
as a parameter.

Please don't share, download or copy this file without permission.

20
© 2020 - WhiteHat Education Technology Private Limited.
Note: This document is the original copyright of WhiteHat Education Technology Private Limited.

- Key​ - Using this key we can access the message stored inside the
firebase. As firebase stores data in key:value format.
- We had stored messages in the same format, like -
- your_code : message
- your_friends_code : message
- Where your_code will be our key, and your_friends_code
will be the key or your friend who is sending a message.
- Hence we will be using your_code key to get our message, and
your_friends_code key to get our friend's message.
- 1st we will get our data
Now why are we getting our data?
You have used whatsapp right, when we send a message your
message is also displayed on the screen right. Similarly to be able
to view the entire conversation we need to display our message
and our friends' messages as well.
So first we will get the message we have just sent. To get the
message we have sent we will need our code, the code we used to
send the data is stored ​your_code
in ​variable
Therefore here the key will ​your
be code ​ that you have entered on
the login window which is stored ​your_code
in ​variable.

Please don't share, download or copy this file without permission.

21
© 2020 - WhiteHat Education Technology Private Limited.
Note: This document is the original copyright of WhiteHat Education Technology Private Limited.

This will give the encrypted hexadecimal string from firebase database
and we will store​gin ​variable
et_your_data

​ et_your_data
● Printg ​variable which holds encrypted hexadecimal string
from the firebase database on console.

Q -​Do you remember ​encrypt()


​ function gave the encrypted data in
which format?
A - ​encrypt()
​ function gave the encrypted data in bytes

Q -​We have stored the encrypted data in which form in the firebase
database?
A - ​We have stored the data in firebase database in encrypted format

As we get the message in encrypted hexadecimal format we need to


decrypt it so that it's readable by humans, hence we will be using the
​ function.
decrypt()

Please don't share, download or copy this file without permission.

22
© 2020 - WhiteHat Education Technology Private Limited.
Note: This document is the original copyright of WhiteHat Education Technology Private Limited.
The decrypt() function takes the input in bytes so that it can
convert the encrypted message and gives the original message in
bytes. But as we know the message coming from firebase is in
hexadecimal format.
● Hence we need to first convert this hexadecimal into bytes format.
Q - Do you remember which function is used to convert hexadecimal
string to a bytes string?
A - bytes.fromhex() function is used to convert hexadecimal string to
bytes string.

And we will pass the hexadecimal string to bytes.fromhex() function and


store it in byte_str variable.

this will convert


into hexadecimal into bytes.
● Now that we have our encrypted string in bytes we will decrypt the string
to get original string
Q - Which function is used to decrypt the data?

A - decrypt() function is used to decrypt the data.

Q - What parameters are to be passed to decrypt() function ?

Please don't share, download or copy this file without permission.

23
A - decrypt() function takes 2 parameters:

- Key - This key should be the same key that is used to encrypt the
data. We used string “AIM” as our key to encrypt the data ,
therefore we will use the same string as a key to decrypt the data.
Always use the same key to encrypt and decrypt the data. If you
© 2020 - WhiteHat Education Technology Private Limited.
Note: This document is the original copyright of WhiteHat Education Technology Private Limited.

Please don't share, download or copy this file without permission.

24
ever used one key for encryption and another key for decryption
you won’t be able to get the same data. This makes the encryption
more strong.
- Message​ - The text which you would like to decrypt. The text that
we want to decrypt is stored​byte_str
in a ​variable.

And store it in variable ​original .


The​decrypt() ​ function gives the decrypted data but in bytes. Therefore
variable​original ​ will have decrypted data in bytes like
​b’ this
decrypted
-
data’
● Print the decrypted data which is stored in variable original on console.
We will concatenate variable original to string “Original data : “

● Now to convert the string from bytes to get the data in plain text that is
​ b and ‘ single quote from the original
remove the characters ​, wedata

© 2020 - WhiteHat Education Technology Private Limited.

Please don't share, download or copy this file without permission.

25
Note: This document is the original copyright of WhiteHat Education Technology Private Limited.

will decode it ​utotf-8(unicode transformation format)


​using the

decode()​ function. Like this and store it in


​ final_message
the variable

● Print the decoded message stored in ​fvariable ​ on console.


inal_message

● Now as we have the proper plain text message ready we will be


displaying it in the text area. Therefore we will use the insert() function to
add the text message in the textarea. This function takes 2 parameters

Please don't share, download or copy this file without permission.

26
© 2020 - WhiteHat Education Technology Private Limited.
Note: This document is the original copyright of WhiteHat Education Technology Private Limited.

- start_index ​- We would like to add the text at the end of any string
that is at the end of the existing data in textarea so give END where
END means the last character or index in the text area
- text​- The text that is to be added or displayed in the text area. We
want to display the decrypted text which is stored in variable
final_message.
Variable​ final_message ​holds the decrypted message. And then
we will concatenate “\n” string. “\n” means a new line. And we are
adding a new line so that the next message that is sent by our
friend should be added on the next line and not the same line.

● Now we have to get the message sent by another person, that is our
friend and display it in the text area.
Q -​Which function is used to get the data from the firebase database?
A -​To get the data from the firebase database we will ​get()use the
function from the firebase library that we have installed and imported.
Syntax:
​path , key
firebase.get( ​)
We use the module name( ​firebase
​) with dot operator and
​get()​ function

Please don't share, download or copy this file without permission.

27
© 2020 - WhiteHat Education Technology Private Limited.
Note: This document is the original copyright of WhiteHat Education Technology Private Limited.

- Path​ - path or url from which we want to get the data from the
database. We will be getting the data directly from the root
directory(folder). Which is denoted by “/”. Therefore we will pass “/”
as a parameter.

- Key​ - Using this key we can access the message stored inside the
firebase. As we had stored our key in your_code and your friends
key in your_friends_code we will be using those codes to get our
message and your friend’s messages from the firebase database
We have already written code to get our data now we want to get
the message sent by your friend. To get the message sent by your
friend we have to use the your friends code which is stored in
your_friends_code ​variable
Therefore here the key will ​your_friends_code
be ​that you have
entered on the login window which is ​ystored
our_friends_code
in
variable.

This will give the encrypted hexadecimal string from the firebase database
of the message sent by our friend which is​gstored
et_friends_data
in
variable.

Please don't share, download or copy this file without permission.

28
© 2020 - WhiteHat Education Technology Private Limited.
Note: This document is the original copyright of WhiteHat Education Technology Private Limited.

● Now when getting the data sent by our friend we are not sure if our friend
has actually sent any new message or not. And if our friend hasn’t sent
any message at all then ​get_friends_data ​variable will be empty. As we
get the data sent by our friend, but as there is no message the
get_friends_data ​variable will say​None
“ ​” that is there was no message.
And in the assumption that this variable holds encrypted hexadecimal
value and if we try to decrypt it will give an error. So to avoid this error, we
will first check​get_friends_data
if ​variable is not “None” using the if
statement. And​get_friends_data
if ​variable is not “None” means it
contains the encrypted hexadecimal message sent by our friend so we
can go ahead and decrypt.

Please don't share, download or copy this file without permission.

29
© 2020 - WhiteHat Education Technology Private Limited.
Note: This document is the original copyright of WhiteHat Education Technology Private Limited.

!=​ stands for not equal to , as not in programming is represented by an


exclamation mark.

○ Now Inside this if statement we will write code to decrypt the


​get_friends_data
message. Print ​variable which holds data from
the firebase database on console.

Please don't share, download or copy this file without permission.

30
© 2020 - WhiteHat Education Technology Private Limited.
Note: This document is the original copyright of WhiteHat Education Technology Private Limited.

○ Q -​Which function is used to convert hexadecimal string to bytes?


A -​​bytes.fromhex()
​ function is used to convert hexadecimal string
to bytes string.
And we will pass the hexadecimal string ​bytes.fromhex()
to
function.and store it​bin
yte_str​variable.

Please don't share, download or copy this file without permission.

31
© 2020 - WhiteHat Education Technology Private Limited.
Note: This document is the original copyright of WhiteHat Education Technology Private Limited.

○ Now that we have our encrypted string in bytes we will decrypt the
string to get the original string ​dusing
ecrypt()​function.​decrypt()
function takes two parameters
Q -​What are the 2 parameters​dthat ​function takes?
ecrypt()
A-
- Key​- This key should be the same key that is used to
encrypt the data. We used string “AIM” as our key to encrypt
the data , therefore we will use the same string as a key to
decrypt the data. Remember to always use the same key to
encrypt and decrypt the data. If you ever used one key for
encryption and another key for decryption you won’t be able
to get the same data. This makes the encryption more
strong.

Please don't share, download or copy this file without permission.

32
© 2020 - WhiteHat Education Technology Private Limited.
Note: This document is the original copyright of WhiteHat Education Technology Private Limited.

- Message ​ - The text which you would like to decrypt. The


​byte_str
text that we want to decrypt is stored in a ​variable.

​original .
And store it in variable
​ ​
Thedecrypt()function gives the decrypted data but in
bytes. Therefore variable original will have decrypted data in
bytes like this - b’ decrypted data’

○ So to convert the string from bytes to get the data in plain text that
​ b and ‘ single quote
is remove the characters ​from the original
data, we will decode ​uit tf-8(unicode
to transformation format)
using the​decode()
​ function. Like this

Please don't share, download or copy this file without permission.

33
© 2020 - WhiteHat Education Technology Private Limited.
Note: This document is the original copyright of WhiteHat Education Technology Private Limited.

and store it in the variable


final_message

○ Now that we have got our friend's message before displaying it on


the textarea we should check if the decrypted message is a new
message or old message.
As you know every time you save new data in the database. It does
not create a new entry in the database instead it replaces the old
message with the new message. That is why whenever you try to
get the message from the database you will only get the recent
message and not all the messages. Therefore we check the

Please don't share, download or copy this file without permission.

34
© 2020 - WhiteHat Education Technology Private Limited.
Note: This document is the original copyright of WhiteHat Education Technology Private Limited.

message whether it is an old message which we have already


displayed on the textarea or the message is changed.
We had created the ​last_value​variable to hold the previous
message.
Therefore we will check if​last_value
the ​variable is not the same
as the new message which is stored ​final_message
in ​. And if it is
not the same only then we will display the message in the text
area. To check we will use if statement inside if statement we will
check if​last_value
​variable is not the same​final_message
as
using​not in​ keyword

This will check if it is a new message and not an old one.

Please don't share, download or copy this file without permission.

35
© 2020 - WhiteHat Education Technology Private Limited.
Note: This document is the original copyright of WhiteHat Education Technology Private Limited.

■ Print the decoded message stored in variable final_message


on console

■ Now as we have the proper plain text message ready we will


be displaying it in the text area. Therefore we will use the
insert() function to add the text message in the textarea.
This function takes 2 parameters
- start_index ​- We would like to add the text at the end
of any string that is at the end of the existing data in
textarea so give END where END means the last
character or index in the text area

Please don't share, download or copy this file without permission.

36
© 2020 - WhiteHat Education Technology Private Limited.
Note: This document is the original copyright of WhiteHat Education Technology Private Limited.

- text​- The text that is to be added or displayed in the


text area. We want to display the decrypted text
​final_message.
which is stored in variable ​Variable
final_message ​holds the decrypted message. And
then we will concatenate “\n” string. “\n” means a new
line. And we are adding a new line so that the next
message that is sent by our friend should be added
on the next line and not the same line.

■ Then we will update the last_value variable with the


final_message. That is store the new message in the

Please don't share, download or copy this file without permission.

37
© 2020 - WhiteHat Education Technology Private Limited.
Note: This document is the original copyright of WhiteHat Education Technology Private Limited.

last_value variable. So that next time it will be checked


against this one

Please don't share, download or copy this file without permission.

38
© 2020 - WhiteHat Education Technology Private Limited.
Note: This document is the original copyright of WhiteHat Education Technology Private Limited.

2. This getData() function should be called everytime we send new data. Therefore
we will call getData() function inside the sendData() function

Teacher Stops Screen Share

Now it is your turn.

● Ask Student to press ESC key to come back to panel


● Guide Student to start Screen Share
● Teacher gets into Fullscreen
Step 3: Ask the student to start coding in
Student-Led the same file that was created in
Activity the last class
(10 mins)

Now, it's your turn. Show me how you Student-Activity-1- CODE


will create this application. DIAGRAM

Please don't share, download or copy this file without permission.

39
© 2020 - WhiteHat Education Technology Private Limited.
Note: This document is the original copyright of WhiteHat Education Technology Private Limited.

Please don't share, download or copy this file without permission.

40
After you have completed coding,
start screen recording and record
the output using loom and submit After the student has
the video link on the student panel. completed coding, guide
the student to start
screen recording and
record the output using
loom as per
Teacher-Activity-Referen
ce-1, and submit the
video link on the student
panel.

Teacher Guides Student to Stop Screen Share

Step 4: You did great today as well.


Wrap-Up Great! You have two hats off.
(3 mins) (Give at least 2 hats off)
Press the Hats Off Icon for
Creatively Solving
Activities
Q - What is the function to get the
message into the firebase database?
A - get() is used to get the data
into the firebase database. Press the Hats Off Icon for
Great Question
get() function takes 2 parameter
Path - directory inside which the data
is stored
Key - key using which we can access
the message Press the Hats Off Icon for
“Strong Concentration.. ”

© 2020 - WhiteHat Education Technology Private Limited.


Please don't share, download or copy this file without permission.

41
Note: This document is the original copyright of WhiteHat Education Technology Private Limited.

Q - What is the condition to check if

get_friends_data variable is

not equal to None?

A - get_friends_data != None

If you don’t have time


to perform additional
activities, ask the
student to perform all
the additional
activities after class.
Additional activities are
VERY important for
kids, so that they are
ready for the next
module. And some
challenging concepts
are coming ahead.

Also remind the student


to refer to the Student
Please don't share, download or copy this file without permission.

42
Reference activity for
© 2020 - WhiteHat Education Technology Private Limited.
Note: This document is the original copyright of WhiteHat Education Technology Private Limited.
increasing their
knowledge. This should
also be done

For the solution of all the Additional Activity open Teacher-Activity-3 and
navigate to class number C187

Additional Activity 1 -
Run Student-Activity-2 from the panel
The TASK and HINTS are mentioned on the website itself.

Additional Activity 2 -
Run Student-Activity-3 from the panel
The TASK and HINTS are mentioned on the website itself.

Additional Activity 3 -
Run Student-Activity-4 from the panel
The TASK and HINTS are mentioned on the website itself.

Additional Activity 4 -
Run Student-Activity-5 from the panel
The TASK and HINTS are mentioned on the website itself.

Additional Activity 5 -
Run Student-Activity-6 from the panel
The TASK and HINTS are mentioned on the website itself.

Please don't share, download or copy this file without permission.

43
Teacher Clicks

Activity Activity Name Links


© 2020 - WhiteHat Education Technology Private Limited.
Note: This document is the original copyright of WhiteHat Education Technology Private Limited.

Please don't share, download or copy this file without permission.

44
CODE https://curriculum.whitehatjr.com/ADV+Ass
Teacher Activity 1 DIAGRAM et/CODE+DAIGRAM_C187.pdf

Teacher Activity 2 SOURCE CODE https://curriculum.whitehatjr.com/ADV+Ass


et/ADV-C186+-+C187-
20201229T145522Z
-001.zip
Teacher Activity 3 ADDITIONAL https://mahdihat791.github.io/v2/additional
ACTIVITIES _activities_solution.html
SOLUTIONS

Teacher Reference RECORD SCREEN https://curriculum.whitehatjr.com/ADV+Ass


Activity 1 REFERENCE et/C145-+Guide+to+loom+recording.pdf

Student Activity 1 CODE DIAGRAM https://curriculum.whitehatjr.com/ADV+Ass


et/CODE+DAIGRAM_C187.pdf

Student Reference RECORD SCREEN https://curriculum.whitehatjr.com/ADV+Ass


Activity 1 REFERENCE et/C145-+Guide+to+loom+recording.pdf

Student Reference SIMPLE CRYPT https://pypi.org/project/simple-crypt/


Activity 2 LIBRARY

Student Reference FIREBASE PYTHON https://pypi.org/project/python-firebase/


Activity 3

© 2020 - WhiteHat Education Technology Private Limited.


Note: This document is the original copyright of WhiteHat Education Technology Private Limited.

Please don't share, download or copy this file without permission.

45

You might also like