$$$'`$$$$$$$$$$$$$'`$$$$$
$$$$  $$$$$$$$$$$  $$$$$$
$$$$. `$' \' \$`  $$$$$$$
$$$$$. !\  i  i .$$$$$$$$
$$$$$$   `--`--.$$$$$$$$$
$$$$$$L        `$$$$$^^$$
$$$$$$$.   .'   ""~   $$$
$$$$$$$$.  ;      .e$$$$$
$$$$$$$$$   `.$$$$$$$$$$$
$$$$$$$$    .$$$$$$$$$$$$
$$$$$$$     $$$$$$$$$$$$$

Python CLI encrypted chat program.

This CLI program written in Python allows you to chat securely with encryption (Fernet).

git repo: https://codeberg.org/mikolajlubiak/crypt

Usage:

Providing key is optional, just make sure both client and server use the same key.

Server:

./server.py "IP address" "Port" "Key"

example:

./server.py "127.0.0.1" "12345"

Client:

./client.py "IP address" "Port" "Key"

example:

./client.py "127.0.0.1" "12345" "ojzqTVTIsuE6nemN9huFlYMNmyTHr9dUu-E5bL8HrTo="

Code explanation:

Server:

#!/usr/bin/env python

^ This is SheBang, it allows you to use this syntax while running program in *nix systems:

./filename

example:

./filename.fileextension "1'st parameter" "secound PARAMETER"
from socket import socket, AF_INET, SOCK_STREAM
from threading import Thread
from time import sleep
from subprocess import check_call
from sys import argv, executable

^ This part of code import necessary function from Python modules.

try:
    from cryptography.fernet import Fernet
except:
    check_call((executable, '-m', 'pip', 'install', 'cryptography'))
    from cryptography.fernet import Fernet

^ This also import's function from module, but this module doesn't belong to Python's standard libraries, so there is a chance that you may don't have this module and this part of code makes sure that you have this module, it tries to import this function, but if Python returns any error the code will run:

check_call((executable, '-m', 'pip', 'install', 'cryptography'))

which simply installs this module, and then the code imports the function again.

class Server

^ This initializes class named "Server".

s = socket(AF_INET, SOCK_STREAM)

^ This aliases "socket(AF_INET, SOCK_STREAM)" to "s".

def __init__(self):
    if len(argv) == 4: k = argv[3].encode()
    else:
       k = Fernet.generate_key()
        print(k.decode())
    self.f = Fernet(k)
    self.s.bind((argv[1], int(argv[2])))
    self.s.listen(3)
    self.client, self.addr = self.s.accept()

^ This code section is the "__init__" function, this function initializes when you make news object from this class and may do diffrent things on diffrent objects. The "if" statement checks whether you have provided parameter with the key, and if you have done so, it aliases this key to the "k" variable, else it generates the key and sets it up to the "k" variable and prints it. Then the code aliases "Fernet(k)" to self variable called "f", the "Fernet(k)" is root to things like "Fernet(k).encrypt()" and "Fernet(k).decrypt()". After that the code opens the IP address from 1'st parameter on port provided in 2'nd parameter and listens (ie. whaits for connecton). If it has been asked for connection it accepts it and saves the client socket and the IP address.