Coding Encrypted Chat in Python
Summary
TLDRIn this tutorial video, the host guides viewers through the process of creating an encrypted chat application in Python. Utilizing the RSA library for encryption and core Python modules like socket and threading, the video demonstrates how to establish a secure communication channel that prevents eavesdropping on network traffic. The host also illustrates the effectiveness of encryption using Wireshark to compare clear text and encrypted message transmission, emphasizing the importance of keeping private keys secure for maintaining message confidentiality.
Takeaways
- 😀 The video is a tutorial on creating an encrypted chat in Python.
- 🔒 It emphasizes the importance of encryption for secure communication, preventing third-party eavesdropping.
- 📚 The tutorial uses the RSA library for encryption, which is an external module in addition to Python's core modules like socket and threading.
- 🔑 The concept of asymmetric encryption is introduced, explaining the use of a public key for encryption and a private key for decryption.
- 🔄 The video demonstrates creating a public-private key pair at the start of the session, which is not typical for long-term use cases.
- 💼 The public key can be shared openly, while the private key must remain confidential to ensure message security.
- 🔗 The script involves a client-server model with a choice for the user to either host or connect to a chat session.
- 🤖 The script uses threading to handle both sending and receiving messages simultaneously in separate threads.
- 📡 The tutorial includes a practical demonstration of network traffic using Wireshark to contrast encrypted versus unencrypted messages.
- 🛑 The video shows that without encryption, messages can be easily read by sniffing network traffic, but with encryption, the content is secure.
- 🎓 The tutorial concludes with a demonstration of the encrypted chat in action and a comparison using Wireshark, highlighting the security benefits of encryption.
Q & A
What is the main topic of the video?
-The video is about building an encrypted chat application in Python.
Why is encryption important in chat applications?
-Encryption is important to ensure that the communication between clients is secure and cannot be easily intercepted or read by third parties.
What is the difference between encrypted and unencrypted network communication?
-Encrypted communication means that the messages are encoded and can only be decoded by the intended recipient using a private key, making it secure against eavesdropping. Unencrypted communication sends messages in clear text, which can be easily read by anyone who can access the network traffic.
What tool is used in the video to demonstrate the difference between encrypted and unencrypted network traffic?
-Wireshark is used to capture and analyze network traffic to show the difference between encrypted and unencrypted messages.
What is the RSA library used for in this context?
-The RSA library is used for implementing asymmetric encryption, which involves a public key for encrypting messages and a private key for decrypting them.
How does asymmetric encryption with RSA work?
-In asymmetric encryption, there are two keys: a public key that can be shared with anyone and is used to encrypt messages, and a private key that is kept secret and is used to decrypt messages. The private key can only decrypt messages that were encrypted with the corresponding public key.
What are the two main functions that handle message sending and receiving in the chat application?
-The two main functions are the sending messages function and the receiving messages function, both of which run in separate threads to handle communication with the client.
How are the keys exchanged between clients in the encrypted chat application?
-The keys are exchanged by sending the public key of one client to the other client over the established connection. The public key is then used to encrypt messages that can only be decrypted with the recipient's private key.
What is the significance of using a private key and a public key in the encryption process?
-The private key is used to decrypt messages that were encrypted with the corresponding public key. It is kept secret to ensure that only the intended recipient can decrypt and read the messages. The public key can be shared openly and is used to encrypt messages for the recipient.
How does the video demonstrate the effectiveness of the encryption in the chat application?
-The video demonstrates the effectiveness of encryption by showing the inability to read the content of encrypted messages when using Wireshark to sniff network traffic, as opposed to the clear text messages that can be easily read when encryption is not used.
Outlines
🔒 Building an Encrypted Chat in Python
The video introduces a tutorial on creating an encrypted chat application in Python. The host explains the concept of encrypted communication where messages are encrypted at the sender's end and decrypted at the receiver's, preventing unauthorized access to the message content. The demonstration includes using the RSA library for encryption and decryption, and the core Python modules like 'socket' and 'threading' for handling connections. The host also plans to compare encrypted and unencrypted network traffic using Wireshark.
🤖 Setting Up the Chat Environment
This paragraph details the initial setup for the chat application, including the creation of a server socket and binding it to a local IP address and port. The script differentiates between hosting a chat and connecting to an existing one, with the user's choice determining the role of the client. The server is set to accept only one connection, and placeholders are used for client communication. The paragraph also outlines the structure for sending and receiving messages in a loop, with threading introduced for asynchronous communication.
🔄 Exchanging Keys for Asymmetric Encryption
The host discusses the process of exchanging public keys between clients to enable encrypted communication. Each client generates a public-private key pair using the RSA library, with the public key being shared openly while the private key remains secret. The video script includes code snippets for sending the public key in PEM format and receiving it from the partner client. The paragraph also addresses the need for both clients to send and receive public keys to establish a secure communication channel.
📹 Demonstrating Encryption with Wireshark
After establishing the basic chat functionality, the host proceeds to implement RSA encryption for messages. The script is modified to encrypt messages before sending and decrypt them upon receipt. The host then uses Wireshark to demonstrate the difference between encrypted and unencrypted network traffic. By sniffing the network, the host shows how clear text messages can be easily read without encryption, emphasizing the importance of encryption for secure communication.
🔒 Finalizing the Encrypted Chat Application
The final part of the script involves running the chat application with encryption enabled and using Wireshark to confirm that the messages are no longer readable as clear text. The host tests the application by sending messages and observing that the encrypted data packets are uniform in size and their content is inaccessible. The video concludes with the host sending a long message to test the system's response and confirming that the encrypted chat application successfully prevents eavesdropping.
📢 Wrapping Up and Inviting Engagement
In the concluding paragraph, the host wraps up the tutorial by summarizing the process of building an encrypted chat in Python and encourages viewers to like, comment, and subscribe for more content. The host also reminds viewers to hit the notification bell to stay updated with future videos, ending the tutorial on a friendly and engaging note.
Mindmap
Keywords
💡Encrypted Chat
💡Python
💡RSA
💡Asymmetric Encryption
💡Socket Module
💡Threading Module
💡Wireshark
💡Public and Private Key
💡TCP Chat
💡Network Traffic
Highlights
Introduction to building an encrypted chat in Python using RSA library for encryption.
Explanation of the importance of encryption in secure communication to prevent eavesdropping.
Demonstration of the difference between encrypted and unencrypted network traffic using Wireshark.
Overview of asymmetric encryption with public and private keys in RSA.
Instructions on installing the RSA library using pip.
Code walkthrough for setting up a basic TCP chat server and client in Python.
Description of the process for clients to choose to host or connect to a chat session.
Explanation of the socket binding process to an IP address and port for the server.
Discussion on the exchange of public keys between clients for mutual encryption.
Implementation of threading for concurrent sending and receiving of messages.
Debugging a script to ensure proper thread execution for chat functionality.
Introduction of RSA encryption for message exchange in the chat application.
Generation of public-private key pairs at the start of the chat session.
Transmission of public keys between clients for encryption purposes.
Encryption of messages using the partner's public key before sending.
Decryption of received messages using the client's private key.
Testing the encrypted chat application and observing the clear text difference.
Using Wireshark to demonstrate the inability to read encrypted messages.
Conclusion on the effectiveness of the encrypted chat and its security implications.
Call to action for viewers to like, comment, subscribe, and turn on notifications for future videos.
Transcripts
what is going on guys welcome back in
today's video we're going to learn how
to build an encrypted chat in Python so
let us get right into it
[Music]
alright so I already have quite a lot of
tutorials on this channel on how to
build a chat in Python beat a TCP chat a
UDP chat a TCP chat room a web chat and
flask and in today's video we're going
to build an encrypted chat which means
that the communication between the
individual clients is going to be
encrypted we will not send any clear
text messages we will encrypt the
messages at the sending client and they
will be decrypted at the receiving
client making it basically impossible
for me as a third party to just listen
to the network traffic and say hey this
is the content of the message I can read
it as well just because I'm listening to
the network connection I can see what
you guys are talking about this is not
possible when the connection is
encrypted and in fact I'm going to show
you the difference between uh encryption
and not not encryption by using
Wireshark and actually listening to the
network traffic in this case only on my
machine but this can also be done in a
network if you're a router or something
you can just listen to the network
traffic that's happening and you can see
okay what package is being sent you can
extract the messages in cleared text
unless they are encrypted so this is
what we're going to do today in Python
and for that we're going to use One
external Library called RSA this is
going to be the library that we use for
the encryption and the rest is going to
be done in core python so we're going to
use the socket module the threading
module for the connection and everything
and we're going to use RSA the external
module for the actual encryption and for
that we're going to open up the command
line and we're going to say pip install
RSA and this is asymmetric encryption so
this basically means in a nutshell we
have a private key and a public key and
the public key is known by everyone so
if I have a public key you can know it
everyone can know it there's no secret
about this key everyone can know this
key and everyone can use this key to
encrypt a message for me and this
encrypted message can only be decrypted
with my private key which hopefully only
I know so the private key is also
sometimes called the secret key should
be kept secret at all times so only I
can know it because that's the only way
to decrypt the message and it's is
called asymmetric because we use a
different key for the encryption in a
different key for the decryption it's
not like using a password that you just
you know use to encrypt and decrypt
something you use a public key that is
known by everyone everyone can use that
to encrypt a message specifically for
you and only you can decrypt that
message with the respective private key
and it's basically impossible to derive
the private key from the public key so
that's the basic idea here and this is
what we're going to use as an encryption
here and we're going to start by saying
import socket
we're going to also import threading
and we're going to say import RSA and
before we talk about anything regarding
the
um encryption we're going to have a
basic choice because we want to use one
script in this video for the client and
we don't want to use a service script
and a client script so we want one
Central script and you can choose
whether you want to host or you want to
connect because one client will have to
host a conversation and one client will
connect to the uh to the hosted socket
because you cannot just have two clients
talking to each other one client has to
also
um host a connection at least when we
use TCP in UDP we could just send
packages so for that reason we're going
to say Choice equals input do you want
to host this is option one or do you or
let's say or to
connect option two
and then we say if maybe not a question
mark maybe colon
if the choice is equal to one then we're
going to host the connection so we're
going to say server equals socket dot
socket socket Dot afinet and socket dot
stockstream that basically means
internet socket TCP because connection
oriented protocol here if you want to
use UDP you would just say sock dgram
for datagram and then we say server dot
bind and we bind it to the local ipv4
address which can be found out in the
terminal on Windows by saying ipconfig
so in the command line
um we just go to the ethernet adapter
and we go to ipv4 and we copy this IP
address here
and I'm bind it to the Tuple of this and
the port number 99999
that's the basic idea and now we say
server Dot listen
and this is going to be a client that
will only accept one connection so we
don't need anything to handle uh we
don't we don't need
um multiple connections which is why
we're going to call the accept function
only once and we're going to say that
the client and the address that is
connected actually we don't need the
address so we can just leave this as an
empty as a placeholder here as an
unnamed return value so client is going
to be equal to server dot accept so when
a client connects to that server it's
going to just accept and this client
here will be our communication method to
that client that connected
um that's the idea here and then what
we're going to do
uh or actually let's do that later on um
we're going to exchange the keys here
because the other client needs to know
our public key and we need to know the
public key of the other client so that
they can encrypt something for us and we
can encrypt something for them uh but
we're going to add this here in a second
so let's just accept the connection for
now and let's say also otherwise we're
actually elif the choice happens to be
two then we're going to say that the
client is equal to socket socket again
socket
AF inet sock stream
but this time we're going to connect
client dot connect to in this case we're
going to use the same IP address of
course if you use this across the
network or maybe even in the internet
you would want to specify your IP
address here and the other person would
specify
um
the IP address of you your public IP
address
and you would specify if you connect the
public IP address of the other person
but in this case all of this happens on
one machine so we're going to do it with
the same IP address of course you can
also ask for input so you can say IP
equals input enter the IP address if you
want to but in this case I'm going to
use only my IP address so that's not
necessary
um and then here we would also exchange
the key otherwise we're going to say
exit
because that's an invalid input and then
essentially all we need to do is we need
to send messages and receive messages so
we're going to have uh one function
that's going to run a separate thread
which is going to be the sending
messages function
is going to run with a parameter C which
is going to be our client and we're
going to say while true we're going to
be asked for new messages so message is
going to be inputs what do you want to
send to this person and then we're going
to just say C sent
message dot encode
and uh we're gonna print you and then
whatever you
sent to the other client
so that you have a protocol a log
um and then
we're going to also have a receiving
messages
function which is going to say while
true
just print partner whatever the partner
says and this is going to be C receive
1024 bytes decode
that is the idea and then we're going to
just say threading dot threat
we don't need the input here
threading.thread one is going to Target
the sending messages function
um we're going to pass as arguments here
the client
and one is going to be the receiving
messages also with the argument client
uh what's the problem here client can be
undefined okay we're not gonna care
about this year because it's not going
to be undefined uh and one nice thing
you can see here is the decline is
always going to be the correct object to
talk about because we don't need the
server the server is only made for
accepting the connection the client is
the accepted connection so this is the
client we use to communicate with the
other client and here we also have the
client that we use to communicate so
basically after going through this block
here the client is going to be declined
and we can just do it like that so this
should already be an unencrypted chat
so I should already be able to run a
terminal here
and what was the key bind for split I
hope this is it there you go
I'm gonna navigate to my current working
directory which is this one and I'm
gonna do this here as well
uh wrong directory
and
now we're going to run main.py here
we're going to run main.py here
and I'm going to say I want to host and
I'm going to say I want to connect
and this crash the script why is that oh
because we need to start the threads as
well
that is the issue
so let's do the same thing I want to
host I want to connect
hey
hey hello
what's up
not much there you go so this works
um this is the ordinary connection now
we're going to encrypt this connection
so we're going to use RSA first of all
when we start the script to create our
own public private uh key pair and
usually you don't generate public and
private key for every session you have
your public key for a certain amount of
time and you just put it on your website
or something
um on a public website and uh you don't
necessarily generate it for each session
but in this case we're going to do it
like that we're going to say that the
public
key
private key
is going to be equal to RSA new keys
1024 bytes
or bits not sure
um and then we say
also that the public key of the partner
is going to be none by default
and we're going to get it then via the
socket connection so in this case here
once we accepted the connection we're
going to start by saying client dot send
so the hosting person the hosting client
is going to send the key first we're
going to send to our other client the
public key but of course in order to be
able to send it we need to package it as
bytes so we're going to say public key
dot safe pkcs1
um
and this is going to be sent
um
wasn't like yeah we need to specify the
format which is pem so basically we take
the public key we save it in this format
pm and then we receive also the public
key of the other client by saying public
well the partner is going to be equal to
client receive
1024 bytes and this is going to be the
basis for the public key and we're going
to say RSA public key uh what was it RSA
uh public key.load pkcs1
whatever we get here
and this is how it works we're going to
do the same thing down here but we're
going to do it the other way around
because when one client is sending the
other has to receive so the hosting
client will send first and then receive
and the connecting client will receive
and then send
and then the only thing that we need to
change now that we have that is we need
to encrypt every message and we need to
decrypt every message so instead of just
sending we're going to say
um RSA dot encrypt
we're going to encrypt the message dot
encode
so the encoded message will be encrypted
with the public key of the partner so
that the partner can then decrypt it
with the private key and we're now the
partner the partner or where now we
we're receiving from the partner in this
case we're not just printing whatever we
get here we're going to say that
um
what we want to print is RSA dot decrypt
and this is going to be whatever we
receive here in the form of bytes so
we're not going to decode in here we're
going to receive whatever we receive and
it's going to be decrypted with our
private key
and this result here will be decoded
that's that so let's see before we look
into Wireshark and package sniffing if
that works
so let me just split this here again
let's go to the python directory okay
wrong one again I changed the binding
recently which I have a different
functionality so I'm gonna navigate here
and then I'm gonna run
main py again
run main py again I want to host here
I want to connect here
test
hey okay so you can see everything is
clear text even though it's encrypted
actually and now let's see what the
actual difference is because right now
we just had the same functionality as
before but what changed behind the
scenes
so for that we're going to open up
Wireshark if you don't have Wireshark
you need to install it the installation
process is not too complicated you just
download Wireshark and you run it and
then what you can do is you can capture
certain interfaces so I can go to
options here I can choose an interface
usually of course you would want to
choose Ethernet or Wi-Fi or something
but since this is happening here on the
same machine so since I'm sniffing and
listening on the same machine I will go
with the loop back traffic because at
the end of the day I'm sending myself
some messages on localhost so I'm going
to start this network here and you can
see now packages are being sent all the
time for all sorts of reasons you don't
have to look at everything here but uh
let's briefly just disable the
encryption so we're going to say
they can still exchange keys but we're
not going to encrypt the messages we're
just gonna receive and decode
and we're going to just
send without encryption
and then we're going to see how easy it
is to get the content of these uh
packages so I'm going to run here
again maybe I should leave this open
there you go Main
Main
and now I'm going to say I want a host
here I'm gonna connect here and now I'm
gonna say hello world
what is up
My secret is that I love Matlab
but I am afraid
to admit rightfully so uh let's go into
Wireshark and let's see if we can see
some packages I can stop capturing now
and you can see here that we have some
packages from this IP to this IP so not
just from localhost localhost but from
specifically this IP to this IP and if I
click on it you can see that I see
something that no one wants to see
someone confessing his love to Matlab
and you can see my secret is that I love
Matlab but I'm afraid to admit it or to
admit you can see I just listened to the
packages and this package contains data
58 bytes and this is clear text I can
just read what's in it now we have a
second package here with some other data
and we should have more packages
containing more messages here I can see
what is up
then I can see here hello world so you
can see I can just look at the packages
with Wireshark and see what's happening
let's do the same thing now with the
encrypted
um lines of code
so now we encrypt every message and we
decrypt every message and I can run this
again so I can
uh okay I cannot close this I have to
restart this
sorry about that
let's navigate there again
I'm going to run main py here I'm going
to run main py here host connect and now
let's run Wireshark again options
loopback start continue without saving
listens again to some packages and now I
can just go ahead and type some messages
hello world
how are you
I am afraid to admit that I code in
Matlab
and now we can go to Wireshark again we
can stop the capturing and we can look
at the same packages so let's look again
there you go here we have again Source
the IP address and destination the IP
address if I click on it you can see now
I still have data but it's always going
to be 128 bytes and I cannot see what's
in it so I can I cannot read any of that
content here and we can also look at the
other packages so I can scroll up here
we have
another message again 128 bytes no
chance to see what's in there so I I
don't know I cannot see the content of
this message
um I don't know actually what would
happen if I include a very long message
probably I would have some uh problems
let's try maybe we can find out I mean
actually I stopped capturing right
but you can see the difference you can
see that we can no longer just sniff the
network connection we can no longer just
listen to the connection and see what
two clients are communicating about
because we only get some random bytes
that we don't know
how to interpret now I'm going to send a
bit more content maybe we're going to
get either an error or maybe we're going
to get larger packages or we're going to
get 128 again
uh
okay no overflow error okay but you can
see the difference you can see that when
we encrypt the messages for our clients
there's no difference in experience but
we are not able to just listen to the
connection and see what they're talking
about which is very good because now we
have a little bit more security as long
as you keep the private key private it's
basically impossible to uh to
unencrypt these messages and this is how
you do that this is how you build an
encrypted chat in Python all right so
that's it for today's video I hope you
enjoyed it and hope you learned
something if so let me know by hitting a
like button and leaving a comment in the
comment section down below and of course
don't forget to subscribe to this
Channel and hit the notification Bell to
not miss a single future video for free
other than that thank you much for
watching see you next video and bye
thank you
foreign
[Music]
5.0 / 5 (0 votes)