#!/usr/bin/env python3 from base58 import b58decode from base64 import b64decode, b64encode from libnacl import crypto_sign_ed25519_sk_to_curve25519 as private_sign2crypt from libnacl import crypto_sign_ed25519_pk_to_curve25519 as public_sign2crypt from libnacl.sign import Signer, Verifier from libnacl.public import SecretKey, PublicKey, Box # solve this for poka sender_pub = 'DCovzCEnQm9GUWe6mr8u42JR1JAuoj3HbQUGdCkfTzSr' recip_seed = 'GA47R2WcyAw2GmEU8e2rYwYiLwLCZctfJtwzk87nhJpZ' nonce = '482xiAewUx0RM6oLglHQG//0ySelpnL9' title = 'LTmTBOGiakjGPGR7fCjWPnAVVFqkVjrmrxqY/iDWvdhWP+8oLtNgULScN2Rl/NifZGCA3LI=' content = 'YLVTaec04RoN9AMYNqVchnGknFipFTn3u06S/iCV84wuGLpsKoAsBbCfKci+7TsEdD7V3PQCyRSDANICRHuOiZOol1Svu87uQ3u1qwKSoAlcyloq1OdgTBNgkKCMkbQ0UYCsROr4bOuL8l3P7/xpK4+XFLwpmWCHuX+9fug6WovURlHSnVn8uYwuwn/ySF5CuYmULSXeU+1awrxDERdKPfysGYt2gi6DfX+LWd7ojbHPZTxvYlyjfgzG8bT6iDfFmtxUXEz9+3LpClewfZ7jdktaSM5dFE4+7CanBVQ=' signer = Signer(b58decode(recip_seed)) sk = SecretKey(private_sign2crypt(signer.sk)) verifier = Verifier(b58decode(sender_pub).hex()) pk = PublicKey(public_sign2crypt(verifier.vk)) box = Box(sk.sk, pk.pk) plain_title = box.decrypt(b64decode(nonce) + b64decode(title)) plain_content =box.decrypt(b64decode(nonce) + b64decode(content)) # now lets build the ciphertext again for a full round trip # poka's example used same keys for sender and recipient; we can reuse box # # libnacl.public.Box's encrypt() method prefixes the returned ciphertext # with a 24 byte nonce... whether sent in or not, # and the decrypt() method expects this nonce to be prefixed on the # ciphertext. # # show that poka's encrypted title is missing the 24b nonce cipherbytes = box.encrypt(plain_title, nonce=b64decode(nonce)) assert cipherbytes[:24] == b64decode(nonce) assert b64encode(cipherbytes[24:]).decode('utf-8') == title # show that poka's encrypted content is missing the 24b nonce cipherbytes = box.encrypt(plain_content, nonce=b64decode(nonce)) assert cipherbytes[:24] == b64decode(nonce) assert b64encode(cipherbytes[24:]).decode('utf-8') == content print(plain_title.decode('utf-8')) print(plain_content.decode('utf-8')) print(''' Thank you Poka, that was a frustrating and fun Sunday. -Spencer971 8XVSTMVWoRmRoLkELEMqeSXjviRWTaTqttnr7evcgrrF ''')