From cb5b9e6561a3320153236305b401241a8c327d82 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Juhani=20Krekel=C3=A4?= Date: Thu, 25 Jun 2020 12:47:22 +0300 Subject: [PATCH] Make the tool nicer --- tool.py | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/tool.py b/tool.py index 3df3566..b7bf17c 100644 --- a/tool.py +++ b/tool.py @@ -16,7 +16,6 @@ def enc_pk(pk, plaintext): raw_shared_secret = compact_ecdh_curve25519.ecdh(pk, ephemeral_sk) shared_secret = compact_xchapoly.hchacha20(raw_shared_secret, b'\x00'*24) nonce = os.urandom(24) - print(shared_secret, nonce, plaintext)#debg return ephemeral_pk + nonce + compact_xchapoly.enc(b'', shared_secret, nonce, plaintext) def dec_sk(sk, ciphertext): @@ -43,10 +42,17 @@ if __name__ == '__main__': print('pubkey:', pk.hex()) elif sys.argv[1] == '-E': pk = bytes.fromhex(sys.argv[2]) - print(enc_pk(pk, input().encode('utf-8')).hex()) + plaintext = sys.stdin.buffer.read() + sys.stdout.buffer.write(enc_pk(pk, plaintext)) elif sys.argv[1] == '-D': with open(sys.argv[2], 'rb') as f: sk = f.read() - print(dec_sk(sk, bytes.fromhex(input()))) + ciphertext = sys.stdin.buffer.read() + plaintext = dec_sk(sk, ciphertext) + if plaintext is None: + print('%s: Error: Ciphertext authentication failure' % os.path.basename(sys.argv[0]), file=sys.stderr) + sys.exit(1) + else: + sys.stdout.buffer.write(plaintext) else: usage()