コード例 #1
0
  /** Encrypts a command to be sent to the recipient. */
  public EncryptedCommand(Server sender, byte[] senderKey, Server recipient, String data) {
    try {
      this.iv = ByteUtilities.toHexString(Aes.generateIv());

      // Nonce lists will be reset on app restart, but the serverID should be a new public key. This
      // means the nonce lists should all be trying to talk to a new entry, and should all agree on
      // the nonce.

      // Set up the nonce
      DecryptedPayload decryptedPayload = new DecryptedPayload();
      if (!outgoingNonces.containsKey(recipient.getServerId())) {
        outgoingNonces.put(recipient.getServerId(), 1L);
      }
      decryptedPayload.setNonce(outgoingNonces.get(recipient.getServerId()));
      this.nonce = decryptedPayload.getNonce();
      outgoingNonces.put(recipient.getServerId(), decryptedPayload.getNonce() + 1L);
      decryptedPayload.setPayload(data);
      data = ByteUtilities.toHexString(decryptedPayload.toJson().getBytes("UTF-8"));
      this.sender = sender;

      byte[] otherKey = ByteUtilities.toByteArray(recipient.getServerId());
      byte[] sharedKey = Secp256k1.generateSharedSecret(senderKey, otherKey);
      this.payload = Aes.encrypt(sharedKey, ByteUtilities.toByteArray(iv), data);
    } catch (Exception e) {
      LOGGER.error(null, e);
    }
  }