def encrypt(self, key: bytes, nonce: Optional[bytes] = None) -> EncryptedWallet: serialized = jsonpickle.encode(self) byts = serialized.encode() nonce = nonce if nonce else randombytes(crypto_secretbox_NONCEBYTES) raw = crypto_secretbox(byts, nonce, key) return EncryptedWallet(raw, nonce)
def __encrypt(self, plaintext): cipherText = libnacl.crypto_secretbox( plaintext, struct.pack('<I', self.__CurrentPacketNumAudio) + "\x00" * 20, self.__secret_key) if (self.__CurrentPacketNumAudio > 32766): self.__CurrentPacketNumAudio = 0 return cipherText
def test_secret_box(self): msg = b'Are you suggesting coconuts migrate?' sk1 = libnacl.utils.salsa_key() nonce1 = libnacl.utils.rand_nonce() enc_msg = libnacl.crypto_secretbox(msg, nonce1, sk1) self.assertNotEqual(msg, enc_msg) clear_msg = libnacl.crypto_secretbox_open(enc_msg, nonce1, sk1) self.assertEqual(msg, clear_msg)
def encrypt(self, msg, nonce=None): ''' Encrypt the given message. If a nonce is not given it will be generated via the rand_nonce function ''' if nonce is None: nonce = libnacl.utils.rand_nonce() if len(nonce) != libnacl.crypto_secretbox_NONCEBYTES: raise ValueError('Invalid Nonce') ctxt = libnacl.crypto_secretbox(msg, nonce, self.sk) return nonce + ctxt
def test_secretbox(self): msg = b'Are you suggesting coconuts migrate?' nonce = libnacl.utils.rand_nonce() key = libnacl.utils.salsa_key() c = libnacl.crypto_secretbox(msg, nonce, key) m = libnacl.crypto_secretbox_open(c, nonce, key) self.assertEqual(msg, m) with self.assertRaises(ValueError): libnacl.crypto_secretbox(msg, b'too_short', key) with self.assertRaises(ValueError): libnacl.crypto_secretbox(msg, nonce, b'too_short') with self.assertRaises(ValueError): libnacl.crypto_secretbox_open(c, b'too_short', key) with self.assertRaises(ValueError): libnacl.crypto_secretbox_open(c, nonce, b'too_short')
def encodeMessage(self, d, useWritePassword=False): "Given a JSON message, encode it so as to be suitable to send to another node" if useWritePassword and not self.writePassword: raise RuntimeError("You don't have a write password") pw = self.writePassword if useWritePassword else self.syncKey pw = pw.encode("utf8").ljust(32, b'\0') r = jsonEncode(d).encode('utf8') t = struct.pack("<Q", int(time.time() * 1000000)) r = libnacl.crypto_secretbox(r, t + b'\0' * 16, pw) return libnacl.crypto_generichash( pw)[:16] + self.localNodeVK + libnacl.crypto_sign( t + r, self.localNodeSK)
def __decrypt(self, msg): nonce = msg[-4:] + "\x00" * 20 if msg[:2] == '\x81\xc9': msg = msg[0x18:-4] elif msg[:2] == '\x90\x78': msg = msg[0x1C:-4] else: msg = msg[0x18:-4] Logger.LogMessage('wtf header received: {}'.format( msg.encode('hex')), log_level=LogLevel.WARNING) plaintext = libnacl.crypto_secretbox(msg, nonce, self.__secret_key) Logger.LogMessage("Decrypted %d bytes %s:" % (len(plaintext), plaintext[0x10:].encode('hex'))) return plaintext
def encrypt_to(self, message, recipients): """Encrypt a secret message to ``recipients`` using our private key.""" nonce = os.urandom(24) key = os.urandom(32) secret = dict( sender=json_bytes(self.publicKey), nonce=json_bytes(nonce), secret=json_bytes(libnacl.crypto_secretbox(message, nonce, key)), keys={}, ) print recipients for pub in recipients: box = libnacl.crypto_box(key, nonce, pub, self.privateKey) secret['keys'][json_bytes(pub)] = json_bytes(box) return secret
def encrypt(self,d,k,nonce): return libnacl.crypto_secretbox(d, nonce, k)
def _encryptConsole(cls, command, nonce): command = command.encode('UTF-8') if cls._consoleKey is None: return command return libnacl.crypto_secretbox(command, nonce, cls._consoleKey)
def _encryptConsole(cls, command, nonce): if cls._consoleKey is None: return command return libnacl.crypto_secretbox(command, nonce, cls._consoleKey)
def encrypt_command(self, cmd, nonce): """encrypt console""" cmd = cmd.encode('utf-8') return libnacl.crypto_secretbox(cmd, nonce, self.console_key)
def encrypt(self, pt, key, counter): if len(pt) % 16: pt += '\x00' * (16 - len(pt) % 16) return libnacl.crypto_secretbox(pt, counter, key)
def encrypt(self, d, k, nonce): return libnacl.crypto_secretbox(d, nonce, k)