title: SnmpV3 Trap Receiver with Authentication/Privacy - dynamically obtain engineID from agents
body: <p>I am trying to develop an <code>SNMPv3</code> Trap Receiver using the WebNms AdventNet API.
  If I use authentication and privacy options (<code>MD5 + DES</code>), I need to know the <code>engineID</code> of the Agent that sends the traps in
  order to decrypt the trap content. How can I obtain this <code>engineID</code> dynamically (without hardcoding it in my application)?
  <a href="https://www.webnms.com/snmp/help/snmpapi/snmpv3/v3/configureuser.html" rel="nofollow">I saw that it's possible to perform a discovery of the <code>engineID</code></p></a> </p></p>
  
  <p>But for this to work I need to provide the port used by the Agent when sends the traps (and the agent is a real network element which uses random source ports).</p>
  
  <p>The following code is working, but I hard-coded the <code>engineID</code>.
  Is there a different way to decrypt the traps without hardcoding the <code>engineID</code>?</p>
  
  <pre><code>public class DragosApp2 implements SnmpClient{
      public static void main(String[] args) throws SnmpException {
          SnmpAPI api = new SnmpAPI();
          SnmpEngineEntry snmpEntry = new SnmpEngineEntry("10.10.66.79");
          SnmpEngineTable engineTable = api.getSnmpEngine();
          engineTable.addEntry(snmpEntry);
          SnmpSession session = new SnmpSession(api);
          session.addSnmpClient(new DragosApp2());
          UDPProtocolOptions ses_opt = new UDPProtocolOptions();
          ses_opt.setLocalPort(162);
          session.setProtocolOptions(ses_opt);
          session.open();
          byte[] engineID = gethexValue("0x80001f888026f9036957333c81");  // HOW can I replace this part??
  
          USMUserEntry user = new USMUserEntry(new String("dragos3").getBytes(), engineID);
          user.setAuthProtocol(USMUserEntry.MD5_AUTH);
          user.setPrivProtocol(USMUserEntry.CBC_DES);
  
          byte[] authKey = USMUtils.password_to_key(USMUserEntry.MD5_AUTH, 
                                                  new String("12345678").getBytes(),
                                                  new String("12345678").getBytes().length,
                                                  engineID);
  
          byte[] privKey = USMUtils.password_to_key(USMUserEntry.MD5_AUTH,
                              new String("12345678").getBytes(),
                              new String("12345678").getBytes().length,
                              engineID,
                              USMUserEntry.CBC_DES);
  
          user.setAuthPassword(new String("12345678").getBytes());
          user.setPrivPassword(new String("12345678").getBytes());
          user.setAuthKey(authKey);
          user.setPrivKey(privKey);
          user.setSecurityLevel((byte)3);
          user.setEngineEntry(snmpEntry);
          USMUserTable uut = (USMUserTable)api.getSecurityProvider().getTable(3);
          uut.addEntry(user);
      }   
  }
  </code></pre>