XChaCha20 Python3 CLI tool

Liste des GroupesRevenir à s crypt 
Sujet : XChaCha20 Python3 CLI tool
De : pollux (at) *nospam* tilde.club (Stefan Claas)
Groupes : sci.crypt
Date : 08. Sep 2024, 18:31:34
Autres entêtes
Organisation : Ch1ffr3punk5
Message-ID : <89f2389309ef1efcee726c83b77588c1556e289d@i2pn2.org>
User-Agent : flnews/1.3.0pre11 (for GNU/Linux)
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
PG5vbmNlZmlsZT4gPCBpbmZpbGUgPiBvdXRmaWxlCgpYQ2hhQ2hh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XBoZXIuZW5j
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

Date Sujet#  Auteur
8 Sep 24 * XChaCha20 Python3 CLI tool6Stefan Claas
8 Sep 24 +* Re: XChaCha20 Python3 CLI tool3Stefan Claas
8 Sep 24 i`* Re: XChaCha20 Python3 CLI tool2Stefan Claas
8 Sep 24 i `- Re: XChaCha20 Python3 CLI tool1Stefan Claas
7 Oct 24 `* Re: XChaCha20 Python3 CLI tool2Phil Carmody
7 Oct 24  `- Re: XChaCha20 Python3 CLI tool1Stefan Claas

Haut de la page

Les messages affichés proviennent d'usenet.

NewsPortal