I’d like to show how to establish a simple encrypted chat with AES-256-CBC using netcat and openssl only.
Run at your server
Server $ while IFS= read -r userinput;do echo "$userinput" | openssl enc -aes-256-ctr -a -A -k SuperSecretPWD;echo;done | nc -l -p 8877 | while IFS= read -r serveroutput;do echo "Incoming: $(echo "$serveroutput" | openssl enc -d -a -A -aes-256-ctr -k SuperSecretPWD)";done
Run at your client
Client $ while IFS= read -r userinput;do echo "$userinput" | openssl enc -aes-256-ctr -a -A -k SuperSecretPWD;echo;done | nc localhost 8877 | while IFS= read -r serveroutput;do echo "Incoming: $(echo "$serveroutput" | openssl enc -d -a -A -aes-256-ctr -k SuperSecretPWD)";done
You can use one password for the encryption and another for the decryption.
You can also try to use different cipher algorithms
# openssl list-cipher-algorithms
To measure speed
# openssl speed aes-256-ctr
Kudos to Mika Kousa ( R&D Engineer at Nokia Solutions and Networks ) who has helped to optimzie it.
To make it simpler to understand, below is an example of an unencrypted chat with netcat.
Run at your server
Server $ while IFS= read -r userinput; do echo $userinput ; done | nc -l -p 8877 | while IFS= read -r serveroutput; do echo "Incoming: $serveroutput"; done
**Run at your client**
Client $ while IFS= read -r userinput; do echo $userinput ; done | nc localhost 8877 |while IFS= read -r serveroutput; do echo "Incoming: $serveroutput"; done
Note for Windows users