Discussion:
XChaCha20 Python3 CLI tool
Add Reply
Stefan Claas
2024-09-08 17:31:34 UTC
Reply
Permalink
Hi all,

DJB's XChaCha20 stream cipher is pretty cool for Text messages (SMS),
if you have also a proper encoder for the binary output.

So I decided to convert my Go Code to Python3.

Here is the program to play with, base64 encoded.

$ ./xchacha20.py
Error: Incorrect number of arguments
Usage: ./xchacha20.py <keyfile> <noncefile> < infile > outfile

XChaCha20 encryption/decryption tool

Arguments:
keyfile Path to the file containing the key in hexadecimal format
noncefile Path to the file containing the nonce in hexadecimal format

The program reads from stdin and writes to stdout. Use input/output redirection for files.

Examples:
Encryption: ./xchacha20.py key.hex nonce.hex < plaintext.txt > encrypted.bin
Decryption: ./xchacha20.py key.hex nonce.hex < encrypted.bin > decrypted.txt

Note: The key should be 32 hex bytes (64 characters) long.
The nonce should be 24 hex bytes (48 characters) long.

IyEvdXNyL2Jpbi9lbnYgcHl0aG9uMwoKaW1wb3J0IHN5cwppbXBvcnQgb3MKZnJvbSBDcnlwdG8u
Q2lwaGVyIGltcG9ydCBDaGFDaGEyMAoKZGVmIHJlYWRfaGV4X2ZpbGUoZmlsZW5hbWUpOgogICAg
d2l0aCBvcGVuKGZpbGVuYW1lLCAncicpIGFzIGY6CiAgICAgICAgaGV4X3N0cmluZyA9IGYucmVh
ZCgpLnN0cmlwKCkKICAgIHJldHVybiBieXRlcy5mcm9taGV4KGhleF9zdHJpbmcpCgpkZWYgcHJp
bnRfdXNhZ2UoKToKICAgIHByaW50KGYiIiJVc2FnZToge3N5cy5hcmd2WzBdfSA8a2V5ZmlsZT4g
PG5vbmNlZmlsZT4gPCBpbmZpbGUgPiBvdXRmaWxlCgpYQ2hhQ2hhMjAgZW5jcnlwdGlvbi9kZWNy
eXB0aW9uIHRvb2wKCkFyZ3VtZW50czoKICBrZXlmaWxlICAgIFBhdGggdG8gdGhlIGZpbGUgY29u
dGFpbmluZyB0aGUga2V5IGluIGhleGFkZWNpbWFsIGZvcm1hdAogIG5vbmNlZmlsZ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IGZpbGU9c3lzLnN0
ZGVycikKICAgICAgICBzeXMuZXhpdCgxKQoKICAgIHRyeToKICAgICAgICBub25jZSA9IHJlYWRf
aGV4X2ZpbGUobm9uY2VfZmlsZSkKICAgIGV4Y2VwdCBFeGNlcHRpb24gYXMgZToKICAgICAgICBw
cmludChmIkVycm9yIHJlYWRpbmcgbm9uY2UgZmlsZToge2V9IiwgZmlsZT1zeXMuc3RkZXJyKQog
ICAgICAgIHN5cy5leGl0KDEpCgogICAgaWYgbGVuKGtleSkgIT0gMzI6CiAgICAgICAgcHJpbnQo
ZiJFcnJvcjogSW52YWxpZCBrZXkgc2l6ZS4gRXhwZWN0ZWQgMzIgaGV4IGJ5dGVzLCBnb3Qge2xl
bihrZXkpfSBieXRlcyIsIGZpbGU9c3lzLnN0ZGVycikKICAgICAgICBzeXMuZXhpdCgxKQoKICAg
IGlmIGxlbihub25jZSkgIT0gMjQ6CiAgICAgICAgcHJpbnQoZiJFcnJvcjogSW52YWxpZCBub25j
ZSBzaXplLiBFeHBlY3RlZCAyNCBoZXggYnl0ZXMsIGdvdCB7bGVuKG5vbmNlKX0gYnl0ZXMiLCBm
aWxlPXN5cy5zdGRlcnIpCiAgICAgICAgc3lzLmV4aXQoMSkKCiAgICBjaXBoZXIgPSBDaGFDaGEy
MC5uZXcoa2V5PWtleSwgbm9uY2U9bm9uY2UpCgogICAgd2hpbGUgVHJ1ZToKICAgICAgICBjaHVu
ayA9IHN5cy5zdGRpbi5idWZmZXIucmVhZCg4MTkyKQogICAgICAgIGlmIG5vdCBjaHVuazoKICAg
ICAgICAgICAgYnJlYWsKICAgICAgICBzeXMuc3Rkb3V0LmJ1ZmZlci53cml0ZShjaXBoZXIuZW5j
cnlwdChjaHVuaykpCgppZiBfX25hbWVfXyA9PSAiX19tYWluX18iOgogICAgbWFpbigpCg==


$ openssl rand -hex 32 > key
$ openssl rand -hex 24 > nonce
$ echo -n 'Hello World!' | xchacha20 key nonce | hexdump -Cv
00000000 01 eb 7f 2e d5 e9 a4 e0 c8 e4 e2 d6 |............|
0000000c
--
Regards
Stefan
Stefan Claas
2024-09-08 17:48:25 UTC
Reply
Permalink
Post by Stefan Claas
Hi all,
DJB's XChaCha20 stream cipher is pretty cool for Text messages (SMS),
if you have also a proper encoder for the binary output.
So I decided to convert my Go Code to Python3.
Here is the program to play with, base64 encoded.
Forgot to mention:

pip3 install pycryptodome
--
Regards
Stefan
Stefan Claas
2024-09-08 18:54:28 UTC
Reply
Permalink
Post by Stefan Claas
Hi all,
DJB's XChaCha20 stream cipher is pretty cool for Text messages (SMS),
if you have also a proper encoder for the binary output.
So I decided to convert my Go Code to Python3.
Here is the program to play with, base64 encoded.
Just for fun :-):

$ echo -n 'Test' | xchacha20 key nonce | armor
-----BEGIN PGP MESSAGE-----

eQyt7Q==
=yEnM
-----END PGP MESSAGE-----
--
Regards
Stefan
Stefan Claas
2024-09-08 19:03:11 UTC
Reply
Permalink
Post by Stefan Claas
Post by Stefan Claas
Hi all,
DJB's XChaCha20 stream cipher is pretty cool for Text messages (SMS),
if you have also a proper encoder for the binary output.
So I decided to convert my Go Code to Python3.
Here is the program to play with, base64 encoded.
$ echo -n 'Test' | xchacha20 key nonce | armor
-----BEGIN PGP MESSAGE-----
eQyt7Q==
=yEnM
-----END PGP MESSAGE-----
And a padded SMS in two fiver groups, for easy and error
free typing and reading with a dumb phone. :-)

$ echo -n 'Test' | mp | xchacha20 key nonce2 | az | ug -g
Data successfully padded.
Original size: 4 bytes
Padded size: 5 bytes
OCMOM PCSKG
--
Regards
Stefan
Phil Carmody
2024-10-07 14:17:02 UTC
Reply
Permalink
Post by Stefan Claas
Hi all,
DJB's XChaCha20 stream cipher is pretty cool for Text messages (SMS),
if you have also a proper encoder for the binary output.
So I decided to convert my Go Code to Python3.
Here is the program to play with, base64 encoded.
$ ./xchacha20.py
Error: Incorrect number of arguments
Usage: ./xchacha20.py <keyfile> <noncefile> < infile > outfile
XChaCha20 encryption/decryption tool
keyfile Path to the file containing the key in hexadecimal format
noncefile Path to the file containing the nonce in hexadecimal format
Why is the nonce in a file - are you planning on using it multiple
times?

Phil
--
We are no longer hunters and nomads. No longer awed and frightened, as we have
gained some understanding of the world in which we live. As such, we can cast
aside childish remnants from the dawn of our civilization.
-- NotSanguine on SoylentNews, after Eugen Weber in /The Western Tradition/
Stefan Claas
2024-10-07 16:22:08 UTC
Reply
Permalink
Post by Phil Carmody
Post by Stefan Claas
Hi all,
DJB's XChaCha20 stream cipher is pretty cool for Text messages (SMS),
if you have also a proper encoder for the binary output.
So I decided to convert my Go Code to Python3.
Here is the program to play with, base64 encoded.
$ ./xchacha20.py
Error: Incorrect number of arguments
Usage: ./xchacha20.py <keyfile> <noncefile> < infile > outfile
XChaCha20 encryption/decryption tool
keyfile Path to the file containing the key in hexadecimal format
noncefile Path to the file containing the nonce in hexadecimal format
Why is the nonce in a file - are you planning on using it multiple
times?
I always use nonces from files and for each message a new one. I have
my nonce rachtet program 'nora' which calculates them, without needing
to transfer them.

https://github.com/706f6c6c7578/nora
--
Regards
Stefan
Loading...