Make the tool nicer

This commit is contained in:
Juhani Krekelä 2020-06-25 12:47:22 +03:00
parent 2abf5ccf6f
commit cb5b9e6561
1 changed files with 9 additions and 3 deletions

12
tool.py
View File

@ -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()